Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

What \'s wrong with this //chapter 8-Q.9 #include using namespace std; //Functio

ID: 440545 • Letter: W

Question

What 's wrong with this //chapter 8-Q.9 #include using namespace std; //Function Prototypes int bubbleSort(int [], int); void showBubble(int [], int); void selectionSort(int [], int); void showSelection(int [], int); int main() { //Array of 20 unsorted values int bubble[5] = {1, 11, 18, 3, 5}; int selection[5] = {1, 11, 18, 3, 5}; int results; //Display Bubble Array cout << " <<< Bubble Sort >>>" << endl; cout << endl; cout << " Original Bubble sorted :" << endl; showBubble(bubble, 5); cout << endl; //Call Bubble Sort Algorithm bubbleSort(bubble, 5); //Display Bubble Sorted Array cout << " Bubble Sort :" << endl; showBubble(bubble, 5); cout << endl; //Display Selection Array cout << " <<< Selection Sort >>> :" << endl; cout << endl; cout << " Original Unsorted Array :" << endl; showSelection(selection, 5); cout << endl; //Call Select Sort Algorithm selectionSort(selection, 5); //Display Selection Sorted Array cout << " Sorted Array " << endl; showSelection(selection, 5); cout << endl; system("pause"); return 0; } int bubbleSort(int array[], int size) { bool swap; int num; int exchange = 0; do { swap = false; for(int count = 0; count < (size - 1); count++) { if (array[count] > array [count + 1]) { num = array[count]; array[count] = array[count + 1]; array[count + 1] = num; exchange++; swap = true; count++; exchange++; } } } while (swap); cout << " No of exchange " << exchange <<endl; cout << endl; } void showBubble(int array[], int size) { for(int count = 0; count < size; count++) cout << array[count] << " "; cout << endl; } void selectionSort(int array[], int size) { int startScan, minIndex, minValue; for (startScan = 0; startScan < (size - 1); startScan++) { minIndex = startScan; minValue = array[startScan]; for(int index = startScan + 1; index < size; index++) { if(array[index] < minValue) { minValue = array[index]; minIndex = index; } } array[minIndex] = array[startScan]; array[startScan] = minValue; } } void showSelection(int array[], int size) { for (int count = 0; count < size; count++) cout << array[count] << " "; cout << endl; }

Explanation / Answer

#include<iostream>              // missing
int bubbleSort(int [], int);
void showBubble(int [], int);
void selectionSort(int [], int);
void showSelection(int [], int);
int main() { //Array of 20 unsorted values
int bubble[5] = {1, 11, 18, 3, 5};
int selection[5] = {1, 11, 18, 3, 5};
int results; //Display Bubble Array
cout << " <<< Bubble Sort >>>" << endl;
cout << endl;
cout << " Original Bubble sorted :" << endl;
showBubble(bubble, 5);
cout << endl; //Call Bubble Sort Algorithm
bubbleSort(bubble, 5); //Display Bubble Sorted Array
cout << " Bubble Sort :" << endl;
showBubble(bubble, 5);
cout << endl; //Display Selection Array
cout << " <<< Selection Sort >>> :" << endl; cout << endl;
cout << " Original Unsorted Array :" << endl;
showSelection(selection, 5); cout << endl; //Call Select Sort Algorithm
selectionSort(selection, 5); //Display Selection Sorted Array
cout << " Sorted Array " << endl;
showSelection(selection, 5);
cout << endl; system("pause");
return 0;
}
int bubbleSort(int array[], int size) {    // EEROR IN BUBBLE SORT
bool swap; int num; int exchange = 0;
do {     // EEROR IN BUBBLE SORT
swap = false;
for(int count = 0; count < (size - 1);count++){  
if (array[count] > array [count + 1]) {
num = array[count];
array[count] = array[count + 1];
array[count + 1] = num;
exchange++;
swap = true;
count++;
exchange++;
}
}
} while (swap);
cout << " No of exchange " << exchange < cout << endl;
}
void showBubble(int array[], int size) {
for(int count = 0; count < size;count++)
cout << array[count] << " ";
cout << endl;
}
void selectionSort(int array[], int size) {
int startScan, minIndex, minValue;
for (startScan = 0; startScan < (size - 1); startScan++) {
minIndex = startScan;
minValue = array[startScan];
for(int index = startScan + 1;index < size; index++) {
if(array[index] < minValue) {
minValue = array[index]; minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minValue;
}
}
void showSelection(int array[], int size) {
for (int count = 0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}

for bubbleSort use this code :

int bubbleSort(int array[], int size) {
      int num,count, exchange ;

        for (int count = 4; count > 0;count--){

   for (int exchange = 0; exchange < count;exchange++){

if (array[exchange] > array [exchange + 1]) {

                         array[exchange] = array[exchange + 1];

                          num = array[exchange];

                         array[exchange + 1] = num;

                  }

        }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote