0% found this document useful (0 votes)
7 views

Quick Sort, Binary Search, Find Min and Max

sdfhjg
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Quick Sort, Binary Search, Find Min and Max

sdfhjg
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Welcome to All

Data Structures and Algorithm


Subject Name : Data Structures and
Algorithm
Subject Code : 16SCCCA5/16SCCCS5/16SCCIT5

Class : III BCA/III B.Sc C.S/III B.Sc IT


Sub. Incharge : Dr.K.Bhuvaneswari
Assistant Professor
Dept. of BCA

Idhaya College for Women


Kumbakonam
Unit – III : Algorithms – Priority Queues - Heaps –
Heap Sort – Merge Sort – Quick Sort – Binary Search –
Finding the Maximum and Minimum.
Quick Sort

• This sorting algorithm uses the idea of divide and conquer.


• It finds the element called pivot which divides the array into
two halves in such a way that elements in the left half are
smaller than pivot and elements in the right half are greater
than pivot.
• We follow three steps recursively.
1. Find pivot that divides the array into two halves.
2. Quick Sort the left half
3. Quick Sort the right half
Example
• Consider an array arr having 6 elements.
5 2 6 1 3 4
• Arrange the elements in ascending order using Quick Sort
algorithm. This is unsorted array.
Pivot
Initial Point

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

• Now move the pivot to right


So we move left Pivot
• Is pivot > left ?
one position • yes
towards right
0 1 2 3 4 5
4 2 6 1 3 5

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

So we move left • Is pivot > left ? Pivot – 5


one position • No Left - 6
Pivot
towards right • So we swap pivot and left
0 1 2 3 4 5
4 2 6 1 3 5

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

And move the • Now the pivot is So we will start


pivot to left and pointing at left from right
move towards Pivot
right
left
0 1 2 3 4 5
4 2 5 1 3 6

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

• And move the pivot to right


• Now the pivot pointing at right
Pivot
• So we start from left
0 1 2 3 4 5
4 2 3 1 5 6

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.

Elements left of pivot are smaller Pivot Elements right of


pivot are greater
0 1 2 3 4 5
4 2 3 1 5 6

Left sub array Left Right Right sub array


• So pivot has divided the array into two sub arrays
• We will now Quick sort the left sub array.
Pivot – 4
• Is pivot < right? Right - 1
• no
• So we swap pivot & right
Pivot • Start from right

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

Now move the pivot to right


• Is pivot > Left? Pivot – 4
• yes Left - 2
Pivot
• So we move left one position
towards right
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

Right sub array


Left Right
• Pivot has divided the array into right sub array as there is
no element to the left of 1.
• So we Quick sort the right sub array
Pivot Is Pivot < right?
Yes
0 1 2 3 4 5
1 2 3 4 5 6
So we move to left
Left 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
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]

• Case 2) x < aq x < a[mid]

• Case 3) x > aq x > a[mid]


• q is always chosen such that aq is the middle element
q = [(n+1)/2].
This result is called binary search.

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

• The elements in a[1:n] are polynomials, vectors, very large


number or strings of character.
• Straight Maxmin requires 2(n-1) elements comparisons in the
best, average and worst cases.
• The comparison a[i] < min is necessary only when a[i] > max is
false.
• Best case occurs when the elements in increasing order.
• The number of elements comparisons is n-1
• Worst case occurs in decreasing order. In this case the number
of element comparisons is 2(n-1)
• P has to be divided into smaller instances.
• P1 & P2
• P1 = ( [n/2], a[1],……a[n/2] )
• P2 = ( n – [ n/2 ], a[[n/2] + 1]….a[n])
• Max(P) is the larger of Max(P1) & Max(P2)
Recursively finding the maximum and minimum
Thank you

You might also like