Professional Documents
Culture Documents
menu Driven Program For Selection Sort, Bubble Sort, Insertion Sort
menu Driven Program For Selection Sort, Bubble Sort, Insertion Sort
menu Driven Program For Selection Sort, Bubble Sort, Insertion Sort
#include<iostream.h>
void bubblesort(int arr[],int m)
{ int temp,ctr=0;
for(int i=0;i<m;++i)
{ for(int j=0;j<((m-1)-i);++j)
{
if (arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
1
void insertionsort(int arr[],int m)
{
int temp,j;
for(int i=1;i<m;++i)
{
temp=arr[i];
j=i-1;
while(temp<arr[j] && j>=0)
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
}
int main()
{
int arr[30],m,x;
char ch='y';
do
{
cout<<"Enter Size of the array::";
cin>>m;
cout<<"Enter elements of Array::";
for(int i=0;i<m;++i)
{
cin>>arr[i];
}
cout<<"Select the sorting Method:";
cout<<"\n1)Bubble Sort. \n2)Selection Sort. \n3)Insertion Sort.";
cout<<"\n Enter ur choice::";
cin>>x;
switch(x)
{
2
case 1:
{ cout<<"Sorting using Bubble sort";
bubblesort(arr,m);
break;
}
case 2:
{ cout<<"Sorting Using Selection Sort";
selectionsort(arr,m);
break;
}
case 3:
{ cout<<"Sorting Using insertion Sort";
insertionsort(arr,m);
break;
}
default:
cout<<"Wrong Selection";
}
cout<<"\nSorted array is as follows::\n";
for(x=0;x<m;++x)
cout<<"\t"<<arr[x];
cout<<"\n Do u want to continue::";
cin>>ch;
}while(ch=='y' || ch=='Y');
}