0% found this document useful (0 votes)
53 views4 pages

Merge Sort

This C program implements quicksort to sort an array of integers. It takes in an unsorted array of integers from the user, calls a partition function that recursively divides the array into smaller sub-arrays and sorts them, and then displays the sorted array. Key functions include getdata to input values, display to output the array, sort to sort partitions, and partition to recursively divide the array for sorting.

Uploaded by

Sumit Agarwal
Copyright
© Attribution Non-Commercial (BY-NC)
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)
53 views4 pages

Merge Sort

This C program implements quicksort to sort an array of integers. It takes in an unsorted array of integers from the user, calls a partition function that recursively divides the array into smaller sub-arrays and sorts them, and then displays the sorted array. Key functions include getdata to input values, display to output the array, sort to sort partitions, and partition to recursively divide the array for sorting.

Uploaded by

Sumit Agarwal
Copyright
© Attribution Non-Commercial (BY-NC)
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
You are on page 1/ 4

#include<stdio.

h>

void getdata(int arr[],int n)

int i;

printf("

enter the data:

");

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

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

void display(int arr[],int n)

int i;

printf("

");

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

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

getchar();
}

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

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

l=low;

i=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];

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

void main()

int arr[20];

int n;

printf("Enter number of data:");

scanf("%d",&n);

getdata(arr,n);

partition(arr,0,n-1);

display(arr,n);

getchar();

You might also like