Friday, December 4, 2009

Quick Sort in C by Kernighan Ritchie

void pArr(int v[],int left,int right)
{
int i;
for(i=left;i<=right;i++) printf("%d ",v[i]); printf("\n"); } void swap(int v[],int i,int j) { int tmp; tmp= v[i]; v[i] = v[j]; v[j] = tmp; } void qsort(int v[],int left,int right) { int i,last; pArr(v,left,right); if (left >= right)
return;
swap(v,left,(left+right)/2);
last = left;
for(i = left + 1;i<=right; i++)
if(v[i] < v[left])
swap(v,++last,i);
swap(v,left,last);
qsort(v,left,last - 1);
qsort(v,last + 1,right);
}

int main()
{

int a[8] = {1,5,8,11,3,4,2,3};
pArr(a,0,7);
qsort(a,0,7);
pArr(a,0,7);
}

No comments:

Blog Archive