fork download
  1. //Cameron Pham CS1A Chapter 8, p. 489, #10
  2. //
  3. /******************************************************************************
  4. * SORT ORDER
  5. * ______________________________________________________________________________
  6. * This program demonstrates two sorting methods, bubble sort and selection sort,
  7. * using two identical arrays of eight integers. It displays the original
  8. * contents of each array, sorts the values, and displays the array after each
  9. * pass. The program allows the user to observe and compare how each sorting
  10. * method organizes the numbers.
  11. *******************************************************************************/
  12. #include <iostream>
  13. using namespace std;
  14. // Function prototypes
  15. void displayArray(const int array[], int size);
  16. void bubbleSort(int array[], int size);
  17. void selectionSort(int array[], int size);
  18. int main()
  19. {
  20. const int SIZE = 8;
  21. // Store identical values in both arrays
  22. int firstArray[SIZE] = {8, 3, 6, 1, 7, 2, 5, 4};
  23. int secondArray[SIZE] = {8, 3, 6, 1, 7, 2, 5, 4};
  24. // Display and sort the first array using bubble sort
  25. cout << "BUBBLE SORT" << endl;
  26. cout << "Original array: ";
  27. displayArray(firstArray, SIZE);
  28. bubbleSort(firstArray, SIZE);
  29. // Display and sort the second array using selection sort
  30. cout << "\nSELECTION SORT" << endl;
  31. cout << "Original array: ";
  32. displayArray(secondArray, SIZE);
  33. selectionSort(secondArray, SIZE);
  34. return 0;
  35. }
  36. // Display all values in an array
  37. void displayArray(const int array[], int size)
  38. {
  39. for (int count = 0; count < size; count++)
  40. {
  41. cout << array[count] << " ";
  42. }
  43. cout << endl;
  44. }
  45. // Sort the array using ascending bubble sort
  46. void bubbleSort(int array[], int size)
  47. {
  48. int temp;
  49. for (int pass = 0; pass < size - 1; pass++)
  50. {
  51. // Compare neighboring values and swap when needed
  52. for (int count = 0; count < size - pass - 1; count++)
  53. {
  54. if (array[count] > array[count + 1])
  55. {
  56. temp = array[count];
  57. array[count] = array[count + 1];
  58. array[count + 1] = temp;
  59. }
  60. }
  61. // Display the array after each pass
  62. cout << "Pass " << pass + 1 << ": ";
  63. displayArray(array, size);
  64. }
  65. }
  66. // Sort the array using ascending selection sort
  67. void selectionSort(int array[], int size)
  68. {
  69. int minIndex;
  70. int temp;
  71. for (int start = 0; start < size - 1; start++)
  72. {
  73. minIndex = start;
  74. // Find the smallest value in the unsorted portion
  75. for (int count = start + 1; count < size; count++)
  76. {
  77. if (array[count] < array[minIndex])
  78. {
  79. minIndex = count;
  80. }
  81. }
  82. // Move the smallest value into its correct position
  83. temp = array[start];
  84. array[start] = array[minIndex];
  85. array[minIndex] = temp;
  86. // Display the array after each pass
  87. cout << "Pass " << start + 1 << ": ";
  88. displayArray(array, size);
  89. }
  90. }
Success #stdin #stdout 0s 5344KB
stdin
Standard input is empty
stdout
BUBBLE SORT
Original array: 8 3 6 1 7 2 5 4 
Pass 1: 3 6 1 7 2 5 4 8 
Pass 2: 3 1 6 2 5 4 7 8 
Pass 3: 1 3 2 5 4 6 7 8 
Pass 4: 1 2 3 4 5 6 7 8 
Pass 5: 1 2 3 4 5 6 7 8 
Pass 6: 1 2 3 4 5 6 7 8 
Pass 7: 1 2 3 4 5 6 7 8 

SELECTION SORT
Original array: 8 3 6 1 7 2 5 4 
Pass 1: 1 3 6 8 7 2 5 4 
Pass 2: 1 2 6 8 7 3 5 4 
Pass 3: 1 2 3 8 7 6 5 4 
Pass 4: 1 2 3 4 7 6 5 8 
Pass 5: 1 2 3 4 5 6 7 8 
Pass 6: 1 2 3 4 5 6 7 8 
Pass 7: 1 2 3 4 5 6 7 8