fork download
  1. //Devin Scheu CS1A Chapter 8, P. 488, #9
  2. //
  3. /**************************************************************
  4. *
  5. * BENCHMARK BUBBLE AND SELECTION SORT EXCHANGES
  6. * ____________________________________________________________
  7. * This program determines the number of exchanges made by
  8. * bubble sort and selection sort on two identical arrays.
  9. * ____________________________________________________________
  10. * INPUT
  11. * None
  12. *
  13. * OUTPUT
  14. * bubbleExchanges : The number of exchanges for bubble sort
  15. * selectionExchanges : The number of exchanges for selection sort
  16. *
  17. **************************************************************/
  18.  
  19. #include <iostream>
  20. #include <iomanip>
  21.  
  22. using namespace std;
  23.  
  24. // Function for bubble sort with exchange count
  25. void bubbleSort(int arr[], int size, int& exchanges) {
  26. exchanges = 0;
  27. for (int i = 0; i < size - 1; i++) {
  28. for (int j = 0; j < size - i - 1; j++) {
  29. if (arr[j] > arr[j + 1]) {
  30. int temp = arr[j];
  31. arr[j] = arr[j + 1];
  32. arr[j + 1] = temp;
  33. exchanges++;
  34. }
  35. }
  36. }
  37. }
  38.  
  39. // Function for selection sort with exchange count
  40. void selectionSort(int arr[], int size, int& exchanges) {
  41. exchanges = 0;
  42. for (int i = 0; i < size - 1; i++) {
  43. int minIndex = i;
  44. for (int j = i + 1; j < size; j++) {
  45. if (arr[j] < arr[minIndex]) {
  46. minIndex = j;
  47. }
  48. }
  49. if (minIndex != i) {
  50. int temp = arr[i];
  51. arr[i] = arr[minIndex];
  52. arr[minIndex] = temp;
  53. exchanges++;
  54. }
  55. }
  56. }
  57.  
  58. int main () {
  59.  
  60. //Variable Declarations
  61. const int ARRAY_SIZE = 20; //OUTPUT - Size of the arrays
  62. int array1[ARRAY_SIZE] = {5, 7, 2, 8, 9, 1, 0, 4, 3, 6, 10, 12, 14, 16, 18, 11, 13, 15, 17, 19};
  63. int array2[ARRAY_SIZE]; //OUTPUT - Copy of array for selection sort
  64. int bubbleExchanges; //OUTPUT - The number of exchanges for bubble sort
  65. int selectionExchanges; //OUTPUT - The number of exchanges for selection sort
  66.  
  67. //Copy array for independent sorting
  68. for (int i = 0; i < ARRAY_SIZE; i++) {
  69. array2[i] = array1[i];
  70. }
  71.  
  72. //Perform Bubble Sort
  73. bubbleSort(array1, ARRAY_SIZE, bubbleExchanges);
  74.  
  75. //Perform Selection Sort
  76. selectionSort(array2, ARRAY_SIZE, selectionExchanges);
  77.  
  78. //Separator and Output Section
  79. cout << "-------------------------------------------------------" << endl;
  80. cout << "OUTPUT:" << endl;
  81.  
  82. //Output Result
  83. cout << left << setw(25) << "Bubble Sort Exchanges:" << right << setw(15) << bubbleExchanges << endl;
  84. cout << left << setw(25) << "Selection Sort Exchanges:" << right << setw(15) << selectionExchanges << endl;
  85.  
  86. } //end of main()
Success #stdin #stdout 0.01s 5224KB
stdin
Standard input is empty
stdout
-------------------------------------------------------
OUTPUT:
Bubble Sort Exchanges:                35
Selection Sort Exchanges:             13