Saturday, August 9, 2014

Program for quick sort

Program to implement quick sort. If you need help, comment below

#include<stdio.h> int a[20],b[20],n,z; void quick(int x[],int first,int last) { int pivot; if(first<last) { pivot=partition(x,first,last); quick(x,first,pivot-1); quick(x,pivot+1,last); } } int partition(int x[],int first,int last) { int pivot,temp,i,j; pivot=first; i=first; j=last; while(i<j) { while((x[i]<=x[pivot])&&(i<last)) i++; while(x[j]>x[pivot]) j--; if(i<j) { temp=x[i]; x[i]=x[j]; x[j]=temp; } for(z=1;z<=n;z++) printf("%d\t ",a[z]); printf("\n"); } temp=x[pivot]; x[pivot]=x[j]; x[j]=temp; return j; } int main() { int i,j,c,ch; printf("\nQuicksort"); printf("\nEnter the no. of elements in the array:\t"); scanf("%d",&n); printf("\nEnter the elements into the array:\n"); for(i=1;i<=n;i++) scanf("%d",&a[i]); quick(a,1,n); printf("\nAfter sorting:\n"); for(i=1;i<=n;i++) printf("%d ",a[i]); }

No comments:

Post a Comment