fork download
  1. // Nathanael Schwartz CS1A Chapter 8, P. 515, #1
  2. //
  3. /*******************************************************************************
  4.  *
  5.  * BUBBLE SORT AND SELECTION SORT WITH PASS PRINTS
  6.  * _____________________________________________________________________________
  7.  * This program uses two identical arrays of eight integers. It displays the
  8.  * contents of the first array, then sorts it using a bubble sort algorithm. The
  9.  * program prints the array contents after each pass of the bubble sort.
  10.  * The program then displays the contents of the second array, then sorts it
  11.  * using a selection sort algorithm, printing the array contents after each pass.
  12.  * _____________________________________________________________________________
  13.  * INPUT
  14.  * arr1 : Array to be sorted using bubble sort
  15.  * arr2 : Array to be sorted using selection sort
  16.  *
  17.  * OUTPUT
  18.  * The contents of the arrays after each pass of the respective sorting
  19.  * algorithm, showing the progress of the sorting.
  20.  *
  21.  ******************************************************************************/
  22.  
  23. #include <iostream>
  24. using namespace std;
  25.  
  26. // Bubble Sort function with pass prints
  27. void bubbleSort(int arr[], int size) {
  28. int exchangeCount = 0;
  29. for (int i = 0; i < size - 1; ++i) {
  30. for (int j = 0; j < size - i - 1; ++j) {
  31. if (arr[j] > arr[j + 1]) {
  32. // Swap the elements
  33. swap(arr[j], arr[j + 1]);
  34. ++exchangeCount;
  35. }
  36. }
  37. // Print the array after each pass
  38. cout << "After pass " << i + 1 << ": ";
  39. for (int k = 0; k < size; ++k) {
  40. cout << arr[k] << " ";
  41. }
  42. cout << endl;
  43. }
  44. }
  45.  
  46. // Selection Sort function with pass prints
  47. void selectionSort(int arr[], int size) {
  48. int exchangeCount = 0;
  49. for (int i = 0; i < size - 1; ++i) {
  50. int minIndex = i;
  51. for (int j = i + 1; j < size; ++j) {
  52. if (arr[j] < arr[minIndex]) {
  53. minIndex = j;
  54. }
  55. }
  56. // Swap the elements if necessary
  57. if (minIndex != i) {
  58. swap(arr[i], arr[minIndex]);
  59. ++exchangeCount;
  60. }
  61. // Print the array after each pass
  62. cout << "After pass " << i + 1 << ": ";
  63. for (int k = 0; k < size; ++k) {
  64. cout << arr[k] << " ";
  65. }
  66. cout << endl;
  67. }
  68. }
  69.  
  70. int main() {
  71. // Initialize two identical arrays of integers
  72. int arr1[8] = {12, 3, 5, 7, 19, 2, 13, 18};
  73. int arr2[8] = {12, 3, 5, 7, 19, 2, 13, 18};
  74.  
  75. // Display the first array
  76. cout << "Original array for bubble sort: ";
  77. for (int i = 0; i < 8; ++i) {
  78. cout << arr1[i] << " ";
  79. }
  80. cout << endl;
  81.  
  82. // Perform bubble sort and print the array after each pass
  83. bubbleSort(arr1, 8);
  84.  
  85. // Display the second array
  86. cout << "\nOriginal array for selection sort: ";
  87. for (int i = 0; i < 8; ++i) {
  88. cout << arr2[i] << " ";
  89. }
  90. cout << endl;
  91.  
  92. // Perform selection sort and print the array after each pass
  93. selectionSort(arr2, 8);
  94.  
  95. return 0;
  96. }
  97.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
Original array for bubble sort: 12 3 5 7 19 2 13 18 
After pass 1: 3 5 7 12 2 13 18 19 
After pass 2: 3 5 7 2 12 13 18 19 
After pass 3: 3 5 2 7 12 13 18 19 
After pass 4: 3 2 5 7 12 13 18 19 
After pass 5: 2 3 5 7 12 13 18 19 
After pass 6: 2 3 5 7 12 13 18 19 
After pass 7: 2 3 5 7 12 13 18 19 

Original array for selection sort: 12 3 5 7 19 2 13 18 
After pass 1: 2 3 5 7 19 12 13 18 
After pass 2: 2 3 5 7 19 12 13 18 
After pass 3: 2 3 5 7 19 12 13 18 
After pass 4: 2 3 5 7 19 12 13 18 
After pass 5: 2 3 5 7 12 19 13 18 
After pass 6: 2 3 5 7 12 13 19 18 
After pass 7: 2 3 5 7 12 13 18 19