bubbleSort

Hi , Im basicly new here. I have few problems that I met in c++ programming.
The problem is about bubbler sort , it works and all , gives you a correct answer.
But I what I want is to see all my arrays , wich array is that . What I mean is , It gives you the list from smallest to biggest of something , but I want to know wich array is that . Could someone give me a hint where should I even look?

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
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;
// first problem

int main()
{
    int peopleNum  , temp;
    int person[10];
    srand((unsigned)time(NULL));	// Im too lazy to type

    cout <<"How many people ate: ";cin>>peopleNum;	// number of people

    for(int i=0;i<peopleNum;i++)	// generating info
    {
        //cout<<i+1<<" person ate: ";
        person[i] = rand() % 10;
    }
    cout<<endl;
    for(int i=0;i<peopleNum;i++)	// showing info
    {
        cout<<i+1<<" person ate: "<<person[i]<<" chickens."<<endl;
    }

    cout<<endl;

    for(int i=0;i<peopleNum;i++)	// the best sorting in ze world
    {
        for(int j=0;j<peopleNum-1-i;j++)
        {
            if(person[j]>person[j+1])
            {
                temp = person[j+1];
                person[j+1] = person[j];
                person[j]=temp;

            }
        }
    }
    for(int i=0;i<peopleNum;i++)	//the final answer , it gives you the list but it won't tell you wich person is what.
    {
        cout<<i+1<<"."<<person[i]<<endl;
    }


    return 0;
}
What do you mean "I want is to see all my arrays , wich array is that" and "I want to know wich array is that"? Because there is only one array in your code.

Do you mean you want to associate names (or other information) with a person besides just number of chickens eaten?

Name Chickens
John 5
Anne 3
Hugo 7
Miyuki 2

Hugo ate the most chickens (7 of them).

Is that it?
Last edited on
yeah , something like that , I want to see wich array holds the smallest number and wich holds the biggest. for example:
int array [5] = {5 , 2 , 7 ,3 , 9};

and the list should look something like this.

array 1 = 2
array 3 = 3
array 0 = 5
array 2 = 7
array 4 = 9

that is not the array, that is Index of array
An array is an ordered list of things (or elements). Each element is at a specific position (or index into the array).

When you sort an array, you explicitly reorder the elements in the array -- that is, each element gets a new index number.

What you are asking to do is keep track of the original indices associated with each element, as well as that element.

There are two basic ways you do this: recover it or keep it.

If you can assume that the elements are unique, then you can recover the original index by simply searching for it in the original array.

The other method is to encode the original index as part of the element. Meaning that the element is no longer just a single number -- it is two numbers -- and the sorting criteria must now take that pair of numbers and ignore one of them.

1
2
3
4
5
6
7
struct person
{
  int original_index;
  int chickens_eaten;
};

person people[ 10 ];

When you fill the array, make sure that you put the values 0..N in the original_index.
When you sort the array, compare only people[j].chickens_eaten

Hope is helps.
Topic archived. No new replies allowed.