fork download
  1. // Timothy Price CS1A Ch 8 Challenge 10
  2. /******************************************************************************
  3.  * Compare sorting algorithm progress
  4.  *
  5.  * This program sorts two arrays using bubble sort and selection sort,
  6.  * displaying the progress as each array is sorted
  7.  *
  8.  * Inputs:
  9.  * Two identical arrays of 8 unsorted integers
  10.  *
  11.  * Outputs:
  12.  * Initial array contents
  13.  * Array state after each pass of bubble sort
  14.  * Array state after each pass of selection sort
  15.  *****************************************************************************/
  16.  
  17. #include <iostream>
  18.  
  19. using namespace std;
  20.  
  21. // Function Prototypes
  22. void bubbleSort(int arr[], int size);
  23. void selectionSort(int arr[], int size);
  24. void printArray(const int arr[], int size);
  25.  
  26. int main() {
  27. const int SIZE = 8;
  28.  
  29. // Two identical arrays of 8 integers
  30. int array1[SIZE] = {34, 12, 55, 3, 29, 41, 18, 7};
  31. int array2[SIZE] = {34, 12, 55, 3, 29, 41, 18, 7};
  32.  
  33. // Display first array and run Bubble Sort
  34. cout << "Original First Array (for Bubble Sort):" << endl;
  35. printArray(array1, SIZE);
  36.  
  37. cout << endl << "Bubble Sort Progress (After Each Pass):" << endl;
  38. bubbleSort(array1, SIZE);
  39.  
  40. cout << endl << "----------------------------------------" << endl << endl;
  41.  
  42. // Display second array and run Selection Sort
  43. cout << "Original Second Array (for Selection Sort):" << endl;
  44. printArray(array2, SIZE);
  45.  
  46. cout << endl;
  47. cout << "Selection Sort Progress (After Each Pass):" << endl;
  48. selectionSort(array2, SIZE);
  49.  
  50. return 0;
  51. }
  52.  
  53. // Function Definitions
  54.  
  55. // Bubble sort modified to print array contents after each pass
  56. void bubbleSort(int arr[], int size) {
  57. for (int i = 0; i < size - 1; ++i) {
  58. for (int j = 0; j < size - i - 1; ++j) {
  59. if (arr[j] > arr[j + 1]) {
  60. int temp = arr[j];
  61. arr[j] = arr[j + 1];
  62. arr[j + 1] = temp;
  63. }
  64. }
  65. // Print contents after completion of the current pass
  66. cout << "After Pass " << (i + 1) << ": ";
  67. printArray(arr, size);
  68. }
  69. }
  70.  
  71. // Selection sort modified to print array contents after each pass
  72. void selectionSort(int arr[], int size) {
  73. for (int i = 0; i < size - 1; ++i) {
  74. int minIndex = i;
  75. for (int j = i + 1; j < size; ++j) {
  76. if (arr[j] < arr[minIndex]) {
  77. minIndex = j;
  78. }
  79. }
  80. if (minIndex != i) {
  81. int temp = arr[i];
  82. arr[i] = arr[minIndex];
  83. arr[minIndex] = temp;
  84. }
  85. // Print contents after completion of the current pass
  86. cout << "After Pass " << (i + 1) << ": ";
  87. printArray(arr, size);
  88. }
  89. }
  90.  
  91. // Helper function to print the array
  92. void printArray(const int arr[], int size) {
  93. for (int i = 0; i < size; ++i) {
  94. cout << arr[i] << " ";
  95. }
  96. cout << endl;
  97. }
Success #stdin #stdout 0.01s 5340KB
stdin
Standard input is empty
stdout
Original First Array (for Bubble Sort):
34 12 55 3 29 41 18 7 

Bubble Sort Progress (After Each Pass):
After Pass 1: 12 34 3 29 41 18 7 55 
After Pass 2: 12 3 29 34 18 7 41 55 
After Pass 3: 3 12 29 18 7 34 41 55 
After Pass 4: 3 12 18 7 29 34 41 55 
After Pass 5: 3 12 7 18 29 34 41 55 
After Pass 6: 3 7 12 18 29 34 41 55 
After Pass 7: 3 7 12 18 29 34 41 55 

----------------------------------------

Original Second Array (for Selection Sort):
34 12 55 3 29 41 18 7 

Selection Sort Progress (After Each Pass):
After Pass 1: 3 12 55 34 29 41 18 7 
After Pass 2: 3 7 55 34 29 41 18 12 
After Pass 3: 3 7 12 34 29 41 18 55 
After Pass 4: 3 7 12 18 29 41 34 55 
After Pass 5: 3 7 12 18 29 41 34 55 
After Pass 6: 3 7 12 18 29 34 41 55 
After Pass 7: 3 7 12 18 29 34 41 55