Insertion Sort and Bubble Sort Menu Driven Program
Insertion Sort and Bubble Sort Menu Driven Program
2.’Bubble sort’ is the simplest sorting technique among all the sorting techniques known. This
technique is simple in the sense that it is easy to understand, easy to implement and easy to
analyze.
3. The ‘Selection Sort’ requires that all keys under sorting be available prior to the execution.
Further, the selection sort considers the following two basic operations.
i)Select: selection of an item from the input list.
ii)Swap: Interchange two items in the list. In selection sorting the above two steps are iterated
to produce the sorted list, which progresses from one end as the iteration continues. The
selection sort can be done on the input list itself. This implies that selection sort is an in place
sorting technique.
Algorithm:
4. Bubble sort
Function: bubble(). This function sorts a given integer array in ascending order
Returns: This function does not return anything.The sorted array is printed after sorting.
5. Selection sort
Function: selection(). This function sorts a given integer array in ascending order
6.Insertion sort
Function: insertion(). This function sorts a given integer array in ascending order
using insertion sort algorithm.
Returns: This function does not return anything.The sorted array is printed after sorting.
Program Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int count=0;
int choice=0,ch=0;
int check=0;
int i=0;
int list[count];
for(i=0;i<count;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&list[i]);
}
do{
printf("Menu:\n\n");
printf("1.Bubble sort\n2.Selection Sort\n3.Inserton sort\n4.Exit\nYour choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
bubble(list,count);
break;
case 2:
selection(list,count);
break;
case 3:
insertion(list,count);
break;
case 4: return 0;
}
printf("Do you want to continue(press 1 to continue any other number to exit): ");
scanf("%d",&ch);
} while(ch==1);
return 0;
}
for(i=0;i<n;i++)
{
for (j=0;j<n-i-1;j++)
{
if (list[j] > list[j+1])
{
c=list[j];
list[j]=list[j+1];
list[j+1]=c;
}
}
}
printf("\nSorted list in ascending order:\n");
for ( i = 0 ; i < n ; i++ )
printf("%d,",list[i]);
printf("\n");
}
int i;
int j,min;
int k;
for(j=0;j<n-1;j++)
{
min=list[j];
k=j;
for(i=j+1;i<n;i++)
{
if(list[i]<min)
{
min=list[i];
k=i;
}
list[k]=list[j];
list[j]=min;
for(i=0;i<n;i++)
{
printf("%d,",list[i]);
}
printf("\n");
for(i=1;i<n;i++)
{
temp=list[i];
j=i-1;
while((j>=0)&&(list[j]>temp))
{
list[j+1]=list[j];
j--;
}
list[j+1]=temp;
}
printf("\n");
}
Program Output
1.Bubble sort
2.Selection Sort
3.Inserton sort
4.Exit
Your choice: 1
1.Bubble sort
2.Selection Sort
3.Inserton sort
4.Exit
Your choice: 2
Sorted list is:12,14,16,17,19,
Do you want to continue(press 1 to continue any other number to exit): 1
Menu:
1.Bubble sort
2.Selection Sort
3.Inserton sort
4.Exit
Your choice: 3
Sorted list is: 12,14,16,17,19,
Do you want to continue(press 1 to continue any other number to exit):6
Discussion:
This program demonstrates the implementation of various sorting algorithms for sorting integer
arrays.
The program takes an integer array from the user as input. The user can enter the numbers in any
random order.After taking the inputs, the user is given a menu control, and any of the above
mentioned sorting algorithms(i.e, bubble sort, selection sort,insertion sort)can be used to sort the
given list.
Sorting algorithms have different time and space complexities, and depending upon the requirement,
the appropriate sorting algorithm is used in a particular situation.
The purpose of this program was just to demonstrate how these different sorting algorithms can be
implemented in C language.