fork download
  1. // Timothy Price CS1A Ch 8 Challenge 9
  2. /******************************************************************************
  3.  * Compare sorting algorithms
  4.  *
  5.  * This program compares the performance of bubble sort and selection sort
  6.  * algorithms by sorting two identical arrays of unsorted integers and tracking
  7.  * the number of exchanges each algorithm makes.
  8.  *
  9.  * Inputs:
  10.  * Two identical arrays
  11.  * The fixed size of the array (20)
  12.  *
  13.  * Outputs:
  14.  * The original unsorted array displayed on the screen
  15.  * Bubble sort result and number of exchanges
  16.  * Selection sort result and number of exchanges
  17.  *****************************************************************************/
  18.  
  19. #include <iostream>
  20.  
  21. using namespace std;
  22.  
  23. // Function Prototypes
  24. void bubbleSort(int arr[], int size, int& exchanges);
  25. void selectionSort(int arr[], int size, int& exchanges);
  26. void printArray(const int arr[], int size);
  27.  
  28. const int SIZE = 20; // Size of array
  29.  
  30. int main() {
  31.  
  32. // Data dictionary
  33. int array1[SIZE] = {
  34. 84, 15, 33, 97, 2, 56, 71, 24, 68, 41,
  35. 10, 89, 53, 22, 60, 48, 77, 6, 94, 35
  36. };
  37. int array2[SIZE] = {
  38. 84, 15, 33, 97, 2, 56, 71, 24, 68, 41,
  39. 10, 89, 53, 22, 60, 48, 77, 6, 94, 35
  40. };
  41.  
  42. int bubbleExchanges = 0;
  43. int selectionExchanges = 0;
  44.  
  45. // Call original unsorted array
  46. cout << "Original Array:" << endl;
  47. printArray(array1, SIZE);
  48. cout << "--------------------------------------------------------" << endl;
  49.  
  50. // Call Bubble Sort
  51. bubbleSort(array1, SIZE, bubbleExchanges);
  52. cout << "Bubble Sorted Array:" << endl;
  53. printArray(array1, SIZE);
  54. cout << "Bubble Sort Total Exchanges: " << bubbleExchanges << endl << endl;
  55.  
  56. // Call Selection Sort
  57. selectionSort(array2, SIZE, selectionExchanges);
  58. cout << "Selection Sorted Array:" << endl;
  59. printArray(array2, SIZE);
  60. cout << "Selection Sort Total Exchanges: " << selectionExchanges << endl;
  61.  
  62. return 0;
  63. }
  64.  
  65. // Function Definitions
  66.  
  67. // Performs Bubble Sort and counts exchanges
  68. void bubbleSort(int arr[], int size, int& exchanges) {
  69. exchanges = 0;
  70. for (int i = 0; i < size - 1; ++i) {
  71. for (int j = 0; j < size - i - 1; ++j) {
  72. if (arr[j] > arr[j + 1]) {
  73. // Swap elements
  74. int temp = arr[j];
  75. arr[j] = arr[j + 1];
  76. arr[j + 1] = temp;
  77. exchanges++;
  78. }
  79. }
  80. }
  81. }
  82.  
  83. // Performs Selection Sort and counts exchanges
  84. void selectionSort(int arr[], int size, int& exchanges) {
  85. exchanges = 0;
  86. for (int i = 0; i < size - 1; ++i) {
  87. int minIndex = i;
  88. for (int j = i + 1; j < size; ++j) {
  89. if (arr[j] < arr[minIndex]) {
  90. minIndex = j;
  91. }
  92. }
  93. // Swap only if the minimum element is not already in place
  94. if (minIndex != i) {
  95. int temp = arr[i];
  96. arr[i] = arr[minIndex];
  97. arr[minIndex] = temp;
  98. exchanges++;
  99. }
  100. }
  101. }
  102.  
  103. // Helper function to print the array
  104. void printArray(const int arr[], int size) {
  105. for (int i = 0; i < size; ++i) {
  106. cout << arr[i] << " ";
  107. }
  108. cout << endl;
  109. }
Success #stdin #stdout 0s 5340KB
stdin
Standard input is empty
stdout
Original Array:
84 15 33 97 2 56 71 24 68 41 10 89 53 22 60 48 77 6 94 35 
--------------------------------------------------------
Bubble Sorted Array:
2 6 10 15 22 24 33 35 41 48 53 56 60 68 71 77 84 89 94 97 
Bubble Sort Total Exchanges: 94

Selection Sorted Array:
2 6 10 15 22 24 33 35 41 48 53 56 60 68 71 77 84 89 94 97 
Selection Sort Total Exchanges: 16