0% found this document useful (0 votes)
31 views

Sandhya.G: DATE:2.4.11 EXP - No: Aim

The document describes experiments on sorting algorithms. It contains code to implement insertion sort, merge sort, and quick sort on sets of random numbers. For each algorithm, it provides the code, inputs test data of 10 numbers, runs the sorting algorithm, and displays the sorted output. It concludes that the required results were obtained for all three sorting algorithms.

Uploaded by

Sandhya Giri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Sandhya.G: DATE:2.4.11 EXP - No: Aim

The document describes experiments on sorting algorithms. It contains code to implement insertion sort, merge sort, and quick sort on sets of random numbers. For each algorithm, it provides the code, inputs test data of 10 numbers, runs the sorting algorithm, and displays the sorted output. It concludes that the required results were obtained for all three sorting algorithms.

Uploaded by

Sandhya Giri
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Sandhya.

G 201010302
2

DATE:2.4.11

EXP.No:

AIM:

To perform insertion sort on a set of elements and dispaly the


result.

PROGRAM CODE:

#include<stdio.h>

#include<conio.h>

void main()

clrscr();

int a[20],n,i,j,k,temp;

printf("\n enter the number of elements:");

scanf("%d",&n);

printf("\n enter %d eleements for sorting:",n);

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

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

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

{
1
Sandhya.G 201010302
2

temp=a[k];

j=k-1;

while(temp<a[j]&&j>=0)

a[j+1]=a[j];

j--;

a[j+1]=temp;

printf("\n elements sorted using insertion sort:");

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

printf("%4d",a[i]);

getch();

RESULT:

Thus the rquired output was obtained.

2
Sandhya.G 201010302
2

DATE:2.4.11

EXP>NO:

AIM:

To perform merge sort on a list of 10 numbers and diaplay the


result.

PROGRAM CODE:

#include<stdio.h>

#include<conio.h>

void getdata(int arr[],int n)

{ int i;

printf("\n enter the data:");

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

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

void display(int arr[],int n)

{ int i;

printf("\n elements sorted using merge sort:");

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

{ printf("%4d",arr[i]);
3
Sandhya.G 201010302
2

void sort(int arr[],int low,int mid,int high)

{ int i,j,k,l,b[20];

i=low;l=low;

j=mid+1;

while((l<=mid)&& (j<=high))

{ if(arr[l]<=arr[j])

{ b[i]=arr[l];

l++;

else

{ b[i]=arr[j];

j++;

i++;

if(l>mid)

{ for(k=j;k<=high;k++)

{ b[i]=arr[k];

4
Sandhya.G 201010302
2

i++;

else

{ for(k=l;k<=mid;k++)

{ b[i]=arr[k];

i++;

} }

for(k=low;k<=high;k++)

{ arr[k]=b[k];

void partition(int arr[],int low,int high)

{ int mid;

if(low<high)

{ mid=(low+high)/2;

partition(arr,low,mid);

partition(arr,mid+1,high);

sort(arr,low,mid,high);

5
Sandhya.G 201010302
2

void main()

{ clrscr();

int arr[20],n;

printf("\n enter the number of elements:");

scanf("%d",&n);

getdata(arr,n);

partition(arr,0,n-1);

display(arr,n);

getch();

RESULT:

Thus required result was obtained.

6
Sandhya.G 201010302
2

DATE:2.4.11

EXP.NO:

AIM:

To perform quick sort on agiven set of numbers and dispaly the


result.

PROGRAM RESULT:

#include<stdio.h>

#include<conio.h>

void getdata(int arr[],int n)

printf("\n enter data:");

int i;

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

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

}
7
Sandhya.G 201010302
2

void display(int arr[],int n)

int i;

printf("\n elements sorted using quick sort are:");

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

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

void quicksort(int arr[],int l,int h)

int low=l,high=h,temp;

int key=arr[(low+high)/2];

do

while(key>arr[low])

low++;

while(key<arr[high])

high--;

if(low<=high)

8
Sandhya.G 201010302
2

temp=arr[low];

arr[low++]=arr[high];

arr[high--]=temp;

}while(low<=high);

if(l<high)

quicksort(arr,l,high);

if(low<h)

quicksort(arr,low,h);

void main()

clrscr();

int arr[20],n;

printf("\n enter the number of elemnts:");

scanf("%d",&n);

getdata(arr,n);

quicksort(arr,0,n-1);

9
Sandhya.G 201010302
2

display(arr,n);

getch();

RESULT:

Thus required result was obtained.

10

You might also like