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;

}``````
2
Contributors
5
Replies
6
Views
9 Years
Discussion Span
Last Post by Ancient Dragon

line 24: Performance can be improved as shown below. The outer loop goes from 0 to number-of-elsmenets -1, its not necessary to look at the last item. The inner loop starts at i+1 and continues to the number of elements.

``````for(int i = 0; i < 9; ++i)
{
for(int j= i+1, j < 10; ++j)
{
if( numbers[i] > numbers[j])
{
// swap needed here
}
}
}``````

For the second question, just add a flag to indicate whether or not any swaps were made during that pass. If not, then no point continuing because the array is already sorted.

Sorry to be ignorant but i have no idea what a flag is.

a flag is just an integer whose falue is either true (1) or false(0). So create an int and set it to false before the inner loop starts, them if a swap is made set it to true. After the inner loop finishes check the value of that int and if it is true then break out of the outer loop.

Thanks for the help, but im too thick for c++. I just dont get any of it

>>I just dont get any of it
Sometimes it just takes awhile for things to sink in. Maybe you need to restudy the material in your textbook and to all the problems at the end of the chapters.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.