1 / 8

Bubble Sort

II Bubble Sort - Basic. 25. 57. 48. 37. 12. 92. 86. 33. 25. 57. 48. 37. 12. 92. 86. 33. 25. 48. 57. 37. 12. 92. 86. 33. 25. 48. 37. 57. 12. 92. 86. 33. 25. 48. 37. 12. 57. 92. 86. 33. 25. 48. 37. 12. 57. 92. 86. 33. 25. 48. 37. 12. 57. 86. 92.

Download Presentation

Bubble Sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. II Bubble Sort - Basic 25 57 48 37 12 92 86 33 25 57 48 37 12 92 86 33 25 48 57 37 12 92 86 33 25 48 37 57 12 92 86 33 25 48 37 12 57 92 86 33 25 48 37 12 57 92 86 33 25 48 37 12 57 86 92 33 92 25 48 37 12 57 86 33 Bubble Sort 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

  2. II Bubble Sort - Basic 25 48 37 12 57 86 33 92 25 48 37 12 57 86 33 92 25 25 25 25 25 25 37 48 37 37 37 48 12 12 37 12 37 48 12 12 48 48 12 48 57 57 57 33 57 57 33 33 33 33 57 33 86 86 86 86 86 86 92 92 92 92 92 92 25 37 48 12 57 86 33 92 25 37 12 48 57 86 33 92 25 37 12 48 57 86 33 92 25 37 12 48 57 86 33 92 25 37 12 48 57 33 86 92 Others’ ordering may also be improvedlike the above. Bubble Sort (2nd pass ) X[0..6] (3rd pass ) X[0..5] This slot now has the 3rd largest data This slot now has the 2nd largest data

  3. II Bubble Sort - Basic 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]) up_to_position 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 } }

  4. III Linear Insertion Sort - Idea 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

  5. III Linear Insertion Sort - Algorithm 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. 1 for 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] */ 2 value_i = x[i] 3 neighbr_pos = i-1 4 while neighbr_pos >= 0 and A[neighbr_pos] > value_i 5 A[neighbr_pos+1] = A[neighbr_pos] //shift the neighbour 6 neighbr_pos --; //go to next neighbour 7 x[neighbr_pos+1] = value_i

  6. 5 2 4 7 1 3 2 6 5 2 4 7 1 3 2 6 1 All of them say ‘This is easy. No need to do anything.’ 5 2 4 7 3 2 6 Merge Sort “So complicated!!, I’ll split them and call other Mr. MergeSorts to handle.” 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:

  7. 5 2 4 7 1 3 2 6 5 2 4 7 2 4 5 7 1 3 2 6 1 2 3 6 5 2 4 7 1 3 2 6 2 5 4 7 1 3 2 6 1 All of them say ‘This is easy. No need do anything.’ 1 5 2 4 7 3 2 6 5 2 4 7 3 2 6 Merge Sort Then the first Mr. MergeSort succeeds and returns. The first Mr. MergeSort calls his secretary Mr. Merge to merge the returned numbers 1 2 2 3 4 5 6 7 Then each of the 2 Mr. MergeSorts returns the merged numbers. 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 Then the 4 Mr. MergeSorts returns the merged numbers. Then the 8 Mr. MergeSorts return.

  8. void MERGE-SORT(x, Lower_bound, Upper_bound) Sorts the elements: x = .. 5 2 4 7 1 3 2 6 .. low high Merge Sort 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; } }

More Related