Code For Alls..!

get the code!!

Wednesday, 19 July 2017

Quick Sort

Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays. A large array is partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the partition is made and another array holds values greater than the pivot value.
Quick sort partitions an array and then calls itself recursively twice to sort the two resulting subarrays. This algorithm is quite efficient for large-sized data sets as its average and worst case complexity are of Ο(n2), where n is the number of items.
Following animated representation explains how to find the pivot value in an array.
Quick Sort Partition Animation
The pivot value divides the list into two parts. And recursively, we find the pivot for each sub-lists until all lists contains only one element.
C code:
#include <stdio.h>
#include <stdlib.h>
void quick(int *a,int s,int e)
{
if(s<e)
{
int p;
p=partion(a,s,e);
quick(a,s,p-1);
quick(a,p+1,e);
}
}
int partion(int *a,int s,int e)
{
int i,j,pi,p;
p=a[e];
pi=s;
for(i=s;i<e;i++)
{
if(a[i]<=p)
{
j=a[i];
a[i]=a[pi];
a[pi]=j;
pi++;
}
}
j=a[e];
a[e]=a[pi];
a[pi]=j;
return pi;
}
int main(int argc, char *argv[]) {
int *p,i,n;
scanf("%d",&n);
p=malloc(sizeof(int)*n);
for(i=0;i<n;i++)
scanf("%d",p+i);
quick(p,0,n-1);
for(i=0;i<n;i++)
printf("%d",*(p+i));
return 0;
}