Double Digit sort issues

In the code below I have created a simple bubble sort algorithm. It works for the most part but for some reason it can not properly handle single digits that are also the first digit of a double digit number. For example, if the algorithm is set to put the data in descending order and I enter 1-10 the output will be 1 10 9 8 7 6 5 4 3 2 Code example is below

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>

using namespace std;


void bubbleSort(int[], int);
void enterCount(int[], int);

int main()
{
    int elements =10;
    int people[elements];

    enterCount(people, elements);
    bubbleSort(people, elements);

    return 0;
}

void enterCount(int count_array[], int elems) // enter the amount eaten for all 10 children and find the highest amount
{
    int temp =0, most=0;
    cout << "Please Enter the Amount of pancakes eaten for all 10 children: " << endl;
    for (int i=0; i <elems; i++)
    {
        cout << "Person " << i+1 <<endl;

        cin >> count_array[i];

            if(count_array[i] >temp) // find the hight amount eaten and who it was that ate the most
            {
                temp = count_array[i];
                most = i+1;
            }
    }
    cout << "The most amount of pancakes eaten is: " << temp << endl;
    cout << "The person who ate the most was " << most << endl;

}

void bubbleSort(int sort_array[], int elem) //sort the arrary in decending order
{
    bool stop = true;
    int temporary =0;
    for(int i =0; i <elem&&stop; i++)
    {

        stop=false;

        for(int j=1; j <(elem-1); j++)
        {
            if(sort_array[j+1] - sort_array[j] > 0) // do the swapping as needed
            {
                    temporary = sort_array[j];
                    sort_array[j]=sort_array[j+1];
                    sort_array[j+1]=temporary;
                    stop=true;
            }
        }
    }


    cout <<"Sorted List: "<<endl;
    for(int k =0; k<elem; k++) // print the array elements after the sort
    {

        cout <<endl;
        cout << sort_array[k] << endl;
    }

}
Last edited on
You made two mistakes. The first one is in your code, on line 50. You never compare the very first element, so the very first element will never be moved.

The big, BIG mistake you made was guessing what the problem was, and then not checking. You guessed that the problem was that dougle-digit integers weren't being sorted. You thought this because you put in a single double-digit integer, as the first element, and it wasn't sorted. Your guess was incorrect; that's not the big problem. The big problem was that you didn't check that your guess was correct.

What you should have then done was test your guess. For example, by trying more double-digit integers, in more places. You didn't; you assumed you knew what the problem was, and you guessed wrong, which meant you were looking for completely the wrong thing and trying to fix a bug that didn't exist.

When you think you have guessed what the problem is, TEST it.
Right, that makes sense, thanks
Topic archived. No new replies allowed.