menu Driven Program For Selection Sort, Bubble Sort, Insertion Sort

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

//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;
}
}
}
}

void selectionsort(int arr[],int m)


{
int pos=0,small,temp;
for(int i=0;i<m;++i)
{
small=arr[i];
pos=i;
for(int j=i+1;j<m;++j)
{
if(small>arr[j])
{
small=arr[j];
pos=j;
} }
temp=arr[i];
arr[i]=arr[pos];
arr[pos]=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');
}

You might also like