Hi am so stuck with the following question, I have input my code at the bottom and it gets me the following display
Data items in original order
2 6 4 8 10 12 89 68 45 37
Data items in ascending order
2 4 6 8 10 12 37 45 68 89
How can i get the display shown in the question, i know i am missing one or two lines of code?
A simple bubble sort algorithm is inefficient for large arrays because it continues to pass
through all elements of the array even when many of them have been sorted. Find out about
simple bubble sort algorithms (easily found on web or programming books). Write a routine
to sort the ten-number list shown in ‘Sample Screen Output’ below, and then make the
following modifications to improve the performance of the bubble sort:
a) After the first pass, the largest number is guaranteed to be in the highest-numbered element
of the array; after the second pass, the two highest numbers are “in place,” and so on. Instead
of making nine comparisons on every pass, modify the bubble sort to make eight comparisons
on the second pass, seven on the third pass, and so on.
b) The data in the array may already be in the proper order or near-proper order, so why make
nine passes if fewer will suffice? Modify the sort to check at the end of each pass if any swaps
have been made. If none have been made, then the data must already be in the proper order, so
the program should terminate. If swaps have been made, then at least one more pass is
needed.
Sample Screen Output
Data items in original order
2 6 4 8 10 12 89 68 45 37
After pass 0: 2 4 6 8 10 12 68 45 37 89
After pass 1: 2 4 6 8 10 12 45 37 68
After pass 2: 2 4 6 8 10 12 37 45
After pass 3: 2 4 6 8 10 12 37
After pass 4: 2 4 6 8 10 12
After pass 5: 2 4 6 8 10
After pass 6: 2 4 6 8
After pass 7: 2 4 6
After pass 8: 2 4
Data items in ascending order
2 4 6 8 10 12 37 45 68 89
Number of comparisons = 45
My code so far...
#include <iomanip>
#include <iostream>
using namespace std;
using std::setw;
main()
{
int numbers[10] = {2,6,4,8,10,12,89,68,45,37};
int Swap;
cout <<" Data items in original order\n";
for(int ctr=0; ctr<10; ctr++)
{
cout<< setw(4) <<numbers[ctr];
}
cout<<"\n\n";
for(int i=0; i<10; i++)
for(int n=0; n<10; n++)
if (numbers[n] > numbers[n + 1])
{
Swap = numbers[n];
numbers[n] = numbers[n + 1];
numbers[n + 1] = Swap;
}
cout<<"Data items in ascending order\n";
for (int n=0; n<10; n++)
cout<< setw(4) << numbers[n];
cout<< endl <<endl;
return 0;
}