More Related Content
More from maamir farooq (20)
Sortings
- 1. Insertin
int main()
{ int
a[16],i,j,k,temp,n;
cout<<"enter
number of
elements ";
cin>>n;
cout<<"enter the
elementsn";
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(i=1;i<n;i++)
{
for(j=i;j>=1;j--)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
else
break;
}
}
cout<<"sorted
arrayn"<<endl;
for(k=0;k<n;k++)
{
cout<<a[k]<<endl;
}
return 0;
}
Selection
int main()
{
int data[10],temp,i,n;
cout<<"Enter number of
elementsn";
cin>>n;
cout<<"Enter integersn";
for (i = 0; i < n; i++)
cin>>data[i];
for(int i=0;i<n-1;i++)
for(int j=i+1;j<n;j++)
if(data[i]<data[j])
{
temp=data[i];
data[i]=data[j];
data[j]=temp;
}
cout<<"nSorted Array Is";
for(int i=0;i<n;i++)
cout<<"n"<<data[i];
cout<<endl;
return 0;
}
Bubble
int main()
{
int s,temp,i,j,a[20];
cout<<"Enter total numbers of
elements: ";
cin>>s;
cout<<"Enter elements: ";
for(i=0;i<s;i++)
cin>>a[i];
//Bubble sorting algorithm
for(i=s-2;i>=0;i--)
{
for(j=0;j<=i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
cout<<"After sorting: "<<endl;
for(i=0;i<s;i++)
cout<<a[i]<<endl;
return 0;
}
- 2. Merg
#include<iostream>
#include<conio.h>
using namespace
std;
void mergesort(int
*,int,int);
void merge(int
*,int,int,int);
int a[20],i,n,b[20];
main()
{
cout <<"n enter no
of elements";
cin >> n;
cout <<"enter the
elements";
for(i=0;i<n;i++)
cin >> a[i];
mergesort(a,0,n-1);
cout <<" numbers
after sort";
for(i=0;i<n;i++)
cout << a[i] << " ";
getch();
}
Stack
#include <iostream>
#define STACKSIZE 10
using namespace std;
int Top=-1;
int Stack[STACKSIZE];
void Push(int);
int Pop(void);
bool IsEmpty(void);
bool IsFull(void);
void Traverse(void);
int main( )
{ int item, choice;
while( 1 )
{
cout<< "nnnnn";
cout<< " ******* STACK OPERATIONS
********* nn";
cout<< " 1- Push item n 2- Pop Item n";
cout<< " 3- Traverse / Display Stack Items
n 4- Exit.";
cout<< " nnt Your choice ---> ";
cin>> choice;
switch(choice)
{ case 1: if(IsFull())
cout<< "n Stack Full/Overflown";
- 3. void mergesort(int
a[],int i,int j)
{
int mid;
if(i<j)
{
mid=(i+j)/2;
mergesort(a,i,mid);
mergesort(a,mid+1,j
);
merge(a,i,mid,j);
}
}
void merge(int a[],int
low,int mid ,int high)
{
int h,i,j,k;
h=low;
i=low;
j=mid+1;
while(h<=mid &&
j<=high)
{
if(a[h]<=a[j])
b[i]=a[h++];
else
else
{ cout<< "n Enter a number: ";
cin>>item;
Push(item); }
break;
case 2: if(IsEmpty())
cout<< "n Stack is empty) n";
else
{
item=Pop();
cout<< "n deleted from Stack =
"<<item<<endl;
}
break;
case 3: if(IsEmpty())
cout<< "n Stack is empty) n";
else
{
cout<< "n List of Item pushed on
Stack:n";
Traverse();
}
break;
case 4: exit(0);
default:
cout<< "nnt Invalid Choice: n";
- 4. b[i]=a[j++];
i++;
}
if( h > mid)
for(k=j;k<=high;k++)
b[i++]=a[k];
else
for(k=h;k<=mid;k++)
b[i++]=a[k];
cout <<"n";
for(k=low;k<=high;k
++)
{ a[k]=b[k];
cout << a[k] <<" ";
}
}
}
}
}
void Push(int item)
{
Stack[++Top] = item;
}
int Pop( )
{
return Stack[Top--];
}
bool IsEmpty( )
{ if(Top == -1 )
return true ;
else
return false;
}
bool IsFull( )
{ if(Top == STACKSIZE-1 )
return true;
else
return false;
}
void Traverse( )
{ int TopTemp = Top;