0% found this document useful (0 votes)
56 views3 pages

Insertion and Selection Sort Program

This C program contains functions for insertion sort and selection sort. The main function displays a menu allowing the user to choose between the two sorting algorithms or exit. Based on the selection, it calls the corresponding sorting function which accepts user input for the number of elements and array, performs the sort, and displays the sorted output. Both sorting functions clear the screen, get the array as input, implement the respective algorithms to sort in ascending order, and print the results.

Uploaded by

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

Insertion and Selection Sort Program

This C program contains functions for insertion sort and selection sort. The main function displays a menu allowing the user to choose between the two sorting algorithms or exit. Based on the selection, it calls the corresponding sorting function which accepts user input for the number of elements and array, performs the sort, and displays the sorted output. Both sorting functions clear the screen, get the array as input, implement the respective algorithms to sort in ascending order, and print the results.

Uploaded by

Gaurav Wani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include<stdio.

h>
#include<conio.h>

//024-Mali Pankaj Arun


void insertionsort();
void selectionsort();

void main()
{

int choice;
clrscr();
do
{
printf("\n\t\tProgram for Insertion and
Selection Sort \n");
printf("\n\tMain Menu: \n1.Insertion Sort\
n2.Selection Sort\n3.Exit");
printf("\nSelect menu : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
insertionsort();
break;
case 2:
selectionsort();
break;
case 3:
printf("\nExiting the program");
break;

default: printf("Invalid menu item


selected.");
}
}while(choice != 3);
}

void insertionsort()
{
int n, array[100], i, k, temp,ptr;
clrscr();

printf("Enter number of elements for Insertion Sort\


n");
scanf("%d", &n);

printf("Enter %d integers\n", n);

for (i = 0; i < n; i++)


{
scanf("%d", &array[i]);
}
for (i = 1 ; i <= n - 1; i++)
{

temp = array[i];
ptr = i-1;

while(temp < array[ptr] && ptr >=0)


{
array[ptr+1]= array[ptr];
ptr= ptr-1;
}
array[ptr+1]= temp;
}

printf("Sorted list in ascending order by insertion


sort:\n");

for (i = 0; i <= n - 1; i++)


{
printf("%d\n", array[i]);
}

getch();

void selectionsort()
{
int array[100],i,j,k,n,smallest,position,temp;
clrscr();

printf("Enter number of elements for selection sort\


n");
scanf("%d", &n);

printf("Enter %d integers\n", n);

for (i = 0; i < n; i++)


{
scanf("%d", &array[i]);
}

for (i = 0 ; i <(n - 1); i++) //It is for n-1 passes


{
smallest = array[i];
position = i;

for(j=(i+1); j<n; j++)


{
if(array[j] < smallest)
{
smallest = array[j];
position = j;
}
}
printf("\n Smallest = %d position = %d",smallest,
position);
getch();

if(smallest == array[i])
{
printf("\n");
}
else
{
temp = array[i];
array[i] = smallest;
array[position]= temp;
}
printf("\n");
for(k=0;k<n;k++)
{
printf("\t%d",array[k]);
}
}
printf("\n\n Sorted Elements by selection sort:");
for(i=0;i<n;i++)
{
printf("\t%d",array[i]);
}

getch();

You might also like