Download presentation
Presentation is loading. Please wait.
Published byBernard Singleton Modified over 9 years ago
1
Sorting Sorting: –Task of rearranging data in an order. –Order can be: Ascending Order: –1,2,3,4,5,6,7,8,9 Descending Order: –9,8,7,6,5,4,3,2,1 Lexicographic Order: –Dictionary Order »ada, bat, cat, mat, max, may, min
2
Sorting Terminologies: 256432515 122345556 Stable Sort Unsorted Sorted
3
Sorting Terminologies: 256432515 122345556 Unsorted Array A Sorted Array B 256432515 122345556 Unsorted Array A Sorted Array A Not In Place In Place Sorting
4
Sorting Terminologies: –Stable Sort: A list of unsorted data may contain two or more equal data. If a sorting method maintains, –The same relative position of their occurrences in the sorted list, then it is called: –Stable Sort –In Place Sort: Suppose a set of data to be stored is stored in an array A. If a sorting method takes place, –Within the array A only, that is, without using any other extra storage space, then it is called: –In Place Sorting Method »Memory efficient because it does not require extra memory space.
5
Sorting Methods of Sorting: –Various sorting methods are: Bubble Sort Selection Sort Insertion Sort Quick Sort Merge Sort Heap Sort
6
Bubble Sort 30 20 70 10 Pass-1 20 30 70 10 20 30 70 10 20 30 10 70 20 30 10 70 Pass-2 20 30 10 70 20 10 30 70 20 10 30 70 20 10 30 70 Pass-3 10 20 30 70 10 20 30 70 10 20 30 70 1 2 3 4 A[1] vs A[2] A[2] vs A[3] A[3] vs A[4] A[1] vs A[2] A[2] vs A[3] A[3] vs A[4] A[1] vs A[2] A[2] vs A[3] A[3] vs A[4] n = 4 Array A
7
Bubble Sort Pass-1 Pass-2 Pass-3 A[1] vs A[2] A[2] vs A[3] A[3] vs A[4] A[1] vs A[2] A[2] vs A[3] A[3] vs A[4] A[1] vs A[2] A[2] vs A[3] A[3] vs A[4] for i = 1 to n-1 do // Controls number of passes for j = 1 to n-1 do // Controls comparisons in each pass if(A[ j ] > A[ j+1 ]), then Swap(A[ j ], A[ j+1 ]) EndIf EndFor Stop Any optimization possible? n = 4
8
Bubble Sort 30 20 70 10 Pass-1 20 30 70 10 20 30 70 10 20 30 10 70 20 30 10 70 Pass-2 20 30 10 70 20 10 30 70 20 10 30 70 Pass-3 10 20 30 70 1 2 3 4 A[1] vs A[2] A[2] vs A[3] A[3] vs A[4] A[1] vs A[2] A[2] vs A[3] A[1] vs A[2]
9
Bubble Sort Pass-1 Pass-2 Pass-3 A[1] vs A[2] A[2] vs A[3] A[3] vs A[4] A[1] vs A[2] A[2] vs A[3] A[1] vs A[2] n = 4 for i = 1 to n-1 do //Controls number of passes for j = 1 to n-i do //Controls comparisons in each pass if(A[ j ] > A[ j+1 ]), then Swap(A[ j ], A[ j+1 ]) EndIf EndFor Stop i = 1 i = 2 i = 3 j = 1 to 3 j = 1 to 2 j = 1 to 1
10
Bubble Sort Algorithm: –BubbleSort Input: –Array A[1...n] where n is the number of elements. Output: –Array A with all elements in ascending sorted order. Data Structure: –Array A[1...n]
11
Bubble Sort Steps: for i = 1 to n-1 do //Controls the number of passes for j = 1 to n-i do //Controls the comparisons in each pass if(A[ j ] > A[ j+1 ]), then //Logic to swap temp = A[ j ] A[ j ] = A[ j+1 ] A[ j+1 ] = temp EndIf EndFor Stop
12
Tracing of Bubble Sort 30 20 10 40 Pass-1 20 30 10 40 20 10 30 40 20 10 30 40 20 10 30 40 Pass-2 10 20 30 40 10 20 30 40 10 20 30 40 Pass-3 10 20 30 40 1 2 3 4 A[1] vs A[2] A[2] vs A[3] A[3] vs A[4] A[1] vs A[2] A[2] vs A[3] A[1] vs A[2]
13
Tracing of Bubble Sort 40 30 20 10 Pass-1 30 40 20 10 30 20 40 10 30 20 10 40 30 20 10 40 Pass-2 20 30 10 40 20 10 30 40 20 10 30 40 Pass-3 10 20 30 40 1 2 3 4 A[1] vs A[2] A[2] vs A[3] A[3] vs A[4] A[1] vs A[2] A[2] vs A[3] A[1] vs A[2]
14
Tracing of Bubble Sort 10 20 30 40 Pass-1 10 20 30 40 10 20 30 40 10 20 30 40 10 20 30 40 Pass-2 10 20 30 40 10 20 30 40 10 20 30 40 Pass-3 10 20 30 40 1 2 3 4 A[1] vs A[2] A[2] vs A[3] A[3] vs A[4] A[1] vs A[2] A[2] vs A[3] A[1] vs A[2]
15
Tracing of Bubble Sort 55 77 99 33 22 88 66 44 1 2 3 4 5 6 7 8 Exercise
16
Bubble Sort Analysis of Algorithm BubbleSort: –Performance of any sorting algorithm depends on: Number of comparisons Number of movements (Swappings)
17
Analysis of algorithm BubbleSort Summary CaseComplexity Best Case List already in sorted order. T(n) = n 2 Worst Case List sorted in reverse order. T(n) = n 2 Average Case List sorted in random order. T(n) = n 2
18
Selection Sort 12345 Input Array A min_val min_loc Pass-1 Assumption: Element at location 1 should be minimum. 4030501020 12345 4030501020 12345 1030504020 Pass-2 Assumption: Element at location 2 should be minimum. min_val min_loc Minimum element should have been at location 1. Is it at same location? No SWAP Minimum element should have been at location 2. Is it at same location? No SWAP
19
12345 1020504030 Selection Sort Pass-3 Assumption: Element at location 3 should be minimum. min_val min_loc Minimum element should have been at location 3. Is it at same location? No SWAP 12345 1020304050 Pass-4 Assumption: Element at location 4 should be minimum. min_val min_loc Minimum element should have been at location 4. Is it at same location? Yes NO SWAP Output 12345 1020304050 12345 1020304050
20
Selection Sort Input Array A Pass-1 Assumption: Element at location 1 should be minimum. Minimum element should have been at location 1. Is it at same location? No SWAP min_loc min_val 12345 5040302010 12345 5040302010 min_loc min_val min_loc min_val min_loc min_val min_loc min_val Pass-2 Assumption: Element at location 2 should be minimum. Minimum element should have been at location 2. Is it at same location? No SWAP 12345 1040302050 min_loc min_val min_loc min_val min_loc min_val
21
Pass-3 Assumption: Element at location 3 should be minimum. Minimum element should have been at location 3. Is it at same location? Yes NO SWAP 12345 1020304050 min_loc min_val Selection Sort Pass-4 Assumption: Element at location 4 should be minimum. Minimum element should have been at location 4. Is it at same location? Yes NO SWAP 12345 1020304050 min_loc min_val Output 54321 1020304050 54321 1020304050
22
Selection Sort For i = 1 to n-1, do min_val = A[ i ], min_loc = i For j = i+1 to n, do If(min_val > A[ j ]), then min_val = A[ j ] min_loc = j EndIf EndFor If(i != min_loc) Swap(A[ i ], A[min_loc]) EndIf EndFor Stop To sort 5 elements, 4 passes were required. To sort n elements, n-1 passes are required. In 1 st pass, value at location 1 will be smallest. In 2 nd pass, value at location 2 will be smallest. In i-th pass, value at location i will be smallest. In 1 st pass, we need to do comparisons from 2 nd value. In 2 nd pass, we need to do comparisons from 3 rd value. In ith pass, we need to start comparisons from (i+1)th value upto n.
23
Selection Sort Algorithm: –SelectionSort Input: –Array A[1...n] where n is the number of elements. Output: –Array A with all elements in ascending sorted order. Data Structure: –Array A[1...n]
24
Selection Sort Steps: For i = 1 to n-1, do //Controls the number of passes min_val = A[ i ], min_loc = i For j = i+1 to n, do //Controls the comparisons in each pass If(min_val > A[ j ]), then min_val = A[ j ] min_loc = j EndIf EndFor If(i != min_loc) //Swap A[ i ] and A[min_loc] temp = A[ i ] A[ i ] = A[min_loc] A[min_loc] = temp EndIf EndFor
25
Selection Sort Input Array A Pass-1 Assumption: Element at location 1 should be minimum. Minimum element should have been at location 1. Is it at same location? Yes NO SWAP min_loc min_val 12345 1020304050 12345 1020304050 Pass-2 Assumption: Element at location 2 should be minimum. Minimum element should have been at location 2. Is it at same location? Yes NO SWAP 12345 1020304050 min_loc min_val
26
Pass-3 Assumption: Element at location 3 should be minimum. Minimum element should have been at location 3. Is it at same location? Yes NO SWAP 12345 1020304050 min_loc min_val Selection Sort Pass-4 Assumption: Element at location 4 should be minimum. Minimum element should have been at location 4. Is it at same location? Yes NO SWAP 12345 1020304050 min_loc min_val Output 54321 1020304050 54321 1020304050
27
Tracing of Selection Sort Input Array A 54321 4060501020 Pass-1 54321 4060501020 Min 54321 1060504020 Pass-2 54321 1060504020 Min 54321 1020504060 Pass-3 54321 1020504060 Min 54321 1020405060 Pass-4 54321 1020405060 Min 54321 1020405060 No Swap
28
467593182 123456789 Array A Tracing of Selection Sort
29
Analysis of algorithm SelectionSort Summary CaseComplexity Best Case List already in sorted order. T(n) = n 2 Worst Case List sorted in reverse order. T(n) = n 2 Average Case List sorted in random order. T(n) = n 2
30
Insertion Sort Insertion Sort: –Based on a method called: Bridge Player –The way Bridge Player sort their hands. –Picking up one card at a time, placing into its appropriate position.
31
Insertion Sort Array A 12345 Array B 12345 40 30501020 4030501020
32
Insertion Sort B[1] = A[1] For i = 2 to n, do //Pick the element KEY = A[ i ] //Find appropriate location by comparison location = i While(location > 1) AND (KEY < B[location-1]), do location = location - 1 EndWhile //Shift elements if required j = i While( j > location ) B[ j ] = B [ j – 1 ] j = j - 1 EndWhile //Place the element B[location] = KEY EndFor 1 st element - Direct For all other elements 1. Pick the element 2. Find appropriate position / location. 3. Shift existing elements if required. 4. Place the element.
33
Insertion Sort Algorithm: –InsertionSort Input: –Array A[1...n] where n is the number of elements. Output: –Array B[1...n] with elements sorted in ascending order. Data Structure: –Array A[1...n] and B[1...n]
34
B[1] = A[1] For i = 2 to n, do //Pick the element KEY = A[ i ] //Find appropriate location by comparison location = i While(location > 1) AND (KEY < B[location-1]), do location = location - 1 EndWhile //Shift elements if required j = i While( j > location ) B[ j ] = B [ j – 1 ] j = j - 1 EndWhile //Place the element B[location] = KEY EndFor Steps of InsertionSort
35
Tracing of Insertion Sort Array A 12345 40 30501020 Array B 40 3040 304050 10304050 1030405020 0 1 2 3 4 Iteration
36
Tracing of Insertion Sort 564798312 Array A 123456789
37
Analysis of algorithm InsertionSort Summary CaseComplexity Best Case List already in sorted order. T(n) = n Worst Case List sorted in reverse order. T(n) = n 2 Average Case List sorted in random order. T(n) = n 2
38
Sorting Divide & Conquer Break all the sticks into 2 parts and arrange them in order. Solution: 1) Divide: Separate the sticks from the bundle of sticks so that it could be broken. 2) Conquer (Solve / Win): Break each and every stick into 2 parts (which can be easily done) 3) Combine: Arrange all the parts in an order.
39
Quick Sort Quick Sort: –To sort an array, it uses the concept of: Divide & Conquer –Process: Divide a large list/array into a number of smaller lists/arrays. Sort them separately. Combine the results to get the sorted list.
40
Quick Sort Divide & Conquer Problem Divide P1P1 P2P2 P3P3 PnPn.... Combine Solution Solve
41
Quick Sort 11 25442299 1234567 left loc Scan from right to left right 33 88 right loc left Scan from left to right left loc right X X While(A[loc] <= A[right])While(A[loc] >= A[left]) right = right - 1left = left + 1
42
Quick Sort 22 44339988 12345 leftright loc Scan from right to left right While(A[loc] <= A[right]) right = right - 1 AND (loc < right) Scan from left to right While(A[loc] >= A[left]) left = left + 1 AND (loc > left)
43
Quick Sort 44 88223399 12345 leftright loc Scan from right to left right loc left Scan from left to right loc right loc left While (A[loc] <= A[right]) AND loc < right right = right - 1 While (A[loc] >= A[left]) AND loc > left left = left + 1
44
Quick Sort Partition Algorithm Initialize loc to left loc = left While(left < right), do //Scan from right to left While(A[loc] <= A[right]) AND (loc < right), do right = right - 1 EndWhile If(A[loc] > A[right]), then Swap(A[loc], A[right]) loc = right left = left + 1 EndIf //Scan from left to right While(A[loc] >= A[left]) AND (loc > left), do left = left + 1 EndWhile If(A[loc] < A[left]), then Swap(A[loc], A[left]) loc = left right = right - 1 EndIf EndWhile Scan from right to left Check as to why the scanning stopped. Repeat the scannings till left and right do not meet. Scan from left to right
45
Quick Sort 44 88223399 12345 leftright loc Scan from right to left right loc left Scan from left to right loc right loc left While (A[loc] <= A[right]) AND loc < right right = right - 1 While (A[loc] >= A[left]) AND loc > left left = left + 1
46
Quick Sort 33 22 12 leftright loc 44 3 88 99 45 leftright loc left 22 1 leftright loc right 99 5 leftright loc
47
Quick Sort Algorithm: –QuickSort Input: –L: Lower Bound of Array A. –U: Upper Bound of Array A. Output: –Array A with elements sorted in ascending order. Data Structure: –Array.
48
Steps: left = L, right = U if(left < right), then loc = Partition(left, right) QuickSort(left, loc – 1) QuickSort(loc + 1, right) EndIf Stop Quick Sort
49
Algorithm: –Partition Input: –left: Index of the leftmost element in Array A. –right: Index of the rightmost element in Array A. Output: –loc: Final position of the pivot element. Data Structure: –Array. Remark: –Element located at left is taken as the pivot element.
50
loc = left While(left < right), do //Scan from right to left While(A[loc] <= A[right]) AND (loc < right), do right = right - 1 EndWhile If(A[loc] > A[right]), then Swap(A[loc], A[right]) loc = right left = left + 1 EndIf //Scan from left to right While(A[loc] >= A[left]) AND (loc > left), do left = left + 1 EndWhile If(A[loc] < A[left]), then Swap(A[loc], A[left]) loc = left right = right - 1 EndIf EndWhile
51
Tracing of Quick Sort 44 88223399 12345 44 33 22 12 88 99 45 33 88 22 1 99 5
52
Quick Sort 44 88223399 12345 22 33448899 12345 88443322 12345 Which is the best case of QuickSort out of above 3 options? Best Case would be the case in which equal partitions are done.
53
Analysis of algorithm QuickSort Summary CaseComplexity Worst Case List already in sorted order. T(n) = n 2 Worst Case List sorted in reverse order. T(n) = n 2 Best Case / Average Case List sorted in random order. T(n) = n log 2 n
54
Tracing of Quick Sort 558822994411667733 123456789
55
Merge Sort Merge Sort: –Like Quick Sort, it also works on the basic principle of: Divide & Conquer –It uses a concept / technique called: Merging
56
123 123456 123456789 in1jn2k Sorted Array A Sorted Array B Array C 204060 1030 5070 90 110 1020 304050 60 70 90 110 jkikjkikjki 4 kjkjkjk 7 10
57
Merge Sort Merging Algorithm Initialize i, j, k to 1 i = 1, j = 1, k = 1 While( i <= n1 ) AND ( j <= n2 ), do If ( A[ i ] <= B [ j ] ) C[ k ] = A[ i ] i = i + 1, k = k + 1 Else C[ k ] = B[ j ] j = j + 1, k = k + 1 EndIf EndWhile If ( i > n1 ), then While( j <= n2 ) C[ k ] = B[ j ] j = j + 1, k = k + 1 EndWhile Else If ( j > n2 ), then While( i <= n1 ) C[ k ] = A[ i ] i = i + 1, k = k + 1 EndWhile EndIf Compare elements from both the arrays until 1 array finishes. Now check which array has finished. Move the elements of other array one by one.
58
Merge Sort 602040501030 123456 LRmid 602040 123 501030 456 LmidRL R 6020 12 40 3 5010 45 30 6 LmidRLRL RLR 60 1 20 2 50 4 10 5 LRLRLRLR Array A
59
Merge Sort 602040501030 123456 602040 123 501030 456 6020 12 40 3 5010 45 30 6 60 1 20 2 50 4 10 5 Array A 2060 12 204060 123 1050 45 103050 456 102030405060 123456 Output
60
Merge Sort Algorithm: –MergeSort Input: –L: Lower Bound of Array A. –R: Upper Bound of Array A. Output: –Array A with elements sorted in ascending order. Data Structure: –Array.
61
Steps: if(L< R), then mid = floor((L+R) / 2) MergeSort(L, mid) MergeSort(mid + 1, R) Merge(L, mid, R) EndIf Stop MergeSort
62
Algorithm: –Merge Input: –L: Lower Bound of 1 st sub-array of Array A. –mid: Upper Bound of 1 st sub-array of Array A. mid + 1 will be lower bound of 2 nd sub-array of Array A. –R: Upper Bound of 2 nd sub-array of Array A. Output: –Two sub-arrays are merged and sorted in the array A. Data Structure: –Array. Assumption: –Extra storage space of array C.
63
Merge Algorithm Steps: i = L, j = mid + 1, k = L While( i <= mid ) AND ( j <= R ), do If ( A[ i ] <= A [ j ] ) C[ k ] = A[ i ] i = i + 1, k = k + 1 Else C[ k ] = A[ j ] j = j + 1, k = k + 1 EndIf EndWhile If ( i > mid ), then While( j <= R ) C[ k ] = A[ j ] j = j + 1, k = k + 1 EndWhile Else If ( j > R ), then While( i <= mid ) C[ k ] = A[ i ] i = i + 1, k = k + 1 EndWhile EndIf For m = L to k – 1, do A[ m ] = C[ m ] m = m + 1 EndFor
64
Quick Sort v/s Merge Sort if(left < right), then loc = Partition(left, right) QuickSort(left, loc – 1) QuickSort(loc + 1, right) //Not required EndIf Stop if(L< R), then mid = floor((L+R) / 2) MergeSort(L, mid) MergeSort(mid + 1, R) Merge(L, mid, R) EndIf Stop Quick Sort Merge Sort Divide Conquer Combine HARD DIVISION, EASY COMBINATION EASY DIVISION, HARD COMBINATION Equal sub-division is not always guaranteed. Both the sub-problems are of almost equal size always.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.