bubble sort problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>

using namespace std;

int main() {
    
int pancake[5];
int swap = 0;
int temp;






for (int i = 0;i < 5;i++)
{
    cout << "how many pancakes did person " << i << " eat?" << endl;
    cin >> pancake[i];
}
 
 
for (int i = 0;i < 5;i++) {
    
    if ( pancake[i] > pancake[i + 1]) {
         
         temp = pancake[i];
         pancake[i] = pancake[i + 1];
         swap = 1;
        
         
         
         }
         }


for (int i = 0;i < 5;i++) {
    
    
    cout <<pancake[i] << " " << endl;
    
}
    



system("pause");
return 0;
}



I'm kinda having problem with bubble sort.The program is desgined so that it can sort the numbers out in the order,from most to least.But no.The program prints out garbage and does not work.any help will be appreciated.
Bubble sorts usually have two loops in them and they are usually nested. What I see is not a bubble sort and I am not sure what you are trying to do since most sorts have two loops unless I am building the with a insertion sort.
hmm,,,I see.I have done it wrong then.SO how can I change it around to make it right?
Have you ever tried looking up the algorithm on the internet? It has been around for a long time and has been ported to a lot of languages.

Bubble sorts are generally an outer loop
1
2
for(int outerCount = 0; outerCount < maxElements; outerCount++)
{

An Inner Loop:
1
2
3
4
5
6
7
8
9
10
11
      for(int innerCount = 0; innerCount < maxElements; innerCount++)
      {
             if( element[innerCount] > element[outerCount] )
             {
                   // make a swap...
                   int temp = element[innerCount];
                   element[innerCount] = element[outerCount];
                   element[outerCount] = temp;
            }
     } // end the inner loop
} // end the outer loop. 


These things are brute force sorts and I might not have my code rendition completely right for your situation.
Thank you, and I'm sorry for making you put up with alot of patience on me.However,I do have ONE LAST question.I modified the code a little bit like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

pancake[z] = temp; // end of the sort
int person = i; // You'll later(or probaly already) know why I put this here.
 
}

} // end of inner loop.

} // end of outer loop.

for (int i = 1;i < 5;i++)
{

cout << "person" << person <<  " ate  " <<  pancake[i] << " pancake(s)" << endl;
} // it's supposed to output the number of the person and how many pancakes they ate in order.





problem is the output on the screen.Let's say I chose 4,3,2,and 1.



the output:



person4 ate 1 pancake(s)
person4 ate 2 pancake(s)
person4 ate 3 pancake(s)
person4 ate 4 pancake(s)







it just outputs the same person.I tried to fix this problem,but it doesnt seem to work.Why is this,if you have any idea?
this is the code you used:
 
cout << "person" << person <<  " ate  " <<  pancake[i] << " pancake(s)" << endl;


it should look like:
1
2
// you shouldn't use person..  it should be the counter on the loop.
cout << "person " << i <<  " ate  " <<  pancake[i] << " pancake(s)" << endl;


I hope that helps...

The direction of the if statement that controls the swap in the sort will control the sort order of the sort.
Last edited on
I already tried that,and this is how it prints out,

//lets say I chose 1,2,3, and 4 pancakes.





person 1 ate 1 pancakes
person 2 ate 2 pancakes
person 3 ate 3 pancakes
person 4 ate 4 pancakes,,,

 



at the start of program it asks the user what person 1 and so on ate,
but the output completly ignores this and just prints out in descending order.Hope you know what I mean.
1
2
3
4
5
6
for(int outerCount = 0; outerCount < maxElements; outerCount++)
{
      for(int innerCount = 0; innerCount < maxElements; innerCount++)
      {
             if( element[innerCount] > element[outerCount] )
                 // make a swap. 
That looks like a bad version of selection sort (too much swapping).
bubble sort checks contiguous elements.

@OP: I suppose that you want to mantain the person number related to the pancakes.
You need to store that information somewhere.
By instance you could
1
2
3
4
5
struct pancake{
  int owner, quantity;
};

pancake array[5]; //this is what you sort 


Side note: pancake sorting http://en.wikipedia.org/wiki/Pancake_sorting
Thank you for your reply.Can you write the code for me?as my dad is in my computer and I'm replying from my phone.Thank you.I would much appreciate it.
Topic archived. No new replies allowed.