Question: Complete the Bubblesort algorithm to sort an array of numbers. The pseudocode for the algorithm is provided below. ``` BubbleSort(numbers, numbersSize) { for (i =
Complete the Bubblesort algorithm to sort an array of numbers. The pseudocode for the algorithm is provided below.
```
BubbleSort(numbers, numbersSize) {
for (i = 0; i < numbersSize - 1; i++) {
for (j = 0; j < numbersSize - i - 1; j++) {
if (numbers[j] > numbers[j+1]) {
temp = numbers[j]
numbers[j] = numbers[j + 1]
numbers[j + 1] = temp
}
}
}
}
```
Sample input and output is provided below:
```
UNSORTED: 10 2 78 4 45 32 7 11
SORTED: 2 4 7 10 11 32 45 78
```
COMPLETE CODE BELOW:
#include
using namespace std;
//write your BubbleSort function here
int main() {
int numbers[] = { 10, 2, 78, 4, 45, 32, 7, 11 };
const int NUMBERS_SIZE = 8;
int i;
cout << "Enter eight unsorted numbers: ";
for (i = 0; i < NUMBERS_SIZE; ++i) {
cin >> numbers[i];
}
cout << endl;
cout << "UNSORTED: ";
for (i = 0; i < NUMBERS_SIZE; ++i) {
cout << numbers[i] << ' ';
}
cout << endl;
BubbleSort(numbers, NUMBERS_SIZE);
cout << "SORTED: ";
for (i = 0; i < NUMBERS_SIZE; ++i) {
cout << numbers[i] << ' ';
}
cout << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
