424,985 Members | 1,777 Online Need help? Post your question and get tips & solutions from a community of 424,985 IT Pros & Developers. It's quick & easy.

quick sort

 P: n/a Hi folks, this is the program implementing quicksort #include #include #define swap(a,b){int t; t=a;a=b;b=t;} int partition(int[],int,int); void quicksort(int[],int,int); int main(void) { int a,i; for(i=0;i<5;a[i] = (rand()%100)+1,i++); puts("unsorted array:: "); for(i=0;i<5;printf("\t%d",a[i]),i++); quicksort(a,0,5); puts("sorted array:: "); for(i=0;i<5;printf("\t%d",a[i]),i++); return(EXIT_SUCCESS); } void quicksort(int a,int p,int q) { int j; if(p < q) { j = partition(a,p,q+1); quicksort(a,p,j-1); //quicksort(a,j+1,q); } return; } int partition(int a,int m,int p) { int v = a[m], i = m, j = p; do { do{ i += 1;}while(a[i] <= v && i <= p ); do{j -= 1;}while(a[j] >= v && j >= m); if(i < j) swap(a[i],a[j]); }while(i <= j && i <= p); a[m] = a[j]; a[j] = v; return j; } my questions are as follows 1) this program works well on some compilers(djgpp,bloodshed devc++ 4.0) it is not working on turboc++ version 3.0, i am getting junk values how to rectify this??? 2) is the commented statement in the quicksort function( quicksort(a,j+1,q); ) redundant the partitioning is based on the following algorithm algorithm partition(a,m,p) /* within a[m],a[m+1].....a[p-1] the elements are re arranged in such a manner that if initially t = a[m],then after completion a[q]=t for some q between m and p-1,a[k] <= t for m <= k < q,and a[k] >= t for q < k < p,q is returned,set a[p] = infinity */ { v = a[m];i=m,j=p; repeat { repeat i = i+1; until(a[i] >= v); repeat j = j-1; until(a[j] <= v); if(i < j) swap(a[i],a[j]) }until(i >= j); a[m] = a[j];a[j]=v; return j; } Apr 11 '06 #1 