fork download
  1. //Diego Martinez CSC5 Chapter 8, P.487, #9
  2. /*******************************************************************************
  3. * COMPARING SORTING ALGORITHM BENNCHMARKS
  4. * ______________________________________________________________________________
  5. * This program compares the performance of the bubble sort and selection sort
  6. * algorithms by sorting two identical arrays of 20 integers in ascending order.
  7. *
  8. * Computation is based on the Formula:
  9. * Comparing and exchanging array elements while sorting the numbers in
  10. * ascending order using the Bubble Sort and Selection Sort algorithms.
  11. *______________________________________________________________________________
  12. * INPUT
  13. * A predefined array of 20 integer values stored in the program
  14. * no user input is required
  15. * OUTPUT
  16. * The total number of exchanges made by the Bubble Sort algorithm
  17. * The total number of exchanges made by the Selection Sort algorithm
  18. *******************************************************************************/
  19. #include <iostream>
  20. using namespace std;
  21.  
  22. // Function Prototypes
  23. void bubbleSort(int array[], int size, int &exchanges);
  24. void selectionSort(int array[], int size, int & exchanges);
  25.  
  26. int main()
  27. {
  28.  
  29. const int SIZE = 20;
  30.  
  31. // Original array
  32. int numbers[SIZE] = {
  33. 45, 23, 67, 12, 89,
  34. 34, 90, 11, 56, 78,
  35. 2, 99, 41, 65, 18,
  36. 73, 27, 50, 6, 81
  37. };
  38.  
  39. // Copy arrays
  40. int bubbleArray[SIZE];
  41. int selectionArray[SIZE];
  42.  
  43. // Copy values into both arrays
  44. for (int i = 0; i < SIZE; i++)
  45. {
  46. bubbleArray[i] = numbers[i];
  47. selectionArray[i] = numbers[i];
  48. }
  49. int bubbleExchanges = 0;
  50. int selectionExchanges = 0;
  51.  
  52. // Call Sorting Functions
  53. bubbleSort(bubbleArray, SIZE, bubbleExchanges);
  54. selectionSort(selectionArray, SIZE, selectionExchanges);
  55.  
  56. // Display results
  57. cout << "Bubble Sort Exchanges: " << bubbleExchanges << endl;
  58. cout << "Selection Sort Exchanges: " << selectionExchanges << endl;
  59.  
  60. return 0;
  61. }
  62. // Bubble Sort Function
  63. void bubbleSort(int array[], int size, int & exchanges)
  64. {
  65. int temp;
  66. for (int maxElement = size - 1; maxElement > 0; maxElement--)
  67. {
  68. for (int index = 0; index < maxElement; index++)
  69. {
  70. if (array[index] > array[index + 1])
  71. {
  72. temp = array [index];
  73. array[index] = array[index + 1];
  74. array[index + 1] = temp;
  75. exchanges++;
  76. }
  77. }
  78. }
  79. }
  80. // Selection Sort Function
  81. void selectionSort(int array[], int size, int & exchanges)
  82. {
  83. int minIndex, minValue;
  84. for (int start = 0; start < (size -1); start++)
  85. {
  86. minIndex = start;
  87. minValue = array[start];
  88.  
  89. for (int index = start +1; index < size; index++)
  90. {
  91. if (array[index] < minValue)
  92. {
  93. minValue = array[index];
  94. minIndex = index;
  95. }
  96. }
  97. if (minIndex != start)
  98. {
  99. array[minIndex] = array[start];
  100. array[start] = minValue;
  101.  
  102. exchanges++;
  103. }
  104. }
  105. }
Success #stdin #stdout 0s 5312KB
stdin
Standard input is empty
stdout
Bubble Sort Exchanges: 95
Selection Sort Exchanges: 17