Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort
Bubble Sort Smaller values bubble their way to the top like air bubbles rising in water
Bubble Sort 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.
Bubble Sort Compare age[0] and age[1] age[1] is bigger. Do nothing. Compare age[1] and age[2]. age[1] is bigger. Swap age
Bubble Sort Compare age[2] and age[3] age[2] is bigger. Swap age
Bubble Sort Compare age[3] and age[4] age[3] is bigger. Swap age
Bubble Sort 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.
Bubble Sort 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.
Swap Does this code swap the values in a and b? int a = 2; int b = 3; a = b; b = a;
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;
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
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];
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; }
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];
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 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].
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];
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.
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--; }
Happy Valentine’s Day!!