Presentation is loading. Please wait.

Presentation is loading. Please wait.

Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort.

Similar presentations


Presentation on theme: "Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort."— Presentation transcript:

1 Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort

2 Bubble Sort Smaller values bubble their way to the top like air bubbles rising in water. 4 8 9 3 1 3 2 1 0 3 4 8 3 3 1 3 2 1 0 9

3 Bubble Sort 4 8 9 3 1 3 2 1 0 3 Algorithm Start with the first two elements in the array (element 0 and 1). Compare them. If the value at the smaller subscript is a larger number, swap the values. Continue moving through the array comparing two values at a time.

4 Bubble Sort 4 8 9 3 1 3 2 1 0 3 Compare age[0] and age[1] age[1] is bigger. Do nothing. Compare age[1] and age[2]. age[1] is bigger. Swap age

5 Bubble Sort 4 8 3 9 1 3 2 1 0 3 Compare age[2] and age[3] age[2] is bigger. Swap age

6 Bubble Sort 4 9 3 8 1 3 2 1 0 3 Compare age[3] and age[4] age[3] is bigger. Swap age

7 Bubble Sort 4 3 3 8 1 3 2 1 0 9 This completes pass one Note that the largest value sank to the bottom of the array. This is guaranteed to happen on the first pass. age Begin second pass. Repeat exactly as for first pass. Compare age[0] and age[1], then age[1] and age[2], and so on, swapping values whenever the value at the higher subscript is smaller.

8 Bubble Sort 4 8 3 3 1 3 2 1 0 9 This completes pass two age Begin third pass. Repeat exactly as for first pass. Compare age[0] and age[1], then age[1] and age[2], and so on, swapping values whenever the value at the higher subscript is smaller. Note that no swaps happened in this pass and none will happen in the fourth and final pass.

9 Swap Does this code swap the values in a and b? int a = 2; int b = 3; a = b; b = a;

10 Swap Does this code swap the values in a and b? int a = 2; int b = 3; int temp; temp = a; a = b; b = temp;

11 Bubble Sort Program Initialize array with numbers that are out of order Display contents of array Sort the array using a bubble sort Display contents of array

12 Bubble Sort Program Initialize array with numbers that are out of order const int arraySize = 10; int age[arraySize] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; Display contents of array for (int i = 0; i < arraySize; i++ ) cout << setw(4) << age[i];

13 Bubble Sort Program Sort the array using a bubble sort int temp; for (int pass = 1; pass < arraySize; pass++) { for (int i = 0; i < arraySize; i++) { if (age [i] > age[i + 1]) { temp = age[i]; age[i] = age[i + 1]; age[i+1] = temp; }

14 const int arraySize = 10, temp; int age[arraySize] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; for (int i = 0; i < arraySize; i++ ) cout << setw(4) << age[i]; cout << endl; for (int pass = 1; pass < arraySize; pass++) { for (int i = 0; i < (arraySize - 1); i++) { if (age [i] > age[i + 1]) { temp = age[i]; age[i] = age[i + 1]; age[i+1] = temp; } for (int i = 0; i < arraySize; i++ ) cout << setw(4) << age[i];

15 Exercise Let’s make the bubble sort more efficient. After the first pass of the bubble sort the largest number is guaranteed to be in the highest-numbered element of the array. So we don’t need to check this value on the next pass. 4 8 9 3 1 3 2 1 0 3 4 3 3 8 1 3 2 1 0 9 age Modify bubble sort so it makes one less comparison on each pass. In this example, pass 2 should only compare age[0] & age[1]1, age[1] & age[2], age[2] & age[3] but should not compare age[3] and age[4].

16 const int arraySize = 10, temp; int age[arraySize] = {2, 6, 4, 8, 10, 12, 89, 68, 45, 37 }; int index = arraySize - 1; for (int i = 0; i < arraySize; i++ ) cout << setw(4) << age[i]; cout << endl; for (int pass = 1; pass < arraySize; pass++) { for (int i = 0; i < index; i++) { if (age [i] > age[i + 1]) { temp = age[i]; age[i] = age[i + 1]; age[i+1] = temp; } index--; } for (int i = 0; i < arraySize; i++ ) cout << setw(4) << age[i];

17 Exercise Let’s make the bubble sort even more efficient. The data may not need all the passes to get into the proper order. It may end up in the proper order earlier than that. The way you can tell is if any swaps are made in a pass. If no swaps are made then the data is already in the proper order. No further sorting is necessary. Modify the bubble sort to check to see if any swaps were made on a pass and stop sorting if no swaps were made.

18 int numSwaps; int numPasses = arraySize; for (int pass = 1; pass < numPasses; pass++) { numSwaps = 0; for (int i = 0; i < index; i++) { if (age [i] > age[i + 1]) { temp = a[i]; age[i] = age[i + 1]; age[i+1] = temp; numSwaps++; } if (0 == numSwaps) numPasses = 0; index--; }

19 Happy Valentine’s Day!!


Download ppt "Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort."

Similar presentations


Ads by Google