Assignment No 10

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

Assignment no 10

Design and implement a menu driven program for implementing insertion sort and
quick sort for population of a town.

#include<stdio.h>

#include<conio.h>

int arr[100],n;

void InsertSort();

void QuickSort(int,int);

int partition(int,int);

void swap(int,int);

int main()

int choice;

int i,temp;

printf("Year Population\n");

printf("2001 20\n2002 36\n2003 12\n2004 50\n");

printf("Enter the number of Years:-\n");

scanf("%d",&n);

printf("Enter %d Years Population:-\n",n);

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

scanf("%d",&arr[i]);

printf("Select your choice:-\n");

printf("1. Insertion Sort\n");

printf("2. Quicksort\n");

printf("Select your choice:-\n");

x:scanf("%d",&choice);
switch(choice)

case 1:

InsertSort();

break;

case 2:

QuickSort(0,n-1);

break;

default:

printf("Enter 1,2 or 3.\n");

goto x;

printf("\n\nSorted Array:-\n");

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

printf("%d ",arr[i]);

return 0;

//Insert Sort

void InsertSort()

int temp,i,j,k;

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

temp=arr[i];

j=i-1;

while((j>=0) && (arr[j]>temp))

arr[j+1]=arr[j];
j=j-1;

arr[j+1]=temp;

printf("\nPass %d: ",i);

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

printf("%d ",arr[k]);

//Quick Sort

void QuickSort(int low, int high)

int m;

if(low<high)

m=partition(low,high);

QuickSort(low,m-1);

QuickSort(m+1,high);

int partition(int low, int high)

int pivot=arr[low];

int i=low;

int j=high;

while(i<=j)

while(arr[i]<=pivot)

i++;
while(arr[j]>pivot)

j--;

if(i<j)

swap(i,j);

swap(low,j);

return j;

void swap(int i,int j)

int temp;

int k;

temp = arr[i];

arr[i]=arr[j];

arr[j]=temp;

printf("\nPass: ");

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

printf("%d ",arr[k]);

Output :

You might also like