fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 8 P.488 #9
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Compare Exchanges
  6.  * ____________________________________________________________________________
  7.  * This program compares the exchanges made between a bubble sort and a
  8.  * selection sort on two identical arrays. The program will sort the arrays and
  9.  * then display the amount of exchanges each sorting method used.
  10.  * ____________________________________________________________________________
  11.  * Input
  12.  * SIZE :Amount of elements in both arrays
  13.  * array1 :array with 20 integers that is identical to array2
  14.  * array2 :array with 20 integers that is identical to array1
  15.  * Output
  16.  * bubbleExchanges :amount of exchanges used with the bubble sort method
  17.  * selectionExchanges :amount of exchanges used with the selction sort method
  18.  *****************************************************************************/
  19. #include <iostream>
  20. using namespace std;
  21. //Function Prototype
  22. void bubbleSort(int arr[], int size, int &exchangeCount);
  23. void selectionSort(int arr[], int size, int &exchangeCount);
  24.  
  25. int main() {
  26. //Data Dictionary
  27. const int SIZE = 20;
  28. int array1[SIZE] = {25, 47, 3, 19, 8, 56, 32, 71, 44, 10,
  29. 60, 21, 5, 39, 15, 67, 50, 2, 90, 11};
  30. int array2[SIZE];
  31. int bubbleExchanges = 0;
  32. int selectionExchanges = 0;
  33.  
  34. for(int i = 0; i < SIZE; i++)
  35. array2[i] = array1[i];
  36.  
  37. //Sort
  38. bubbleSort(array1, SIZE, bubbleExchanges);
  39. selectionSort(array2, SIZE, selectionExchanges);
  40.  
  41. //Display Result
  42. cout << "Number of exchanges made by Bubble Sort: " << bubbleExchanges << endl;
  43. cout << "Number of exchanges made by Selection Sort: " << selectionExchanges << endl;
  44.  
  45. return 0;
  46. }
  47. //Bubble Sort Function
  48. void bubbleSort(int arr[], int size, int &exchangeCount){
  49. bool swapped;
  50. for(int i = 0; i < size - 1; i++){
  51. swapped = false;
  52. for (int j = 0; j < size - i - 1; j++){
  53. if (arr[j] > arr [j + 1]){
  54. int temp = arr[j];
  55. arr[j] = arr[j + 1];
  56. arr[j + 1] = temp;
  57. exchangeCount++;
  58. swapped = true;
  59. }
  60. }
  61. if(!swapped)
  62. break;
  63. }
  64. }
  65. //Selection Sort Function
  66. void selectionSort(int arr[], int size, int &exchangeCount){
  67. int minIndex;
  68. int temp;
  69. for(int i = 0; i < size - 1; i++){
  70. minIndex = i;
  71. for(int j = i + 1; j < size; j++){
  72. if(arr[j] < arr[minIndex])
  73. minIndex = j;
  74. }
  75. if(minIndex != i){
  76. temp = arr[i];
  77. arr[i] = arr[minIndex];
  78. arr[minIndex] = temp;
  79. exchangeCount++;
  80. }
  81. }
  82. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Number of exchanges made by Bubble Sort: 89
Number of exchanges made by Selection Sort: 17