fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 8 P. 489 #10
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compare Sorting Progress
  6.  * ____________________________________________________________________________
  7.  * This program demonstrates how Bubble Sort and Selection Sort operate step
  8.  * by step. Using two identical arrays of 8 integers, the program displays the
  9.  * amount of passes it took for each in order to sort them in ascending order.
  10.  * This allows you to visualize the process of the sorting progress.
  11.  * ____________________________________________________________________________
  12.  * Input
  13.  * SIZE :amount of elements in both array1 and array2
  14.  * array1 :an array with 8 integers that are identical to array2
  15.  * array2 :an array with 8 integeres that are identical to array1
  16.  * Output
  17.  * bubbleSort :funciton to sort array1
  18.  * selectionSort :funciton to sort array2
  19.  * displayArray :function to display the array
  20.  *****************************************************************************/
  21. #include <iostream>
  22. using namespace std;
  23. //Function Prototypes
  24. void displayArray(const int arr[], int size);
  25. void bubbleSort(int arr[], int size);
  26. void selectionSort(int arr[], int size);
  27.  
  28. int main() {
  29. const int SIZE = 8;
  30. int array1[SIZE] = {25, 10, 56, 3, 45, 8, 19, 30};
  31. int array2[SIZE];
  32.  
  33. for(int i = 0; i < SIZE; i++)
  34. array2[i] = array1[i];
  35.  
  36. cout << "Original contents of the first array (Bubble Sort): " << endl;
  37. displayArray(array1, SIZE);
  38. cout << "Bubble Sort Process: " << endl;
  39. bubbleSort(array1, SIZE);
  40.  
  41. cout << "Original contents of the second array (Selection Sort): " << endl;
  42. displayArray(array2, SIZE);
  43. cout << "Selection Sort Process: " << endl;
  44. selectionSort(array2, SIZE);
  45.  
  46. return 0;
  47. }
  48. //Function to Display Array Contents
  49. void displayArray(const int arr[], int size){
  50. for (int i = 0; i < size; i++)
  51. cout << arr[i] << " " ;
  52. cout << endl;
  53. }
  54. //Bubble Sort Function
  55. void bubbleSort(int arr[], int size){
  56. bool swapped;
  57. for(int i = 0; i < size - 1; i++){
  58. swapped = false;
  59. for(int j = 0; j< size - i - 1; j++){
  60. if(arr[j] > arr[j + 1]){
  61. int temp = arr[j];
  62. arr[j] = arr[j + 1];
  63. arr[j + 1] = temp;
  64. swapped = true;
  65. }
  66. }
  67. cout << "After pass " << i + 1 << ": ";
  68. displayArray(arr, size);
  69.  
  70. if(!swapped)
  71. break;
  72. }
  73. }
  74. //Selection Sort Function
  75. void selectionSort(int arr[], int size){
  76. int minIndex;
  77. int temp;
  78. for(int i = 0; i < size -1; i++){
  79. minIndex = i;
  80. for(int j = i + 1; j < size; j++){
  81. if(arr[j] < arr[minIndex])
  82. minIndex = j;
  83. }
  84. if(minIndex != i){
  85. temp = arr[i];
  86. arr[i] = arr[minIndex];
  87. arr[minIndex] = temp;
  88. }
  89. cout << "After pass " << i + 1 << ": ";
  90. displayArray(arr, size);
  91. }
  92. }
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Original contents of the first array (Bubble Sort): 
25 10 56 3 45 8 19 30 
Bubble Sort Process: 
After pass 1: 10 25 3 45 8 19 30 56 
After pass 2: 10 3 25 8 19 30 45 56 
After pass 3: 3 10 8 19 25 30 45 56 
After pass 4: 3 8 10 19 25 30 45 56 
After pass 5: 3 8 10 19 25 30 45 56 
Original contents of the second array (Selection Sort): 
25 10 56 3 45 8 19 30 
Selection Sort Process: 
After pass 1: 3 10 56 25 45 8 19 30 
After pass 2: 3 8 56 25 45 10 19 30 
After pass 3: 3 8 10 25 45 56 19 30 
After pass 4: 3 8 10 19 45 56 25 30 
After pass 5: 3 8 10 19 25 56 45 30 
After pass 6: 3 8 10 19 25 30 45 56 
After pass 7: 3 8 10 19 25 30 45 56