Download presentation
Presentation is loading. Please wait.
Published byEleanore Stone Modified over 8 years ago
2
www.cs.cityu.edu.hk/~helena 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] 2557483712928633255748371292863325485737129286332548375712928633254837125792863325483712579286332548371257869233 25483712578633 92 The last slot now has the largest data II Bubble Sort - Basic
3
www.cs.cityu.edu.hk/~helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 2 Bubble Sort (2nd pass ) X[0..6] 2548371257863392 This slot now has the 2nd largest data 25483712578633922537481257863392253712485786339225371248578633922537124857863392253712485733 86 92 (3rd pass ) X[0..5] 2548371257338692 This slot now has the 3rd largest data 25483712573386922537481257338692253712485733869225371248573386922537124833 57 8692 Others’ ordering may also be improved like the above. II Bubble Sort - Basic
4
www.cs.cityu.edu.hk/~helena Helena Wong | Dept of CS | City U. of Hong Kong Lec-08. Sorting - 3 Bubble Sort Result: Original (N=8): 25 57 48 37 12 92 86 33 After pass 1: 25 48 37 12 57 86 33 92 (x[0..7]) After pass 2: 25 37 12 48 57 33 86 92(x[0..6]) After pass 3: 25 12 37 48 33 57 86 92 (x[0..5]) After pass 4: 12 25 37 33 48 57 86 92(x[0..4]) After pass 5: 12 25 33 37 48 57 86 92(x[0..3]) After pass 6: 12 25 33 37 48 57 86 92 (x[0..2]) After pass 7: 12 25 33 37 48 57 86 92(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
5
www.cs.cityu.edu.hk/~helena 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
6
www.cs.cityu.edu.hk/~helena 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
7
www.cs.cityu.edu.hk/~helena 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: 5 2 4 7 1 3 2 6 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: 5 2 4 7 1 3 2 6 5 22 64 71 3 5247 1 326 “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.’
8
www.cs.cityu.edu.hk/~helena 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. 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 5 22 64 71 3 5247 1 326 1 2 2 3 4 5 6 7 2 4 5 7 1 2 3 6 2 52 64 71 3 5247 1 326 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
9
www.cs.cityu.edu.hk/~helena 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:.. 5 2 4 7 1 3 2 6.. 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; }
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.