Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 1 Bubble Sort Scan the array from left to right, exchange pairs of elements that are out-of-order. Repeat the above process for upon (N-1) times where N is the number of records in the array. Example: (1st pass ) X[0..7] The last slot now has the largest data II Bubble Sort - Basic
Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 2 Bubble Sort (2nd pass ) X[0..6] This slot now has the 2nd largest data (3rd pass ) X[0..5] This slot now has the 3rd largest data Others’ ordering may also be improved like the above. II Bubble Sort - Basic
Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 3 Bubble Sort Result: Original (N=8): After pass 1: (x[0..7]) After pass 2: (x[0..6]) After pass 3: (x[0..5]) After pass 4: (x[0..4]) After pass 5: (x[0..3]) After pass 6: (x[0..2]) After pass 7: (x[0..1]) void bubblesort(int x[ ], int N) { int up_to_pos=N-1; //prepare up_to_pos for first pass while ( up_to_pos>=1 ) //perform N-1 passes { for (int j=0; j+1<= up_to_pos ; j++) //process each pair {if (x[j] > x[j+1]) swap(x[j],x[j+1]); } up_to_pos --; //prepare up_to_pos for next pass } } up_to_position II Bubble Sort - Basic
Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 4 Insertion Sort Insertion sort –Repeatedly insert a new element into an already sorted list Example: x[0] is sorted Insert (33) to get x[0..1] sorted Insert (21) to get x[0..2] sorted Insert (84) to get x[0..3] sorted Insert (49) to get x[0..4] sorted Insert (50) to get x[0..5] sorted Insert (75) to get x[0..6] sorted III Linear Insertion Sort - Idea
Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 5 Example: To find a suitable position for (49), - we compare 49 with 84 : Because 84>49, we shift 84 to the right. -Then compare 49 with 67 : Because 67>49, we shift 67 to the right. - Then compare 49 with 33 : Here 33 < 49!!, so 49 should be next to 33. We stop searching and put 49 there. 1for i = 1 to n-1 /*ie. for each x[i]=33, 21, 84,.. 75 */ /* find a suitable position in x[0..i] for x[i] and put x[i] there - the search is done from x[i-1] towards x[0] - the search is stopped once the suitable position for x[i] is found - during the search, shift these neighbors to the right to make a hole for x[i] */ 2value_i = x[i] 3neighbr_pos = i-1 4while neighbr_pos >= 0 and A[neighbr_pos] > value_i 5A[neighbr_pos+1] = A[neighbr_pos] //shift the neighbour 6 neighbr_pos --; //go to next neighbour 7x[neighbr_pos +1] = value_i III Linear Insertion Sort - Algorithm
Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 6 Merge Sort At the beginning, a Mr. MergeSort is called to sort: Then 2 other Mr. MergeSorts are called to sort: Both of them say “Still complicated! I’ll split them and call other Mr. MergeSorts to handle.” Then 4 other Mr. MergeSorts are called to sort: All of them say “Still complicated! I’ll split them and call other Mr. MergeSorts to handle.” Then 8 other Mr. MergeSorts are called to sort: “So complicated!!, I’ll split them and call other Mr. MergeSorts to handle.” All of them say ‘This is easy. No need to do anything.’
Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 7 Merge Sort Then the first Mr. MergeSort succeeds and returns. Then each of the 2 Mr. MergeSorts returns the merged numbers. Then the 4 Mr. MergeSorts returns the merged numbers. Then the 8 Mr. MergeSorts return All of them say ‘This is easy. No need do anything.’ Both Mr. MergeSorts call their secretaries Mr. Merge to merge the returned numbers The 4 Mr. MergeSorts call their secretaries Mr. Merge to merge the returned numbers The first Mr. MergeSort calls his secretary Mr. Merge to merge the returned numbers
Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 8 Merge Sort void MERGE-SORT(x, Lower_bound, Upper_bound) Sorts the elements: x = low high void merge-sort (int x[ ], int low, int high) { if (low < high) { int mid, *buffer = new int[high-low+1]; mid = (low + high) / 2; merge-sort(x, low, mid); merge-sort(x, mid+1, high);.. merge the two sorted lists into buffer[].. copy buffer[] into x[low..high] delete [] buffer; }