Gags
Gags
Gags
a = 2(two subproblems)
n/b = n/2 (each subproblem has size approx n/2)
D(n) = (1) (just compute midpoint of array)
C(n) =n) (merging can be done by scanning sorted
subarrays)
• Idea:
– Take the array you would like to sort and divide
it in half to create 2 unsorted subarrays.
– Next, sort each of the 2 subarrays.
– Finally, merge the 2 sorted subarrays into 1
sorted array.
• Efficiency: O(nlog2n)
arrayC
indexC
12/07/21 03:35 PM http://www.nurul.com 24
Merge Sort
arrayA 1 13 24 26
2 < 13 so we insert
indexA arrayB[indexB] into
arrayC[indexC]
arrayB 2 15 27 38
indexB
arrayC
1
indexC
12/07/21 03:35 PM http://www.nurul.com 25
Merge Sort
arrayA 1 13 24 26
13 < 15 so we insert
indexA arrayA[indexA] into
arrayC[indexC]
arrayB 2 15 27 38
indexB
arrayC
1 2
indexC
12/07/21 03:35 PM http://www.nurul.com 26
Merge Sort
arrayA 1 13 24 26
15 < 24 so we insert
indexA arrayB[indexB] into
arrayC[indexC]
arrayB 2 15 27 38
indexB
arrayC
1 2 13
indexC
12/07/21 03:35 PM http://www.nurul.com 27
Merge Sort
arrayA 1 13 24 26
24 < 27 so we insert
indexA arrayA[indexA] into
arrayC[indexC]
arrayB 2 15 27 38
indexB
arrayC
1 2 13 15
indexC
12/07/21 03:35 PM http://www.nurul.com 28
Merge Sort
arrayA 1 13 24 26
26 < 27 so we insert
indexA arrayA[indexA] into
arrayC[indexC]
arrayB 2 15 27 38
indexB
arrayC
1 2 13 15 24
indexC
12/07/21 03:35 PM http://www.nurul.com 29
Merge Sort
arrayA 1 13 24 26
Since we have exhausted
one of the arrays, arrayA,
we simply copy the
arrayB 2 15 27 38 remaining items from the
other array, arrayB, into
indexB arrayC
1 2 13 15 24 26
arrayC
indexC
12/07/21 03:35 PM http://www.nurul.com 30
Merge Sort
arrayA 1 13 24 26
arrayB 2 15 27 38
arrayC
1 2 13 15 24 26 27 38
1 4 8 9 0 11 5 10 7 6
1 4 8 9 0 11 5 10 7 6
left right
• We want to move all the elements smaller than the
pivot to the left part of the array and all the
elements larger than the pivot to the right part.
1 4 8 9 0 11 5 10 7 6
left right
1 4 8 9 0 11 5 10 7 6
left right
1 4 8 9 0 11 5 10 7 6
left right
1 4 5 9 0 11 8 10 7 6
left right
Quick Sort
• The effect is to push a large element to the
right and a small element to the left.
• We then repeat the process until left and
right cross.
1 4 5 9 0 11 8 10 7 6
left right
1 4 5 0 9 11 8 10 7 6
left right
Quick Sort
1 4 5 0 9 11 8 10 7 6
left right
1 4 5 0 9 11 8 10 7 6
right left
1 4 5 0 6 11 8 10 7 9
right left
Quick Sort
• We now repeat the process using the sub-
arrays to the left and right of the pivot.
1 4 5 0 6 11 8 10 7 9
1 4 5 0 11 8 10 7 9
12/07/21 03:35 PM http://www.nurul.com 57
Quick Sort Pseudocode
quicksort(list, leftMostIndex, rightMostIndex) {
if( leftMostIndex < rightMostIndex )
pivot = list[rightMostIndex]
left = leftMostIndex
right = rightMostIndex – 1
end if
loop (left <= right)
// Find key on left that belongs on right
loop (list[left] < pivot)
left = left + 1 Make sure not to
end loop run off the array
// Find key on right that belongs on left
loop (right >= left AND list[right] > pivot)
right = right – 1
end loop
Quick Sort Pseudocode (cont)
// Swap out-of-order elements
if (left <= right) // Why swap if left = right?
swap(list, left, right)
left = left + 1
right = right – 1
end if
Must account for special case of
end loop
list[left]=list[right]=pivot
// Move the pivot element to its correct location
swap(list, left, rightMostIndex)
// Continue splitting list and sorting
quickSort(list, leftMostIndex, right)
quickSort(list, left+1, rightMostIndex)
}
12/07/21 03:35 PM http://www.nurul.com 59
Quick Sort
• A couple of notes about quick sort:
• There are more optimal ways to choose the
pivot value (such as the median-of-three
method).
• Also, when the subarrays get small, it
becomes more efficient to use the insertion
sort as opposed to continued use of quick
sort.