notes08

# notes08 - CSE 2320 Notes 8: Sorting (Last updated 1/2/12...

This preview shows pages 1–5. Sign up to view the full content.

CSE 2320 Notes 8: Sorting (Last updated 1/2/12 12:26 PM) 8.A. Q UICKSORT Sedgewick 7.1-7.8 Concepts Idea: Take an unsorted (sub)array and partition into two subarrays such that y x z p q r x y y z Pivot Customarily, the last subarray element (subscript r) is used as the pivot value. After partitioning, each of the two subarrays, p . . . q – 1 and q + 1 . . . r, are sorted recursively. Subscript q is returned from P ARTITION (aside: some versions don’t place pivot in its final position). Like M ERGESORT , Q UICKSORT is a divide-and-conquer technique: M ERGESORT Q UICKSORT Divide Trivial P ARTITION Subproblems Sort Two Parts Sort Two Parts Combine M ERGE Trivial Bottom-up Yes No possible?

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 Version 1 (not in Sedgewick): P ARTITION (in Θ n ( ) time, see partition.c ) y x z Pivot A B * # 1 2 3 1 2 3 Already known to have x y Already known to have y < z Untouched y < *: Move B over * y: Move B over Move A over A and B can be at the the same position . . . Termination y A B # int newPartition(int arr[],int p,int r) // From CLRS, 2nd ed. { int x,i,j,temp; x=arr[r]; i=p-1; for (j=p;j<r;j++) if (arr[j]<=x) { i++; temp=arr[i]; arr[i]=arr[j]; arr[j]=temp; } temp=arr[i+1]; arr[i+1]=arr[r]; arr[r]=temp; return i+1; }
3 Example: AB 6 3 7 2 8 4 9 0 1 5 A 6 B 3 7 2 8 4 9 0 1 5 3 A 6 B 7 2 8 4 9 0 1 5 3 A 6 7 B 2 8 4 9 0 1 5 3 2 A 7 6 B 8 4 9 0 1 5 3 2 A 7 6 8 B 4 9 0 1 5 3 2 4 A 6 8 7 B 9 0 1 5 3 2 4 A 6 8 7 9 B 0 1 5 3 2 4 0 A 8 7 9 6 B 1 5 3 2 4 0 1 A 7 9 6 8 B 5 3 2 4 0 1 < 5 > 9 6 8 7 Version 2 (Sedgewick): Pointers move toward each other (also in Θ n ( ) time, see partitionRS.c ) y x z Pivot A B * # 1 2 3 1 2 3 Already known to have x y Already known to have y z Untouched # < y: Move A right y < *: Move B left a b c Swap # and * (unless A and B have collided) Termination y A B # *

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 int partition(Item *a,int ell,int r) { // From Sedgewick, but more complicated since pointers move // towards each other. // Elements before i are <= pivot. // Elements after j are >= pivot. int i = ell-1, j = r; Item v = a[r]; printf("Input\n"); dump(arr,ell,r); for (;;) { // Since pivot is the right end, this while has a sentinel. // Stops at any element >= pivot while (less(a[++i], v)) ; // Stops at any element <= pivot (but not the pivot) or at the left end while (less(v, a[--j])) if (j == ell) break; if (i >= j) break; // Don't need to swap exch(a[i], a[j]); } exch(a[i], a[r]); // Place pivot at final position for sort return i; } Examples: A 6 3 7 2 8 4 9 0 1 5 B Left positioned A 6 3 7 2 8 4 9 0 1 B 5 Right positioned A 1 3 7 2 8 4 9 0 6 B 5 After swap 1 A 3 7 2 8 4 9 0 6 B 5 Left continues 1 3 A 7 2 8 4 9 0 6 B 5 Left positioned 1 3 A 7 2 8 4 9 0 B 6 5 Right positioned 1 3 A 0 2 8 4 9 7 B 6 5 After swap 1 3 0 A 2 8 4 9 7 B 6 5 Left continues 1 3 0 2 A 8 4 9 7 B 6 5 Left positioned 1 3 0 2 A 8 4 9 B 7 6 5 Right continues
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 03/25/2012 for the course CSE 2320 taught by Professor Bobweems during the Spring '12 term at UT Arlington.

### Page1 / 12

notes08 - CSE 2320 Notes 8: Sorting (Last updated 1/2/12...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online