Quick Sort
Quick Sort
1
Merge Sort
• Merge sort:
– divide a list into two halves
– sort the halves
– recombine the sorted halves into a sorted whole
2
Merge Sort Picture
0 1 2 3 4 5 6 7
22 18 12 -4 58 7 31 42
s
22 18 12 -4 58 7 31 42 p
l
i
22 18 12 -4 58 7 31 42 t
22 18 12 -4 58 7 31 42
m
18 22 -4 12 7 58 31 42
e
r
-4 12 18 22 7 31 42 58 g
e
0 1 2 3 4 5 6 7
-4 7 12 18 22 31 42 58
3
def merge_sort(arr): def merge(left, right):
# Base case: if the list has 1 or 0 elements, result = []
it's already sorted i=j=0
if len(arr) <= 1:
return arr # Merge the two sorted lists into result
while i < len(left) and j < len(right):
# Find the middle index to divide the array if left[i] < right[j]:
into two halves result.append(left[i]) # Take from left
mid = len(arr) // 2 i += 1
else:
# Recursively sort the left half result.append(right[j]) # Take from
left_half = merge_sort(arr[:mid]) right
j += 1
# Recursively sort the right half
right_half = merge_sort(arr[mid:]) # Add any remaining elements from left
result.extend(left[i:])
# Merge the sorted halves
return merge(left_half, right_half) # Add any remaining elements from right
result.extend(right[j:])
return result
4
# Example usage
arr = [7, 2, 1, 6, 8, 5, 3, 4]
5
Complexity of Merge
Sort
• To determine the time complexity, let’s break our merge
sort into pieces and analyze the pieces
log(n)
levels
< 28 <
1. Pick a “pivot”
2. Divide into less-than & greater-than pivot
3. Sort each side recursively
9
QuickSort is a sorting algorithm based on the Divide and Conquer that
picks an element as a pivot and partitions the given array around the
picked pivot by placing the pivot in its correct position in the sorted
array.
It works on the principle of divide and conquer, breaking down the
problem into smaller sub-problems.
There are mainly three steps in the algorithm:
1.Choose a Pivot: Select an element from the array as the pivot. The
choice of pivot can vary (e.g., first element, last element, random
element, or median).
2.Partition the Array: Rearrange the array around the pivot. After
partitioning, all elements smaller than the pivot will be on its left, and all
elements greater than the pivot will be on its right. The pivot is then in its
correct position, and we obtain the index of the pivot.
Now recursively sort [5] and [7, 8] → both are already sorted
S 81
43
31 57 select pivot value
13 75
92 0
65 26
S1 0
S2 partition S
31 75
43 65
13 81
92
26 57
QuickSort(S1) and
S1 S2 QuickSort(S2)
0 13 26 31 43 57 65 75 81 92
S 0 13 26 31 43 57 65 75 81 92 Presto! S is sorted
[Weiss]
14
Quick sort vs. Merge sort
Quick sort:
– pick a pivot value from the array
– partition the list around the pivot value
– sort the left half
– sort the right half
Merge sort:
– divide a list into two identically sized halves
– sort the left half
– sort the right half
– recombine the sorted halves into a sorted whole
15
QuickSort Example
i j
5 1 3 9 7 0 4 2 6 8
i j
5 1 3 9 7 0 4 2 6 8
i j
5 1 3 9 7 0 4 2 6 8
i j
5 1 3 2 7 0 4 9 6 8
5 1 3 2 7 0 4 9 6 8
i j
5 1 3 2 7 0 4 9 6 8
i j
5 1 3 2 4 0 7 9 6 8
i j
5 1 3 2 4 0 7 9 6 8
j i
5 1 3 2 4 0 7 9 6 8
j i
0 1 4 2 4 5 6 9 7 8