Professional Documents
Culture Documents
04 - DS and Algorithm - Session - 05.pps
04 - DS and Algorithm - Session - 05.pps
Objectives
In this session, you will learn to:
Sort data by using quick sort
Sort data by using merge sort
Search data by using linear search technique
Search data by using binary search technique
Ver. 1.0
Session 5
Ver. 1.0
Session 5
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.
Ver. 1.0
Session 5
arr
Ver. 1.0
28
55
46
38
16
89
83
30
Session 5
arr
Ver. 1.0
28
55
46
38
16
89
83
30
Session 5
arr
28
55
46
38
16
89
83
30
Pivot
Ver. 1.0
Session 5
arr
28
55
46
38
16
89
83
30
Pivot
Greater Value
Ver. 1.0
Session 5
arr
28
55
46
38
16
89
83
30
Pivot
Greater Value
Ver. 1.0
Smaller Value
Session 5
Swap
arr
28
16
55
46
38
16
55
89
83
30
Pivot
Greater Value
Ver. 1.0
Smaller Value
Session 5
arr
28
16
46
38
55
89
83
30
Pivot
Greater Value
Ver. 1.0
Session 5
arr
28
16
46
38
55
89
83
30
Pivot
Greater Value
Smaller Value
Ver. 1.0
Session 5
arr
28
16
46
38
55
89
83
30
Pivot
Greater Value
Smaller Value
Ver. 1.0
Session 5
arr
0 0 1 1 2
4 2 53 6 4 7 5
2828 16 16 46
38
83
30
List 1
List 2
Pivot
Ver. 1.0
Session 5
Swap
arr
28
16
16
28
List 1
Ver. 1.0
46
38
55
89
83
30
List 2
Session 5
arr
16
28
46
38
55
List 1
Ver. 1.0
89
83
30
List 2
Session 5
arr
16
28
46
38
55
List 1
Ver. 1.0
89
83
30
List 2
Session 5
arr
16
28
46
38
55
List 1
Ver. 1.0
89
83
30
List 2
Session 5
arr
16
28
46
38
55
89
83
30
List 2
List 1
Pivot
Ver. 1.0
Session 5
arr
16
28
46
38
55
89
83
30
List 2
List 1
Pivot
Greater Value
Ver. 1.0
Session 5
arr
16
28
46
38
55
89
83
30
List 2
List 1
Pivot
Ver. 1.0
Session 5
arr
Swap
5 6
16
28
46
38
30
55
89
83
7
30
55
List 2
List 1
Pivot
Ver. 1.0
Session 5
arr
16
28
46
38
30
89
83
55
List 2
List 1
Pivot
Greater Value
Ver. 1.0
Session 5
arr
16
28
46
38
30
89
83
55
List 2
List 1
Pivot
Greater Value
Smaller Value
Ver. 1.0
Session 5
arr
16
28
46
38
30
89
83
55
List 2
List 1
Pivot
Greater Value
Smaller Value
Ver. 1.0
Session 5
0
arr
Ver. 1.0
16
28
46
38
30
89
83
55
Session 5
16
28
46
30
38
30
46
89
83
55
Sublist 1
Ver. 1.0
Sublist 2
Session 5
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
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Answer:
O(n log n)
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5
arr
Ver. 1.0
53
10
30
76
57
24
Session 5
arr
Ver. 1.0
53
10
30
76
57
24
Session 5
arr
Ver. 1.0
53
10
30
76
57
24
Session 5
arr
Ver. 1.0
53
10
30
76
57
24
Session 5
arr
Ver. 1.0
53
10
30
76
57
24
Session 5
arr
Ver. 1.0
2 2 3 3
4 4 5 5 6
53
10
10
3030 7676
3 3 5757 24 24
Session 5
0
arr
Ver. 1.0
53
11
22 3
3 4
10
1010 303030
7676 76 3
45
56
357
24
57
24
Session 5
arr
Ver. 1.0
53
10
30
76
57
24
Session 5
arr
Ver. 1.0
11
22 3
3 4
5
4
65
10
53
5310
3030 76
76 3
357
57
24
57
24
Session 5
arr
Ver. 1.0
22 3 3
10
30
53
5330 7676
4 4 55
6 6
3 3 2457 57 57
24
Session 5
arr
Ver. 1.0
00
11
22
10
3
30
10
53
24 76
30
44
55 6 6
Session 5
arr
Ver. 1.0
10
24
30
53
57
76
Session 5
Session 5
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
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Answer:
Quick sort
Ver. 1.0
Session 5
Answer:
Quick sort and merge sort are based on the divide and conquer
technique.
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Else
Display Found
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Answer:
5,000
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5
arr
Ver. 1.0
13
17
19
25
29
39
40
47
Session 5
arr
Ver. 1.0
13
17
19
25
29
39
40
47
Session 5
arr
13
17
19
25
29
39
40
47
Lower bound
Ver. 1.0
Middle element
Upper bound
Session 5
arr
13
17
19
25
29
39
40
47
Lower bound
Upper bound
Middle element
Ver. 1.0
Upper bound
Middle element
Session 5
arr
13
17
19
25
29
39
40
47
Lower bound
Upper bound
Element found
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Answer:
linear
Ver. 1.0
Session 5
Answer:
binary
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5