1 / 14

CS2223 Recitation Pointer sort -- an Adaptation of Quick Sort

CS2223 Recitation Pointer sort -- an Adaptation of Quick Sort. by Song Wang. Motivation. Be familiar with the quick sort algorithm Indexed data sorting Calling stack minimization for recursive function call. Problem Definition.

cguse
Download Presentation

CS2223 Recitation Pointer sort -- an Adaptation of Quick Sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS2223 RecitationPointer sort--an Adaptation of Quick Sort by Song Wang

  2. Motivation • Be familiar with the quick sort algorithm • Indexed data sorting • Calling stack minimization for recursive function call

  3. Problem Definition • Given an array has a key field and data field, return the array sorted by the key value. • Eg. Note: Key value is unique in the whole column

  4. Pointer Sort (or Index Sort) • Instead of moving large records, we build an array of pointers (an index) to do the swap. • Eg. • Frequently used for huge table sorting, e.g. for relational database table. Sorted Initial

  5. Code--by Gonnet and Baeza-Yates #include <sys/types.h> #include <sys/wait.h> #include <limits.h> #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <sys/resource.h> #define TIMEBASE 699940000.0 struct timeval tp; struct timezone tzp; #define N 100 double sec1, sec2; int depth = 0, m=5, ip, ip1;

  6. Code (Cond.)--by Gonnet and Baeza-Yates main() { int *r, *pr, b=0, e=N-1, s,t, i; //prepare array if ( (r=(int *)malloc( 4*(N+1))) == NULL) {printf("allocation failed\n"); exit(2); } if ( (pr=(int *)malloc( 4*(N+1))) == NULL) {printf("allocation failed\n"); exit(2); } srandom(2934); for (s=0;s<N;s++) {pr[s]=s; r[s] = random()%1000;} Size of int Initialize all pointers Each key value is [0,1000) Easy to read the printouts

  7. Code (Cond.)--by Gonnet and Baeza-Yates //calling quick sort pointer function gettimeofday(&tp, &tzp); sec1 = tp.tv_sec + tp.tv_usec/1000000.0; qsp(r, pr, &b, &e); gettimeofday(&tp, &tzp); sec2 = tp.tv_sec + tp.tv_usec/1000000.0; printf("qs time %f\n", sec2-sec1); //calling insertion sort pinsort(r,pr, N); gettimeofday(&tp, &tzp); sec1 = tp.tv_sec + tp.tv_usec/1000000.0; printf("insort time %f\n", sec1-sec2); Begin index &end index

  8. Code (Cond.)--by Gonnet and Baeza-Yates //checking sorted array for (i=0; i<N-1;i++) if (r[pr[i]] > r[pr[i+1]]) printf("out of order at %d\n",i); } Note: Checking the order is of course only needed until the code is accepted as correct,

  9. Code (Cond.)--by Gonnet and Baeza-Yates threshhold int qsp(int *r, int *pr, int *b, int *e){ int i ,j ,p, pb, t, ab, ae; while ( (*e)- (*b)>m ) { //find the pivot i = *b; j = *e; pb = pr[(*b)]; p = r[pr[(*b)]]; while (i<j) {while (r[pr[j]] > p) j--; pr[i] = pr[j]; while ( (i<j) && (r[pr[i]] <= p) ) i++; pr[j] = pr[i]; } pr[i] = pb; pb,p used for swap later Pivot at i

  10. Code (Cond.)--by Gonnet and Baeza-Yates //recursive call qsp() for both portions if (i-*b < *e -i) { ae = i-1; qsp(r,pr,b,&ae); *b = i+1;} else { ab = i+1; qsp(r,pr,&ab,e); *e = i-1;} }//for while (i<j) }//for qsp Discussed later: Call stack minimization

  11. Code (Cond.)--by Gonnet and Baeza-Yates // perform once a straight insertion sort pinsort(int *a, int *pr, int n) { int i,j, tj; int t; if (n <= 1) return; for (j=1; j<n; j++) { tj = pr[j]; t = a[pr[j]]; for (i=j-1; i>=0; i--) if(t < a[pr[i]]) pr[i+1] = pr[i]; else break; pr[i+1] = tj; } }

  12. A Running Example r N=9 0 1 2 3 4 5 6 7 8 pr Initialize qsp(r, pr, 0, 8); j i pb = 0; p =13 j i

  13. A Running Example (Cond.) r N=9 0 1 2 3 4 5 6 7 8 j i j i i Recursive call Recursive call

  14. Call Stack Minimization if (i-*b < *e -i) { ae = i-1; qsp(r,pr,b,&ae); *b = i+1;} else { ab = i+1; qsp(r,pr,&ab,e); *e = i-1;} • First recursively qsp() shorter partition. • Why? • The worst case: the pivot is always the biggest element • Call left partition first will need O(n) call stack depth

More Related