For function ascendingBubbleSort modify the code in the desc

For function ascendingBubbleSort, modify the code in the descendingBubbleSortV3 function to sort in ascending order.

int descendingBubbleSortV3(int finalExams[], int numberOfExams) {
   int sink = 0;
   int compares = 0;

   bool swapOccurred;
   do {
       swapOccurred = false;
       for (int pair = 0; pair < numberOfExams - 1 - sink; pair++) {
           compares++;
           if (finalExams[pair] < finalExams[pair + 1]) {
               int temp;
               temp = finalExams[pair];
               finalExams[pair] = finalExams[pair + 1];
               finalExams[pair + 1] = temp;
               swapOccurred = true;
           }
       }
       sink++;
   } while (swapOccurred);
   return compares;
}

For function orderedLinearSearch, modify the code in the unorderedLinearSearch function so that it exits the loop once it can be determined that the score is not in the array.

//unordered linear search function
int unorderedLinearSearch(int searchValue, const int finalExams[],
       int numberOfExams) {
   int location = -1;
   for (int index = 0; index < numberOfExams - 1; index++) {
       if (searchValue == finalExams[index]) {
           location = index;
       }
   }
   return location;
}

Solution

Hi, Please find my modification

int ascendingBubbleSort(int finalExams[], int numberOfExams) {
int sink = 0;
int compares = 0;
bool swapOccurred;
do {
swapOccurred = false;
for (int pair = 0; pair < numberOfExams - 1 - sink; pair++) {
compares++;
if (finalExams[pair] > finalExams[pair + 1]) {
int temp;
temp = finalExams[pair];
finalExams[pair] = finalExams[pair + 1];
finalExams[pair + 1] = temp;
swapOccurred = true;
}
}
sink++;
} while (swapOccurred);
return compares;
}

int orderedLinearSearch(int searchValue, const int finalExams[],int numberOfExams) {
int location = -1;
for (int index = 0; index < numberOfExams; index++) {
if (searchValue == finalExams[index]) {
location = index;
break; // exit from loop
}
}
return location;
}

For function ascendingBubbleSort, modify the code in the descendingBubbleSortV3 function to sort in ascending order. int descendingBubbleSortV3(int finalExams[]
For function ascendingBubbleSort, modify the code in the descendingBubbleSortV3 function to sort in ascending order. int descendingBubbleSortV3(int finalExams[]

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site