ECS110 Lecture Notes for Wednesday, November 8, 1995

Professor Rogaway conducting

Lecture 17

Scribe of the day: Thinh Q. Tran




Nov 6, continuation

QUICKSORT( A, l, r)     //A is an array
			//l and r are left and right index
{
	if (l < r) then
	q = PARTITION(A, l, r)
	QUICKSORT(A, l, q-1)
	QUCIKSORT(A, q, r)
}

illustration:
	________________________________________________
	| 4 | 2 | 5 | 1 | 6 | 8 |12 |11 | 10|15 |13 |14 |
	-------------------------------------------------
	  ^               ^   ^                       ^
	  |               |   |                       |
	  l             q-1   q                       r

	//all elements to the left of q are less 8.
	//all elements to the right of q are equal or greater than 8.

int PARTITION(A, l, r) {
	 x <- A[1]   //pivot on the first element

	 i <- l-1   //left pointer
	 j <- r+1   //right pointer
	 for(;;)
		  repeat j-- until  A[j] <= x
		  repeat i++ until A[i] => x
		  if (i < j)  A[i] <-> A[j]   //swap A[i] <-> A[j]
		  	else return  i
}

illustration:

    ------  ------  ------  ------  ------  ------  ------  ------  ------
    |333 |  |241 |  | 87 |  |720 |  |801 |  |600 |  |421 |  |722 |  |680 |
    ------  ------  ------  ------  ------  ------  ------  ------  ------
 ^    ^                                                               ^     ^
 |    |                                                               |     |
 i    l                   let x = 333 be pivot value.                 r      j

    ------  ------  ------  ------  ------  ------  ------  ------  ------
    |333 |  |241 |  | 87 |  |720 |  |801 |  |600 |  |421 |  |722 |  |680 |
    ------  ------  ------  ------  ------  ------  ------  ------  ------
     ^                                                 ^
     |                                                 |
     i                                                 j
     //i stops at 333 because it meets the condition
     //j conitinues to move to the left because it has not visited
     // any element that is less or equal to 333
    ------  ------  ------  ------  ------  ------  ------  ------  ------
    |333 |  |241 |  | 87 |  |720 |  |801 |  |600 |  |421 |  |722 |  |680 |
    ------  ------  ------  ------  ------  ------  ------  ------  ------
     ^                 ^
     |                 |      //j stops at 87 because it is less 333
     i                 j

    ------  ------  ------  ------  ------  ------  ------  ------  ------
    | 87 |  |241 |  |333 |  |720 |  |801 |  |600 |  |421 |  |722 |  |680 |
    ------  ------  ------  ------  ------  ------  ------  ------  ------
      ^               ^
      |               |      //after swap A[i] <-> A[j] or 333 and 87
      i               j      //and continue

    ------  ------  ------  ------  ------  ------  ------  ------  ------
    | 87 |  |241 |  |333 |  |720 |  |801 |  |600 |  |421 |  |722 |  |680 |
    ------  ------  ------  ------  ------  ------  ------  ------  ------
		      ^ ^
		      | |   //j remains the same place because 241 < 333
		      i j   //i moves to right and stops at 333,
		            // because it meets its condition A[i] <= x.
		           //since i = j,  q = return (i)

As the result, we have
    ------  ------  ------  ------  ------  ------  ------  ------  ------
    | 87 |  |241 |  |333 |  |720 |  |801 |  |600 |  |421 |  |722 |  |680 |
    ------  ------  ------  ------  ------  ------  ------  ------  ------
      ^       ^       ^                                                ^
      |       |       |                                                |
      l      q-1      q                                                r
    |__    x <  __| |_____               >= x                         ____|  

    //all elements to the left of q are less than x or 333
    //all elements to the right of q are equal or greater than x or 333

another example:                
    ------  ------  ------  ------  ------  ------  ------
    |600 |  |720 |  | 801|  |333 |  |421 |  |722 |  |680 |
    ------  ------  ------  ------  ------  ------  ------
 ^    ^                                                      ^
 |    |                                                      |
 i    x                   let x or 600 be pivot value.       j

    ------  ------  ------  ------  ------  ------  ------
    |600 |  |720 |  | 801|  |333 |  |421 |  |722 |  |680 |
    ------  ------  ------  ------  ------  ------  ------
      ^                               ^
      |                               |
      i                               j
 //i stops at 600 because 600 = x.
 //j stops at 421 because 421 < 600.
    //swap A[i] <-> A[j]  since i x.
 //j stops at 333 because 333 < 600.
  
 //since j < i, swap A[i] <-> A[j] and continue the process
    ------  ------  ------  ------  ------  ------  ------
    |421 |  |333 |  | 801|  |720 |  |600 |  |722 |  |680 |
    ------  ------  ------  ------  ------  ------  ------
	      ^        ^
	      |        |
	      i        j
     |__   x<  __|  |___________       => x      _________|
    //since i !> j, return (j)

 Analysis:
    worst case :   O(n^2)
    (n-1) steps  -->  O(nlgn)   best case
    (n-2) steps  -->  O(nlgn)  average case

    randomize pivot:
    run O(nlgn): 1. if the array is initially in random order, pivot is 
		    first elment;  Quicksort is E[T] = O(nlgn)

		 2. for any distribution on inputs with random pivot,
		   Quicksort runs in  E[T(n)] = O(nlgn), where T(n)
		   is coin flips of program.
   space requirement:
     _ n
     _ lg n if implement the smaller size first and save the bigger one on 
	 the stack


HEAPSORT: ADT Heap //DATA - a set of elements; those elements are drawn from a total order //OPERATIONS Heap makeHeap() //return an initially empty Heap (set if empty) void insert(int n) //insert element (int) n into Heap int deletemax() //delete the max elment from the set, returning it time: //Heap makeHeap() - O(1) //void insert(int n) - lg n //int deletemax() - lg n _to sort x1, x2,........,xn h <- makeheap() h.insert(x1) h.insert(x2) . . h.insert(xn) A[n] = h.deletemax() A[n-1] = h.deletemax() . . A[0] = h.deletemax() Def.: A rooted tree is heap ordered if every node has a key which is >= its children (equivalently, descendants) example: 51 / | \ 33 17 11 / \ | \ 25 6 8 10 / \ 4 1 Binary heap - a heap ordered complete binary tree 16 / \ 14 10 / \ / \ 8 7 9 3 / \ | 2 4 1 illustration of deletemax() _take out max, 16 _replace it with the far most right child, which is 1 1 / \ 14 10 / \ / \ 8 7 9 3 / \ 2 4 _compare 1 with highest child and swap them if its child is > it. (repeat until child is smaller than 1) ( 14 > 1) (8 > 1) 14 14 / \ / \ 1 10 8 10 / \ / \ / \ / \ 8 7 9 3 1 7 9 3 / \ / \ 2 4 2 4 (4 > 1) 14 / \ 8 10 / \ / \ 4 7 9 3 / \ 2 1 _after shift 1 down to it proper place, take out the max elment, 14 and replace it with 1 again 1 10 / \ / \ 8 10 8 1 / \ / \ / \ / \ 4 7 9 3 1 7 9 3 / / \ 2 2 4 10 / \ 8 9 / \ / \ 4 7 1 3 / 2 _repeat to process to take out the 10 1 9 / \ / \ 8 9 8 1 / \ \ / \ \ 4 7 3 1 7 3 / / \ 2 2 4 9 / \ 8 3 / \ \ 4 7 1 / 2 If this process is continued until the tree has nothing in it you will end up with a sorted list.