fork download
  1. //Devin Scheu CS1A Chapter 8, P. 489, #10
  2. //
  3. /**************************************************************
  4. *
  5. * DEMONSTRATE SORTING WITH PRINT AFTER EACH PASS
  6. * ____________________________________________________________
  7. * This program sorts two identical arrays using bubble sort
  8. * and selection sort, printing the array after each pass.
  9. * ____________________________________________________________
  10. * INPUT
  11. * array1 : An array of 8 integers
  12. * array2 : An indentical array of array 1
  13. *
  14. * OUTPUT
  15. * sortedArrays : The arrays printed after each sort pass
  16. *
  17. **************************************************************/
  18.  
  19. #include <iostream>
  20. #include <iomanip>
  21.  
  22. using namespace std;
  23.  
  24. // Function for bubble sort with print after each pass
  25. void bubbleSort(int arr[], int size) {
  26. for (int i = 0; i < size - 1; i++) {
  27. for (int j = 0; j < size - i - 1; j++) {
  28. if (arr[j] > arr[j + 1]) {
  29. int temp = arr[j];
  30. arr[j] = arr[j + 1];
  31. arr[j + 1] = temp;
  32. }
  33. }
  34. // Print after each pass
  35. cout << "Bubble Sort Pass " << (i + 1) << ": ";
  36. for (int k = 0; k < size; k++) {
  37. cout << arr[k] << " ";
  38. }
  39. cout << endl;
  40. }
  41. }
  42.  
  43. // Function for selection sort with print after each pass
  44. void selectionSort(int arr[], int size) {
  45. for (int i = 0; i < size - 1; i++) {
  46. int minIndex = i;
  47. for (int j = i + 1; j < size; j++) {
  48. if (arr[j] < arr[minIndex]) {
  49. minIndex = j;
  50. }
  51. }
  52. if (minIndex != i) {
  53. int temp = arr[i];
  54. arr[i] = arr[minIndex];
  55. arr[minIndex] = temp;
  56. }
  57. // Print after each pass
  58. cout << "Selection Sort Pass " << (i + 1) << ": ";
  59. for (int k = 0; k < size; k++) {
  60. cout << arr[k] << " ";
  61. }
  62. cout << endl;
  63. }
  64. }
  65.  
  66. int main () {
  67.  
  68. //Variable Declarations
  69. const int ARRAY_SIZE = 8; //OUTPUT - Size of the arrays
  70. int array1[ARRAY_SIZE] = {5, 3, 8, 1, 7, 2, 6, 4};//OUTPUT - First array for bubble sort
  71. int array2[ARRAY_SIZE]; //OUTPUT - Second array for selection sort
  72.  
  73. //Copy array for independent sorting
  74. for (int i = 0; i < ARRAY_SIZE; i++) {
  75. array2[i] = array1[i];
  76. }
  77.  
  78. //Display First Array
  79. cout << "First Array (before bubble sort): ";
  80. for (int i = 0; i < ARRAY_SIZE; i++) {
  81. cout << array1[i] << " ";
  82. }
  83. cout << endl;
  84.  
  85. //Perform Bubble Sort
  86. bubbleSort(array1, ARRAY_SIZE);
  87.  
  88. //Display Second Array
  89. cout << "Second Array (before selection sort): ";
  90. for (int i = 0; i < ARRAY_SIZE; i++) {
  91. cout << array2[i] << " ";
  92. }
  93. cout << endl;
  94.  
  95. //Perform Selection Sort
  96. selectionSort(array2, ARRAY_SIZE);
  97.  
  98. //Separator and Output Section
  99. cout << "-------------------------------------------------------" << endl;
  100. cout << "OUTPUT:" << endl;
  101.  
  102. //Output Final Sorted Arrays
  103. cout << "Final Bubble Sorted Array: ";
  104. for (int i = 0; i < ARRAY_SIZE; i++) {
  105. cout << array1[i] << " ";
  106. }
  107. cout << endl;
  108.  
  109. cout << "Final Selection Sorted Array: ";
  110. for (int i = 0; i < ARRAY_SIZE; i++) {
  111. cout << array2[i] << " ";
  112. }
  113. cout << endl;
  114.  
  115. } //end of main()
Success #stdin #stdout 0.01s 5324KB
stdin
Standard input is empty
stdout
First Array (before bubble sort): 5 3 8 1 7 2 6 4 
Bubble Sort Pass 1: 3 5 1 7 2 6 4 8 
Bubble Sort Pass 2: 3 1 5 2 6 4 7 8 
Bubble Sort Pass 3: 1 3 2 5 4 6 7 8 
Bubble Sort Pass 4: 1 2 3 4 5 6 7 8 
Bubble Sort Pass 5: 1 2 3 4 5 6 7 8 
Bubble Sort Pass 6: 1 2 3 4 5 6 7 8 
Bubble Sort Pass 7: 1 2 3 4 5 6 7 8 
Second Array (before selection sort): 5 3 8 1 7 2 6 4 
Selection Sort Pass 1: 1 3 8 5 7 2 6 4 
Selection Sort Pass 2: 1 2 8 5 7 3 6 4 
Selection Sort Pass 3: 1 2 3 5 7 8 6 4 
Selection Sort Pass 4: 1 2 3 4 7 8 6 5 
Selection Sort Pass 5: 1 2 3 4 5 8 6 7 
Selection Sort Pass 6: 1 2 3 4 5 6 8 7 
Selection Sort Pass 7: 1 2 3 4 5 6 7 8 
-------------------------------------------------------
OUTPUT:
Final Bubble Sorted Array: 1 2 3 4 5 6 7 8 
Final Selection Sorted Array: 1 2 3 4 5 6 7 8