68
Trace of QuickSort Algorithm

Trace of QuickSort Algorithm

Embed Size (px)

DESCRIPTION

Trace of QuickSort Algorithm. quickSort(array, lower, upper) { // Base Case if (lower >= upper) { we’re done } else { partition array around pivot value array[lower] pos contains the new location of pivot value - PowerPoint PPT Presentation

Citation preview

Page 1: Trace of QuickSort Algorithm

Trace of QuickSort Algorithm

Page 2: Trace of QuickSort Algorithm

quickSort(array, lower, upper){ // Base Case if (lower >= upper) { we’re done } else { partition array around pivot value array[lower] pos contains the new location of pivot value quickSort array up to pos: quickSort(array,lower,pos) quickSort array after pos: quickSort(array,pos+1,upper) }}

Page 3: Trace of QuickSort Algorithm

partition(array, lower, upper){ pivot is array[lower] while (true) { scan from right to left using index called RIGHT STOP when locate an element that should be left of pivot

scan from left to right using index called LEFT stop when locate an element that should be right of pivot

swap array[RIGHT] and array[LEFT]

if (RIGHT and LEFT cross) pos = location where LEFT/RIGHT cross swap pivot and array[pos] all values left of pivot are <= pivot all values right of pivot are >= pivot return pos end pos }}

Page 4: Trace of QuickSort Algorithm

6 5 9 12 3 4

0 1 2 3 4 5

quickSort(arr,0,5)

Page 5: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 9 12 3 4

0 1 2 3 4 5

partition(arr,0,5)

Page 6: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 9 12 3 4

0 1 2 3 4 5

partition(arr,0,5)

pivot= ?

Partition Initialization...

Page 7: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 9 12 3 4

0 1 2 3 4 5

partition(arr,0,5)

pivot=6

Partition Initialization...

Page 8: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 9 12 3 4

0 1 2 3 4 5

partition(arr,0,5)

leftpivot=6

Partition Initialization...

Page 9: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 9 12 3 4

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

Partition Initialization...

Page 10: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 9 12 3 4

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

right moves to the left untilvalue that should be to leftof pivot...

Page 11: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 9 12 3 4

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

Page 12: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 9 12 3 4

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

left moves to the right untilvalue that should be to rightof pivot...

Page 13: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 9 12 3 4

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

Page 14: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 9 12 3 4

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

Page 15: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 9 12 3 4

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

swap arr[left] and arr[right]

Page 16: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 12 3 9

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

repeat right/left scanUNTIL left & right cross

Page 17: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 12 3 9

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

right moves to the left untilvalue that should be to leftof pivot...

Page 18: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 12 3 9

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

Page 19: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 12 3 9

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

left moves to the right untilvalue that should be to rightof pivot...

Page 20: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 12 3 9

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

Page 21: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 12 3 9

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

swap arr[left] and arr[right]

Page 22: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 3 12 9

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

swap arr[left] and arr[right]

Page 23: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 3 12 9

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

repeat right/left scanUNTIL left & right cross

Page 24: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 3 12 9

0 1 2 3 4 5

partition(arr,0,5)

left rightpivot=6

right moves to the left untilvalue that should be to leftof pivot...

Page 25: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 3 12 9

0 1 2 3 4 5

partition(arr,0,5)

left

right

pivot=6

Page 26: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 3 12 9

0 1 2 3 4 5

partition(arr,0,5)

left

right

pivot=6

right & left CROSS!!!

Page 27: Trace of QuickSort Algorithm

quickSort(arr,0,5)

6 5 4 3 12 9

0 1 2 3 4 5

partition(arr,0,5)

left

right

pivot=6

right & left CROSS!!!1 - Swap pivot and arr[right]

Page 28: Trace of QuickSort Algorithm

quickSort(arr,0,5)

3 5 4 6 12 9

0 1 2 3 4 5

partition(arr,0,5)

left

right

pivot=6

right & left CROSS!!!1 - Swap pivot and arr[right]

Page 29: Trace of QuickSort Algorithm

quickSort(arr,0,5)

3 5 4 6 12 9

0 1 2 3 4 5

partition(arr,0,5)

left

right

pivot=6

right & left CROSS!!!1 - Swap pivot and arr[right]2 - Return new location of pivot to caller

return 3

Page 30: Trace of QuickSort Algorithm

quickSort(arr,0,5) 3 5 4 6 12 9

0 1 2 3 4 5

Recursive calls to quickSort()using partitioned array...

pivotposition

Page 31: Trace of QuickSort Algorithm

quickSort(arr,0,5)

3 5 4 6 12 9

0 1 2 3 4 5

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

Page 32: Trace of QuickSort Algorithm

quickSort(arr,0,5)

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

Page 33: Trace of QuickSort Algorithm

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

Partition Initialization...

quickSort(arr,0,5)

Page 34: Trace of QuickSort Algorithm

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

Partition Initialization...

quickSort(arr,0,5)

Page 35: Trace of QuickSort Algorithm

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

Partition Initialization...

left

quickSort(arr,0,5)

Page 36: Trace of QuickSort Algorithm

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

Partition Initialization...

left right

quickSort(arr,0,5)

Page 37: Trace of QuickSort Algorithm

right moves to the left untilvalue that should be to leftof pivot...

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

left right

quickSort(arr,0,5)

Page 38: Trace of QuickSort Algorithm

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

left right

quickSort(arr,0,5)

Page 39: Trace of QuickSort Algorithm

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

left right

quickSort(arr,0,5)

Page 40: Trace of QuickSort Algorithm

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

left

right

quickSort(arr,0,5)

Page 41: Trace of QuickSort Algorithm

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

left

right

right & left CROSS!!!

quickSort(arr,0,5)

Page 42: Trace of QuickSort Algorithm

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

left

right

right & left CROSS!!!1 - Swap pivot and arr[right]

quickSort(arr,0,5)

Page 43: Trace of QuickSort Algorithm

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

partition(arr,0,3)

left

right

right & left CROSS!!!1 - Swap pivot and arr[right]right & left CROSS!!!1 - Swap pivot and arr[right]2 - Return new location of pivot to caller

return 0

quickSort(arr,0,5)

Page 44: Trace of QuickSort Algorithm

quickSort(arr,0,3)

3 5 4 6

0 1 2 3

quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

Recursive calls to quickSort()using partitioned array...

Page 45: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3

Page 46: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3

Base case triggered...halting recursion.

Page 47: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3

Base Case: Return

Page 48: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3partition(arr,1,3)

Partition Initialization...

Page 49: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3partition(arr,1,3)

Partition Initialization...

Page 50: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3partition(arr,1,3)

Partition Initialization...left

Page 51: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3partition(arr,1,3)

left right

right moves to the left untilvalue that should be to leftof pivot...

Page 52: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3partition(arr,1,3)

left right

Page 53: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3partition(arr,1,3)

left right

left moves to the right untilvalue that should be to rightof pivot...

Page 54: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3partition(arr,1,3)

left

right

Page 55: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3partition(arr,1,3)

left

right

right & left CROSS!

Page 56: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

5 4 6

1 2 3partition(arr,1,3)

left

right

right & left CROSS!1- swap pivot and arr[right]

Page 57: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

4 5 6

1 2 3partition(arr,1,3)

left

right

right & left CROSS!1- swap pivot and arr[right]

Page 58: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

4 5 6

1 2 3partition(arr,1,3)

left

right

right & left CROSS!1- swap pivot and arr[right]2 – return new position of pivot

return 2

Page 59: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

quickSort(arr,1,2) quickSort(arr,3,3)

4 5 6

1 2 3

Page 60: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

quickSort(arr,1,2) quickSort(arr,3,3)

4 5 6

1 2 3

partition(arr,1,2)

Page 61: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

quickSort(arr,1,2) quickSort(arr,3,3)

4 5 6

1 2 3

Page 62: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

quickSort(arr,1,2) quickSort(arr,3,3)

6

3

quickSort(arr,1,1) quickSort(arr,2,2)

4 5

1 2

Page 63: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

12 9

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

quickSort(arr,1,2) quickSort(arr,3,3)

6

3

quickSort(arr,1,1) quickSort(arr,2,2)

4 5

1 2

partition(arr,4,5)

Page 64: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

9 12

4 5

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

quickSort(arr,1,2) quickSort(arr,3,3)

6

3

quickSort(arr,1,1) quickSort(arr,2,2)

4 5

1 2

partition(arr,4,5)

Page 65: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

quickSort(arr,1,2) quickSort(arr,3,3)

6

3

quickSort(arr,1,1) quickSort(arr,2,2)

4 5

1 2

quickSort(arr,4,5)

9 12

4 5

quickSort(arr,6,5)

Page 66: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

quickSort(arr,1,2) quickSort(arr,3,3)

6

3

quickSort(arr,1,1) quickSort(arr,2,2)

4 5

1 2

quickSort(arr,4,5)

9 12

4 5partition(arr,4,5)

quickSort(arr,6,5)

Page 67: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

quickSort(arr,1,2) quickSort(arr,3,3)

6

3

quickSort(arr,1,1) quickSort(arr,2,2)

4 5

1 2

quickSort(arr,4,5)

9 12

4 5

quickSort(arr,6,5)

Page 68: Trace of QuickSort Algorithm

quickSort(arr,0,3) quickSort(arr,4,5)

quickSort(arr,0,5)

quickSort(arr,0,0)

3

0

quickSort(arr,1,3)

quickSort(arr,1,2) quickSort(arr,3,3)

6

3

quickSort(arr,1,1) quickSort(arr,2,2)

4 5

1 2

quickSort(arr,4,5)

9

12

4

5

quickSort(arr,4,4)

quickSort(arr,5,5)

quickSort(arr,6,5)