0% found this document useful (0 votes)
69 views11 pages

Insertion Sort and Bubble Sort Menu Driven Program

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views11 pages

Insertion Sort and Bubble Sort Menu Driven Program

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Assignment No: 2 Exp No: 2

Date: 20.04.22 Signature:

Problem statement:Write a program to sort a list of elements


.Give user the option to perform sorting using Insertion
sort,bubble sort or selection sort.
Statement:1. ‘INSERTION SORT’ is the simplest sorting method. Suppose there are N keys
in an input list then it requires N iterations to sort the entire list. In this case, an additional
storage space other than the storage space for the input list is required to store the output list.

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:

Input:An unsorted array[6,2,4,1……….,n]

Output: A sorted array[1,2,3, 4,5….n](in ascending order)


Process:

1. Declare Function prototypes


Void bubble(int*int)
Void selection(int*int)
Void insertion(int*int)

2.Create the Function int main()


2.1. int count=0; //size of array
2.2 int choice=0,ch=0; //variables used to store user choice
2.3 int check=0; //used to check status
2.4 int i=0; //loop variable
2.5 creating array of appropriate size
2.6 filling in the array

3.creates menu driven program for the user

A.No 1.for bubble sort operation


B.No 2.for selection sort operation
C. No3for insertion sort operation
D.No 4. For Exit

4. Bubble sort

Function: bubble(). This function sorts a given integer array in ascending order

using bubble sort algorithm.

Input: 1)Integer array

2)size of the array

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

using selection sort algorithm.

Input: 1)Integer array

2)size of the array


Returns: This function does not return anything.The sorted array is printed
after sorting.

6.Insertion sort

Function: insertion(). This function sorts a given integer array in ascending order
using insertion sort algorithm.

Input: 1)Integer array


2)size of the array

Returns: This function does not return anything.The sorted array is printed after sorting.

Program Code:
#include<stdio.h>
#include<stdlib.h>

void bubble(int *,int);


void selection(int *,int);
void insertion(int *,int);

int main()
{
int count=0;
int choice=0,ch=0;
int check=0;
int i=0;

printf("Enter the size of the list: ");


scanf("%d",&count);

int list[count];

for(i=0;i<count;i++)
{
printf("Enter element %d: ",i+1);
scanf("%d",&list[i]);
}

printf("\nNumbers entered: ");


for(i=0;i<count;i++)
printf("%d,",list[i]);
printf("\n");

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;

default: printf("Invalid option\nRetry: ");


break;

}
printf("Do you want to continue(press 1 to continue any other number to exit): ");
scanf("%d",&ch);
} while(ch==1);

return 0;
}

void bubble(int *list,int n)


{
int i,j;
int c;

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");
}

void selection(int *list,int 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;

printf("Sorted list is:");

for(i=0;i<n;i++)
{
printf("%d,",list[i]);
}

printf("\n");

void insertion(int *list,int n)


{
int temp;
int i=0,j=0;

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("Sorted list is: ");


for(i=0;i<n;i++)
{
printf("%d,",list[i]);
}

printf("\n");
}

Program Output

Enter the size of the list: 5


Enter element 1: 17
Enter element 2: 16
Enter element 3: 19
Enter element 4: 14
Enter element 5: 12

Numbers entered: 17,16,19,14,12,


Menu:

1.Bubble sort
2.Selection Sort
3.Inserton sort
4.Exit
Your choice: 1

Sorted list in ascending order:


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: 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.

You might also like