Data Structure with CPP
Bubble short in cpp
#include<iostream> // bubble short
using namespace std;
int main()
{
int i,j,temp;
int n[5];
cout<<"Enter array data";
for(i=0;i<5;i++)
{
cin>>n[i];
}
cout<<"unshorted array data \n";
for(i=0;i<5;i++)
{
cout<<n[i]<<"\t";
}
cout<<endl;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(n[j]<n[i])
{
temp=n[i];
n[i]=n[j];
n[j]=temp;
}
}
}
cout<<"shorted array data\n";
for(i=0;i<5;i++)
{
cout<<n[i]<<"\t";
}
return 0;
}
Selection Short in cpp.
the selection sort technique first selects the smallest element in the array and swaps it with the first element in the array. finding the smallest element in every pass and placing it in the correct position
#include < stdio.h>
int main()
{
int i,j,num,temp,p,min;
int arr[5];
printf("Enter The Number of element");
scanf("%d",&num);
printf("Enter The Elements");
for(i=0;i<num;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<num-1;i++)
{
min=arr[i];
p=i;
for(j=i+1;j<num;j++)
{
if(min>arr[j])
{
min=arr[j];
p=j;
}
}
temp=arr[i];
arr[i]=arr[p];
arr[p]=temp;
}
for(i=0;i<num;i++)
{
printf("%d ",arr[i]);
}
return 0;
}
Insertion short
In this sorting technique, the elements are sorted by comparing the elements with their previous elements. It starts by comparing the second element with the first element. If the second element is smaller than the first, then we will swap it.
#include<iostream>
using namespace std;
int main()
{
int a[5],i,j,temp,min;
cout<<"enter the 5 number in an array"<<endl;
for(i=0;i<5;i++)
cin>>a[i];
cout<<"the list are "<<endl ;
for(i=0;i<5;i++)
cout<<a[i]<<endl;
for(i=0;i<5;i++)
{
temp=a[i],j=i-1;
while(temp<a[j] && j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=temp;
}
cout<<"out put are"<<endl ;
for(i=0;i<5;i++)
cout<<a[i]<<endl;
return 0;
}
Binary Search program using array :
#include<iostream>
using namespace std;
int main()
{ //in binary search first check mid point is equal
//or not with item
int ar[50],item,beg,last,n,mid,x=0,l; // if item is greater then chenge mid with beg
cout<<"Enter Size of array (Max 50 )"; //if item is smaller then change mid with last
cin>>n;
cout<<"Enter array elements in asc order \n" ;
for(int i=0;i<n;i++)
{
cin>>ar[i];
}
cout<<"enter element to be searched for\n";
cin>>item;
beg=0;
last=n-1;
while(beg<=last && !x)
{
mid=(beg+last)/2;
if(item==ar[mid])
{
x=1;
l=mid;
}
else if(item>ar[mid])
{
beg=mid+1;
}
else
{
last=mid-1;
}
}
if(x==1)
{
cout<<"found possition at "<<l+1;
}
else
{
cout<<"not found" ;
}
return 0;
}
Stack program using array :
#include<iostream>
using namespace std;
#define length 10
void push(int);
int pop();
void listStack();
int top=-1;
int stack[length];
int main()
{
int ch,data;
do{
cout << endl << "1: push";
cout << endl << "2: pop";
cout << endl << "3: exit";
cout << endl << "enter choice";
cin >> ch;
switch(ch)
{
case 1:
cout<<"enter number" ;
cin>>data;
push(data);
listStack();
break;
case 2:
cout << "data poped= " << pop();
listStack();
break;
case 3:exit(0);
}
} while(ch);
return 0;
}
void push(int data)
{
if(top+1==length)
{
cout << "stack overflow\n Cannot enter new data";
return;
}
top++;
stack[top]=data;
}
int pop()
{
int tempVar;
if(top==-1)//we can also make isEmpty()
{
cout << "stack is underflow (Empty)";
return(-1);
}
tempVar = stack[top];
top--;
return tempVar;
}
void listStack()
{
if(top<0)
{
cout <<" stack empty";
return;
}
cout << endl << "The stack is" << endl ;
for(int i=top;i>=0;i--)
cout << stack[i] << endl;
}
Queue program in cpp using array :
#include<iostream>
using namespace std;
int num[5]; //Maximum 5 numbers
int front=-1,rear=-1;
void qins(int n);
void qdel();
void qshow();
void qins(int n)
{
if (rear==5-1)
//queue is full
cout<<"Insertion not possible\n";
else
{
if (front==-1)
//Que empty
{
front++;
num[front]=n;
rear=front;
}
else
{
rear++; //Data inserted at the end
num[rear]=n;
}
}
}
void qdel()
{
if (front==-1)
cout<<"Que empty\n";
else
{
int n;
if (front==rear)
{
n=num[front];
cout<<"No. removed is "<<n<<endl;
front=rear=-1; //re-initializing for new entry of data
}
else
{
int n=num[front];
front++; //front position increased
cout<<"No. removed is "<<n<<endl;
rear--;
}
}
}
void qshow()
{
if (front==-1)
cout<<"Que is empty\n";
else
{
for(int i=front;i<=rear;i++)
{
cout<<num[i]<<" ";
}
cout<<endl;
}
}
int main()
{
int ch,n;
ch=0;
while (ch<4)
{
cout<<" 1.ins\n 2.del\n 3.show\n 4.exit";
cin>>ch;
switch(ch)
{
case 1: cout<<"Enter no";cin>>n;
qins(n);
break;
case 2: qdel();
break;
case 3: qshow();
}
}
return 0;
}
Bubble short
#include<iostream> // bubble short
using namespace std;
int main()
{
int i,j,temp;
int n[5];
cout<<"Enter array data";
for(i=0;i<5;i++)
{
cin>>n[i];
}
cout<<"unshorted array data \n";
for(i=0;i<5;i++)
{
cout<<n[i]<<"\t";
}
cout<<endl;
for(i=0;i<5;i++)
{
for(j=i+1;j<5;j++)
{
if(n[j]<n[i])
{
temp=n[i];
n[i]=n[j];
n[j]=temp;
}
}
}
cout<<"shorted array data\n";
for(i=0;i<5;i++)
{
cout<<n[i]<<"\t";
}
return 0;
}
Last Updated on : 23rd Jul 2024 20:25:39 PM
Latest Update
- JavaScript Interview Question MongoDb Command Curd Operation Node MonogoDb Express EJSC Practice Question Example Data Structure with CPPjava practice questionCurd operation with localstorage in javascriptComplete curd operation in react with json server and axios and router-domCPP Practice Question React Interview Question Java Pattern Program