0% found this document useful (0 votes)
80 views

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

This document contains C++ code for a menu-driven program that allows the user to select between bubble sort, selection sort, and insertion sort algorithms to sort an integer array. The program prompts the user to enter the array size and elements, selects a sorting method, calls the corresponding sorting function to sort the array, and prints the sorted array. It then asks the user if they want to continue sorting another array or end the program.

Uploaded by

abithakp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

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

This document contains C++ code for a menu-driven program that allows the user to select between bubble sort, selection sort, and insertion sort algorithms to sort an integer array. The program prompts the user to enter the array size and elements, selects a sorting method, calls the corresponding sorting function to sort the array, and prints the sorted array. It then asks the user if they want to continue sorting another array or end the program.

Uploaded by

abithakp
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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