04 - DS and Algorithm - Session - 05.pps
04 - DS and Algorithm - Session - 05.pps
Store the pivot at its correct position between the two parts of
the list.
You repeat this process for each of the two sublists created
after partitioning.
This process continues until one element is left in each
sublist.
arr
28
55
46
38
16
89
83
30
arr
28
55
46
38
16
89
83
30
arr
28
55
46
38
16
89
83
30
Pivot
arr
28
55
46
38
16
89
83
30
Pivot
Greater Value
arr
28
55
46
38
16
89
83
30
Pivot
Greater Value
Smaller Value
Swap
arr
28
16
55
46
38
16
55
89
83
30
Pivot
Greater Value
Smaller Value
arr
28
16
46
38
55
89
83
30
Pivot
Greater Value
arr
28
16
46
38
55
89
83
30
Pivot
Greater Value
Smaller Value
arr
28
16
46
38
55
89
83
30
Pivot
Greater Value
Smaller Value
arr
0 0 1 1 2
4 2 53 6 4 7 5
2828 16 16 46
38
83
30
List 1
Pivot
List 2
Swap
arr
28
16
16
28
List 1
46
38
55
89
83
30
List 2
arr
00
16
28
46
38
55
List 1
89
83
30
List 2
arr
0
16
List 1
46
38
55
89
83
30
List 2
arr
0
16
List 1
46
38
55
89
83
30
List 2
arr
0
16
46
38
55
89
83
30
List 2
List 1
Pivot
arr
0
16
46
38
55
89
83
30
List 2
List 1
Pivot
Greater Value
arr
0
16
46
38
55
89
83
30
List 2
List 1
Pivot
arr
0
16
Swap
5 6
46
38
30
55
89
83
7
30
55
List 2
List 1
Pivot
arr
0
16
46
38
30
89
83
55
List 2
List 1
Pivot
Greater Value
arr
0
16
46
38
30
89
83
55
List 2
List 1
Pivot
Greater Value
Smaller Value
arr
0
16
46
38
30
89
83
55
List 2
List 1
Pivot
Greater Value
Smaller Value
0
arr
16
28
46
38
30
89
83
55
16
28
46
30
38
30
46
89
83
55
Sublist 1
Sublist 2
2.
3.
4.
5.
pivot
6. Increment i by 1
7. Repeat step 8 until j < low or arr[j] < pivot // Search for an element
// smaller than pivot
8. Decrement j by 1
9. If i < j: // If greater element is on the left of smaller element
Just a minute
What is the total number of comparisons for an average
case in a quick sort algorithm?
Answer:
O(n log n)
arr
53
10
30
76
57
24
arr
53
10
30
76
57
24
arr
53
10
30
76
57
24
arr
53
10
30
76
57
24
arr
53
10
30
76
57
24
arr
2 2 3 3
4 4 5 5 6
53
10
10
3030 7676
3 3 5757 24 24
0
arr
53
11
22 3
3 4
10
1010 303030
7676 76 3
45
56
357
24
57
24
arr
53
10
30
76
57
24
arr
11
22 3
3 4
5
4
65
10
53
5310
3030 76
76 3
357
57
24
57
24
arr
22 3 3
10
30
53
5330 7676
4 4 55
6 6
3 3 2457 57 57
24
arr
00
11
22
10
3
30
10
53
24 76
30
44
55 6 6
arr
10
24
30
53
57
76
e. Repeat until j > high: // If there are still some elements in the
// second sublist append them to the new
list
i. Store arr[j] at index k in array B
ii. Increment j by 1
iii. Increment k by 1
f. Repeat until i > mid: // If there are still some elements in the
// first sublist append them to the new list
i. Store arr[i] at index k in array B
ii. Increment I by 1
iii. Increment k by 1
5. Copy all elements from the sorted array B into the original array arr
Just a minute
Which algorithm uses the following procedure to sort a
given list of elements?
1. Select an element from the list called a pivot.
2. Partition the list into two parts such that one part contains
elements lesser than the pivot, and the other part contains
elements greater than the pivot.
3. Place the pivot at its correct position between the two lists.
4. Sort the two parts of the list using the same algorithm.
Answer:
Quick sort
Just a minute
On which algorithm design technique are quick sort and
merge sort based?
Answer:
Quick sort and merge sort are based on the divide and conquer
technique.
Else
Display Found
Just a minute
You have to apply linear search to search for an element in
an array containing 5,000 elements. If, at the end of the
search, you find that the element is not present in the array,
how many comparisons you would have made to search the
required element in the given list?
Answer:
5,000
arr
13
17
19
25
29
39
40
47
arr
13
17
19
25
29
39
40
47
arr
13
17
19
25
29
39
40
47
Lower bound
Middle element
Upper bound
arr
13
17
19
25
29
39
40
47
Lower bound
Upper bound
Middle element
Middle element
Upper bound
arr
13
17
19
25
29
39
40
47
Lower bound
Upper bound
Element found
Just a minute
In ___________ search algorithm, you begin at one end of
the list and scan the list until the desired item is found or the
end of the list is reached.
Answer:
linear
Just a minute
To implement __________ search algorithm, the list should
be sorted.
Answer:
binary
Summary
In this session, you learned that:
Quick sort and merge sort algorithms are based on the divide
and conquer technique.
To sort a list of items by using the quick sort algorithm, you
need to:
Select a pivot value.
Partition the list into two sublists such that one sublist contains
all items less than the pivot, and the second sublist contains all
items greater than the pivot.
Place the pivot at its correct position between the two sublists.
Sort the two sublists by using quick sort.
Summary (Contd.)
The total time taken by the quick sort algorithm depends on
the position of the pivot value and the initial ordering of
elements.
2
Summary (Contd.)
The best case efficiency of linear search is O(1) and the worst
case efficiency of linear search is O(n).
To apply binary search algorithm, you should ensure that the
list to be searched is sorted.
The best case efficiency of binary search is O(1) and the worst
case efficiency of binary search is O(log n).