Professional Documents
Culture Documents
04 DS and Algorithm Session 05
04 DS and Algorithm Session 05
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
0
arr
1 55
2 46
3 38
4 16
5 89
6 83
7 30
28
Ver. 1.0
Session 5
0
arr
1 55
2 46
3 38
4 16
5 89
6 83
7 30
28
Ver. 1.0
Session 5
0
arr
1 55
2 46
3 38
4 16
5 89
6 83
7 30
28
Pivot
Ver. 1.0
Session 5
1 55
2 46
3 38
4 16
5 89
6 83
7 30
28
Ver. 1.0
Session 5
1 55
2 46
3 38
4 16
5 89
6 83
7 30
28
Ver. 1.0
Session 5
Swap 0
arr
1 16 55
2 46
3 38
6 83
7 30
28
16 89 55
Ver. 1.0
Session 5
1 16
2 46
3 38
6 83
7 30
28
55 89
Ver. 1.0
Session 5
1 16
2 46
3 38
6 83
7 30
28
55 89
Ver. 1.0
Session 5
0
arr
1 16
2 46
3 38
6 83
7 30
28
55 89
Ver. 1.0
Session 5
0 0 1 1 2
arr
3 38
4 2 53 6 4 7 5 55 46 38 55 89 89 83 30
List 2
6 83
7 30
28 28 16 16 46
List 1 Pivot
Ver. 1.0
Session 5
Swap 0
arr
1 16 28
2 46
3 38
6 83
7 30
28 16
55 89
List 2
List 1
Ver. 1.0
Session 5
arr
0 0 16
1 28
2 46
3 38
6 83
7 30
55 89
List 2
List 1
Ver. 1.0
Session 5
arr
0 16
List 1
2 46
3 38
6 83
7 30
55 89
List 2
Ver. 1.0
Session 5
arr
0 16
List 1
2 46
3 38
6 83
7 30
55 89
List 2
Ver. 1.0
Session 5
arr
0 16
List 1
2 46
3 38
6 83
7 30
55 89
List 2
Pivot
Ver. 1.0
Session 5
arr
0 16
List 1
2 46
3 38
6 83
7 30
55 89
List 2
Ver. 1.0
Session 5
2 46
3 38
6 83
7 30
arr
55 89
List 2
Ver. 1.0
Session 5
arr
0 16
List 1
2 46
3 38
Swap 5 6 83
7 30 55
30 89 55
List 2
Ver. 1.0
Session 5
arr
0 16
List 1
2 46
3 38
4 30
5 89
6 83
7 55
Ver. 1.0
Session 5
2 46
3 38
4 30
5 89
6 83
7 55
arr
List 2
Ver. 1.0
Session 5
arr
0 16
List 1
2 46
3 38
4 30
5 89
6 83
7 55
Ver. 1.0
Session 5
0
arr
1 28
2 46
3 38
4 30
5 89
6 83
7 55
16
Ver. 1.0
Session 5
1 28
2 46 30
3 38
4 30 46
5 89
6 83
7 55
16
Sublist 1
Sublist 2
Ver. 1.0
Session 5
2. 3. 4. 5.
Set pivot = arr[low] Set i = low + 1 Set j = high Repeat step 6 until i > high or arr[i] > pivot // Search for an // element greater than //
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
Ver. 1.0
Session 5
12. QuickSort(low, j 1) // Apply quicksort on list left to pivot 13. QuickSort(j + 1, high) // Apply quicksort on list right to pivot
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
0
arr
1 10
2 30
3 76
4 3
5 57
6 24
53
Ver. 1.0
Session 5
0
arr
1 10
2 30
3 76
4 3
5 57
6 24
53
Ver. 1.0
Session 5
0
arr
1 10
2 30
3 76
4 3
5 57
6 24
53
Ver. 1.0
Session 5
0
arr
1 10
2 30
3 76
4 3
5 57
6 24
53
Ver. 1.0
Session 5
0
arr
1 10
2 30
3 76
4 3
5 57
6 24
53
Ver. 1.0
Session 5
0
arr
1 10 10
2 2 3 3 30 76 30 76
4 4 5 5 6
53
3 3 5757 24 24
Ver. 1.0
Session 5
0
arr
11
22 3
3 4
4 5 57 3
6 5 24 57
6 24
53
1010 30 30 76 76 3 30 76 10
Ver. 1.0
Session 5
0
arr
1 10
2 30
3 76
4 3
5 57
6 24
53
Ver. 1.0
Session 5
0
arr
11 5310
22 3 3030 76
3 4 76 3
5 4 57 3
6 5 57 24 57
6 24
10 53
Ver. 1.0
Session 5
0
arr
1 30 53
22 3 3 53 7676 30
4 4 55
6 6
10
3 3 2457 57 57 24
Ver. 1.0
Session 5
0 0
arr
1 1 30 10
2 2
44
55 6 6
10 3
53 76 24 30
53 5724 7657 3
Ver. 1.0
Session 5
0
arr
1 10
2 24
3 30
6 76
53 57
Ver. 1.0
Session 5
2. Set mid = (low + high)/2 3. Divide the list into two sublists of nearly equal lengths, and sort each sublist by using merge sort. The steps to do this are as follows:
a. MergeSort(low, mid b. MergeSort(mid + 1, high)
Session 5
i. If (arr[i] <= arr[j]) Store arr[i] at index k in array B Increment i by 1 Else Store arr[j] at index k in array B Increment j by 1 ii. Increment k by 1
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
Repeat this process until the name Steve is not found. Binary search reduces the number of pages to be searched by half each time.
Ver. 1.0
Session 5
0
arr
1 13
2 17
3 19
6 39
7 40
8 47
25 29
Ver. 1.0
Session 5
0
arr
1 13
2 17
3 19
6 39
7 40
8 47
25 29
Ver. 1.0
Session 5
0
arr
1 13
2 17
3 19
6 39
7 40
8 47
25 29
Lower bound
Middle element
Upper bound
Ver. 1.0
Session 5
0
arr
1 13
2 17
3 19
6 39
7 40
8 47
25 29
Lower bound
Upper bound
Middle element
Ver. 1.0
Session 5
0
arr
1 13
2 17
3 19
6 39
7 40
8 47
25 29
Lower bound
Upper bound
Element found
Ver. 1.0
Session 5
Ver. 1.0
Session 5
Ver. 1.0
Session 5
In the worst case scenario, the element is not found in the list:
After the first bisection, the search space is reduced to n/2 elements, where n is the number of elements in the original list. After the second bisection, the search space is reduced to n/4 2 elements, that is, n/2 elements. After i bisections, the number of comparisons would be n/2 elements.
Ver. 1.0
th
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