0% found this document useful (0 votes)
10 views1 page

Include

The document contains C code that implements sorting algorithms, specifically Quick Sort and a merging function. It includes functions for swapping elements, partitioning the array, and dividing the array into smaller segments for sorting. The main function prompts the user for input, sorts the array, and prints the sorted results.

Uploaded by

MS PRODUCTION
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)
10 views1 page

Include

The document contains C code that implements sorting algorithms, specifically Quick Sort and a merging function. It includes functions for swapping elements, partitioning the array, and dividing the array into smaller segments for sorting. The main function prompts the user for input, sorts the array, and prints the sorted results.

Uploaded by

MS PRODUCTION
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
You are on page 1/ 1

#include <stdio.

h>
void swap(int *a, int *b) { z=0;
int temp = *a; for(int i=start;i<=end;i++)
*a = *b; a[i]=q[z++];
*b = temp; }
} void divide(int *a,int start,int end)
int partition(int arr[], int low, int high) { {
int pivot = arr[high]; if(start>=end)
int i = (low - 1); return;
for (int j = low; j < high; j++) { int x=start+(end-start)/3;
if (arr[j] <= pivot) { int y=(x+1+end)/2;
i++; divide(a,start,x);
swap(&arr[i], &arr[j]); divide(a,x+1,y);
} } divide(a,y+1,end);
swap(&arr[i + 1], &arr[high]); merge(a,start,x,y,end);
return i + 1; }
} int main()
void quickSort(int arr[], int low, int high) { {
if (low < high) { int n,a[100];
int pi = partition(arr, low, high); printf("Enter array size : ");
quickSort(arr, low, pi - 1); scanf("%d",&n);
quickSort(arr, pi + 1, high); printf("Enter array elements : ");
} } for(int i=0;i<n;i++)
int main() { scanf("%d",&a[i]);
int n; divide(a,0,n-1);
printf("Enter the number of elements: "); printf("Sorted array : ");
scanf("%d", &n); for(int i=0;i<n;i++)
int arr[n]; printf("%d ",a[i]);
printf("Enter the elements: "); return 0;
for (int i = 0; i < n; i++) { }
scanf("%d", &arr[i]);
}
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
void merge(int *a,int start,int x,int y,int
end)
{
int t[y-start+1],c=0,i=start,j=x+1;
while(i<=x&&j<=y)
{
if(a[i]<a[j])
t[c++]=a[i++];
else
t[c++]=a[j++];
}
while(i<=x)
t[c++]=a[i++];
while(j<=y)
t[c++]=a[j++];

int q[end-start+1],z=0; i=0,j=y+1;


while(i<c&&j<=end)
{
if(t[i]<a[j])
q[z++]=t[i++];
else
q[z++]=a[j++];
}
while(i<c)
q[z++]=t[i++];
while(j<=end)
q[z++]=a[j++];

You might also like