By | 10.03.2017

# Bubble Selection Insertion Sort

Write a C Menu Driven Program for Bubble Selection Insertion Sort Algorithm using switch case.

In this program, first we ask from the user to enter how many elements he wants to sort i.e n? Then we take n elements from the user which he wants to sort and then display a menu to select any sorting method to sort an array.

## Bubble Sort :

Bubble Sort is an algorithm which is used to sort N elements that are given in a memory for eg: an Array with N number of elements. Bubble Sort compares all the element one by one and sort them based on their values.

It is called Bubble sort, because with each iteration the smaller element in the list bubbles up towards the first place, just like a water bubble rises up to the water surface.

Sorting takes place by stepping through all the data items one-by-one in pairs and comparing adjacent data items and swapping each pair that is out of order.

## Selection Sort :

Selection sorting is conceptually the most simplest sorting algorithm. This algorithm first finds the smallest element in the array and exchanges it with the element in the first position, then find the second smallest element and exchange it with the element in the second position, and continues in this way until the entire array is sorted.

## Insertion Sort :

It is a simple Sorting algorithm which sorts the array by shifting elements one by one. Following are some of the important characteristics of Insertion Sort.

1. It has one of the simplest implementation
2. It is efficient for smaller data sets, but very inefficient for larger lists.
3. Insertion Sort is adaptive, that means it reduces its total number of steps if given a partially sorted list, hence it increases its efficiency.
4. It is better than Selection Sort and Bubble Sort algorithms.
5. Its space complexity is less, like Bubble Sorting, inerstion sort also requires a single additional memory space.
6. It is Stable, as it does not change the relative order of elements with equal keys

Here is source code of the C Menu Driven Program for Bubble Selection Insertion Sort . The C program is successfully compiled and run on a Windows system. The program output is also shown below.

### SOURCE CODE : :

```/*  C Menu Driven Program for Bubble Selection Insertion Sort  */

#include<stdio.h>
#include<stdlib.h>

void display(int a[],int n);
void bubble_sort(int a[],int n);
void selection_sort(int a[],int n);
void insertion_sort(int a[],int n);

//-----------------Main Function----------------------

int main()
{
int n,choice,i;
char ch[20];
printf("Enter no. of elements u want to sort : ");
scanf("%d",&n);
int arr[n];
for(i=0;i<n;i++)
{
printf("Enter %d Element : ",i+1);
scanf("%d",&arr[i]);
}
printf("Please select any option Given Below for Sorting : \n");

while(1)
{

printf("\n1. Bubble Sort\n2. Selection Sort\n3. Insertion Sort\n4. Display Array.\n5. Exit the Program.\n");
scanf("%d",&choice);

switch(choice)
{
case 1:
bubble_sort(arr,n);
break;
case 2:
selection_sort(arr,n);
break;
case 3:
insertion_sort(arr,n);
break;
case 4:

display(arr,n);
break;

case 5:
return 0;
default:
printf("\nPlease Select only 1-5 option ----\n");
}
}
return 0;
}

//-----------End of main function---------------------

//-------------------Display Function-----------------

void display(int arr[],int n)
{
for(int i=0;i<n;i++)
{
printf(" %d ",arr[i]);
}

}

//---------------------Bubble Sort Function-----------

void bubble_sort(int arr[],int n)
{
int i,j,temp;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("After Bubble sort Elements are : ");
display(arr,n);
}

//------------------Selection Sort Function---------

void selection_sort(int arr[],int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}

}
printf("After Selection sort Elements are : ");
display(arr,n);
}

//---------------Insertion Sort Function-------------------

void insertion_sort(int arr[],int n)
{
int i,j,min;
for(i=1;i<n;i++)
{
min=arr[i];
j=i-1;
while(min<arr[j] && j>=0)
{
arr[j+1]=arr[j];
j=j-1;
}
arr[j+1]=min;
}
printf("After Insertion sort Elements are : ");
display(arr,n);
}```

### OUTPUT : :

```/*  C Menu Driven Program for Bubble Selection Insertion Sort  */

Enter no. of elements u want to sort : 6
Enter 1 Element : 7
Enter 2 Element : 2
Enter 3 Element : 9
Enter 4 Element : 0
Enter 5 Element : 1
Enter 6 Element : 5
Please select any option Given Below for Sorting :

1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Display Array.
5. Exit the Program.

7  2  9  0  1  5
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Display Array.
5. Exit the Program.

After Insertion sort Elements are :  0  1  2  5  7  9
1. Bubble Sort
2. Selection Sort
3. Insertion Sort
4. Display Array.
5. Exit the Program.

Process returned 0```

Above is the source code for C Menu Driven Program for Bubble Selection Insertion Sort which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.

Article Rating
Category: C Programming Sorting Programs Tags:

My name is Tunde Ajetomobi, a Tech Enthusiast and Growth Hacker. I enjoy creating helpful content that solves problem across different topics. Codezclub is my way of helping young aspiring programmers and students to hone their skills and find solutions on fundamental programming languages.

Subscribe
Notify of