Write a program in C using switch case to sort 100 numbers entered by the user. Use switch case to ask user for algo to be used. Program must use Selection Sort, Merge Sort, Insertion Sort and Bubble Sort. Show your output for different choices
Answers
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.
It has one of the simplest implementation
It is efficient for smaller data sets, but very inefficient for larger lists.
Insertion Sort is adaptive, that means it reduces its total number of steps if given a partially sorted list, hence it increases its efficiency.
It is better than Selection Sort and Bubble Sort algorithms.
Its space complexity is less, like Bubble Sorting, inerstion sort also requires a single additional memory space.
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
/* 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);