Quick Sort, Binary Search, Find Min and Max
Quick Sort, Binary Search, Find Min and Max
Array Index 0 1 2 3 4 5
Array Elements 5 2 6 1 3 4
Left Right
Initially pointing to the Initially pointing to the
1st element of the array Last element of the array
Pivot
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
Remember this rule:
• All elements to the right of pivot must be greater than pivot.
• All elements to the left of pivot must be smaller than pivot
Pivot And move towards left So we will start
As the pivot is from right
pointing at left
0 1 2 3 4 5
5 2 6 1 3 4
Left Right
• Is pivot > right? Pivot – 5
• No Right - 4
• So we swap pivot and right
Pivot
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Left Right
• Is pivot > left? Pivot – 5
• Yes Left - 2
• So we move left one position Pivot
towards right
0 1 2 3 4 5
4 2 6 1 3 5
Left Right
Left Right
• Is pivot > left?
• No
Pivot • So we swap pivot and left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Left Right
Pivot – 5 • Is pivot < right?
Right - 6 • yes
Pivot • So we move right one position
towards left
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
Pivot – 5
Right - 3
Pivot
0 1 2 3 4 5
4 2 5 1 3 6
Left Right
• Is pivot < right?
• no
• So we swap pivot and right
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Left Right
• Is pivot > left? Pivot – 5
• yes Left - 3
• So we move left one position Pivot
towards right
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
• Is pivot > left ?
• yes
Pivot – 5
• So we move left one position Pivot Left - 1
right
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Pivot
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
• Now both left & right are pointing at the same element of the
array
• This means 5 is the pivot and it is at the sorted position.
0 1 2 3 4 5
4 2 3 1 5 6
Left Right
Is pivot > left?
Initially pointing to the Yes
1st element start from So we move left one position
towards right Pivot – 4
left
Pivot Left - 1
0 1 2 3 4 5
1 2 3 4 5 6
Left Right
Pivot
0 1 2 3 4 5
1 2 3 4 5 6
Left Right
• Is pivot > Left?
• yes
Pivot
• So we move left one position
towards right
0 1 2 3 4 5
1 2 3 4 5 6
Left Right
• Now both left and right are pointing at the same element.
• This means 4 is the pivot and it is at the started position.
Pivot
Elements left of pivot are smaller
0 1 2 3 4 5
1 2 3 4 5 6
Left sub array
Left Right
• So we Quick sort left sub array
• Pivot has divided the array into left sub array as there is a wall
at the right side of 4
Pivot < right ? Pivot – 1
Pivot right yes right - 3
0 1 2 3 4 5
1 2 3 4 5 6
Left Right
Pivot Elements right of pivot
are greater
0 1 2 3 4 5
1 2 3 4 5 6
Left Right
• Now both left and right are pointing at the same element
of the array
• This means 2 is pivot and it is at the sorted position
0 1 2 3 4 5
1 2 3 4 5 6
Left Right
• The array is sorted.
• Quick sort follows recursive algorithm
• We find the pivot which divides the array into two
halves
• We then find pivot for these halves which ultimately
gives us a sorted array
Partition the array a[m : p - 1]about a[m]
Sorting by partitioning
Binary Search
• Let ai, 1< i<n be a list of elements that are sorted in
nondecreasing order.
• Value j aj = x
• P=(n, ai,….al,x)
• N is the number of elements in the list
• ai…..al is the list of elements
• Three possibilities for binary search
• Case 1) x = aq x = = a[mid]
Eg.
Select 14 entries.
-15, -6, 0, 7, 9, 23, 54, 82, 101, 112, 125, 131, 142, 151
a [1 : 14] => have different values of x.
• The variables low, high & mid values of x: 151, -14 & 9 for two
successful searches & one unsuccessful searches.
Binary decision tree for binary search, n = 14
• The first comparison is x with a[7].
• If x < a[7],then the next comparison is with a[3];
• similarly, if x > a[7],then the next comparison is with a[ll].
• Each path through the tree represents a sequence of
comparisons in the binary search method. I
• If x is present, then the algorithm will end at one of the
circular nodes that lists the index into the array where x was
found.
• If x is not present, the algorithm will terminate at one of the
square nodes.
• Circular nodes are called internal nodes, and square nodes are
referred to as external nodes.
Recursive Binary Search
Finding the Minimum and Maximum