Print Friendly and PDF
implementation of BUBBLE SORT, SELECTION SORT,INSERTION SORT,BUCKET SORT,SHELL SORT
#include<iostream> 
#include<conio.h> 
#include<stdlib.h> 
#include<stdio.h> 
using namespace std; 
int main() 
{
int a[10],n=0,choice=0; 
int read_array(int []); 
int print_array(int[],int n); 
int bubble_sort(int[],int n); 
int select_sort(int[],int n); 
int insert_sort(int[],int n); 
int bucket_sort(int[],int n); 
int shell_sort(int[],int n); 
int merge(int a[],int b[][20]); 
while(1) 
{ 
cout<<"\t============================="; 
cout<<"\n\t======== MAIN MENU ========"; 
cout<<"\n\t =========================="; 
cout<<"\n\n\t 1> BUBBLE SORT"; 
cout<<"\n\n\t 2> SELECTION SORT"; 
cout<<"\n\n\t 3> INSERTION SORT"; 
cout<<"\n\n\t 4> BUCKET SORT"; 
cout<<"\n\n\t 5> SHELL SORT"; 
cout<<"\n\n\t 6> EXIT"; 
cout<<"\n\n\t ENTER YOUR CHOICE :=> "; 
cin>>choice; 
switch(choice) 
{
case 1:n=read_array(a); 
cout<<"\n\n\t THE ARRAY ELEMENTS ARE :: "; 
print_array(a,n); 
bubble_sort(a,n); 
cout<<"\n\n\t THE SORTED LIST IS :: "; 
print_array(a,n); 
break;
case 2:n=read_array(a); 
cout<<"\n\n\t THE ARRAY ELEMENTS ARE :: "; 
print_array(a,n); 
select_sort(a,n); 
cout<<"\n\n\t THE SORTED LIST IS :: "; 
print_array(a,n); 
break;
case 3:n=read_array(a); 
cout<<"\n\n\t THE ARRAY ELEMENTS ARE :: "; 
print_array(a,n); 
insert_sort(a,n); 
cout<<"\n\n\t THE SORTED LIST IS :: "; 
print_array(a,n); 
break;
case 4:n=read_array(a); 
cout<<"\n\n\t THE ARRAY ELEMENTS ARE :: "; 
print_array(a,n); 
bucket_sort(a,n); 
cout<<"\n\n\t THE SORTED LIST IS :: "; 
print_array(a,n); 
break; 
case 5:n=read_array(a); 
cout<<"\n\n\t THE ARRAY ELEMENTS ARE :: "; 
print_array(a,n); 
shell_sort(a,n); 
cout<<"\n\n\t THE SORTED LIST IS :: "; 
print_array(a,n); 
break;
case 6: 
cout<<"\n\n\t\t THANK YOU FOR USING THIS SOFTWARE"; 
cout<<"\n\n\t created By:Mr. Faheem dad"; 
exit(0); 
} 
getche(); 
} 
} 
int read_array(int a[]) 
{ 
int n,i; 
cout<<"\n\n\t ENTER THE ARRAY LENGTH :: "; 
cin>>n; 
for(i=0;i<n;i++) 
{ 
cout<<"\n\n\t ENTER THE ELEMENT ::"<<i; 
cin>> a[i]; 
} 
return (n); 
} 
int print_array(int a[],int n) 
{ 
int i; 
for(i=0;i<n;i++) 
cout<<"\t"<<a[i]; 
} 
int bubble_sort(int a[],int n) 
{ 
int i,j,temp; 
for(i=0;i<n-1;i++) 
{ 
for(j=0;j<n-1;j++) 
if(a[j]>a[j+1]) 
{ 
temp=a[j]; 
a[j]=a[j+1]; 
a[j+1]=temp; 
} 
cout<<"\n\n\t PASS ::"<<i+1; 
print_array(a,n); 
} 
}
int select_sort(int a[],int n) 
{ 
int i,j,temp; 
for(i=0;i<n-1;i++) 
{ 
for(j=i+1;j<n;j++) 
{ 
if(a[i]>a[j]) 
{ 
temp=a[i]; 
a[i]=a[j]; 
a[j]=temp; 

} 
}
cout<<"\n\n\t PASS :: "<<i+1; 
print_array(a,n); 
} 
}
int insert_sort(int a[],int n) 
{ 
int i,j,temp; 
for(i=0;i<n;i++)
{
temp=a[i]; 
for(j=i-1;temp<a[j] && j>=0;j--) 
a[j+1]=a[j]; 
a[j+1]=temp; 
cout<<"\n\n\t PASS :: "<<i; 
print_array(a,n); 
} 
}
int shell_sort(int a[],int n) 
{ 
int i,j,k,d,t,x,flag=0; 
int p=1; 
for(d=n/2;d>=1;d=d/2) 
{ 
flag=0; 
for(j=0;j+d<n;j++) 
{ 
for(k=j;k>=0;k=k-d) 
{ 
if(a[k]>a[k+d]) 
{ 
t=a[k]; 
a[k]=a[k+d]; 
a[k+d]=t; 
flag=1; 
} 
} 
} 
if(flag==1) 
{ 
cout<<"\n\n\t PASS :: "<<p++; 
for(x=0;x<n;x++) 
{
cout<<"\t "<<a[x]; 
} 
} 
} 
}
int bucket_sort(int a[],int n) 
{ 
int b[10][20],i,j,row,col,no,x,p=1; 
for(i=1;i<=5;i++) 
{ 
int initzerocol(int b[10][20]); 
for(j=0;j<n;j++) 
{ 
int returndigit(int no,int i); 
row=returndigit(a[j],i); 
b[row][0]=b[row][0]+1; 
col=b[row][0]; 
b[row][col]=a[j]; 
} 
int merge(int a[],int n); 
cout<<"\n\n\t PASS :: "<<p++; 
for(x=0;x<n;x++) 
cout<<"\t "<<a[x]; 
} 
}
int initzerocol(int b[][20]) 
{ 
int i;for(i=0;i<10;i++) 
b[i][0]=0; 
}
int returndigit(int no,int i) 
{ 
int k; 
for(k=1;k<i;k++) 
no=no/10; 
return(no%10); 
}
int merge(int a[],int b[][20]) 
{ 
int i,j,k=0; 
for(i=0;i<10;i++) 
for(i=0;i<=b[i][0];j++) 
{ 
a[k]=b[i][j]; 
k=k+1; 
} 
} 

zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: