Bugs With My Program That Uses Selection Sort

Hey, I am unsure what I have done wrong in my program that I wrote that is suppose to sort 10 integers from least to greatest and output them. I want some help debugging my code. I am not suppose to be using algorithms.

Also, if possible, could you state any flaws in my code that can be improved on.

Sample Input:
7 98 6 43 2 22 94 3 8 4

Desired Output:
2 3 4 6 7 8 22 43 94 98

Received Output:
7 98 6 43 2 22 94 3 8 4

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

using namespace std;

int main () {

    int tmp, n = 0, low = 2147483647;
    int arr1[10];

    for (int i = 0; i <= 9; i++){
        cin >> arr1[i];
    }

    for (int i = 0; i <= 9; i++){
       if (arr1[i] < low) {
            tmp = arr1[n];
            arr1[n] = arr1[i];
            arr1[i] = tmp;
            n++;
        }
    }

    for (int i = 0; i <= 9; i++) {
        cout << arr1[i] << ' ';
    }

    cin.get();
    return 0;

}


It is like the compiler skips the middle for loop:

1
2
3
4
5
6
7
8
    for (int i = 0; i <= 9; i++){
       if (arr1[i] < low) {
            tmp = arr1[n];
            arr1[n] = arr1[i];
            arr1[i] = tmp;
            n++;
        }
    }


Something else that was strange was that it took 9 seconds for my code to compile, I don't know if it was since I used the highest possible integer value or something else.

Thanks for reading.

EDIT: I figured it out.

Here's my revised code:

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

    using namespace std;

    int main () {

        int bak, low, arr1[10];

        for (int i = 0; i <= 9; i++){
            cin >> arr1[i];
        }

        for (int i = 0; i <= 9; i++){
            low = arr1[i];
            bak = i;
            for (int j = i; j <= 9; j++) {
                if (arr1[j] < low) {
                    low = arr1[j];
                    bak = j;
                }
            }
            arr1[bak] = arr1[i];
            arr1[i] = low;
        }

        for (int i = 0; i <= 9; i++) {
            cout << arr1[i] << ' ';
        }

        cin.get();
        return 0;

    }
Last edited on
Hi,

Have you used a debugger? Hopefully there is GUI one in your IDE. You should be able to step through your code 1 line at a time and keep an eye on the values of your variables, thereby deduce where it all goes wrong.

There is poor person's debugging (ppd), but it is a pain - try to use a proper debugger. ppd consists of putting std::cout statements in your code to see what the values are. Hint there is a problem with the values of i and n

There is a very powerful debugging tool called rubber duck debugging. Purchase a small rubber duck, and place it near your computer. When you have a problem, explain out loud to the duck how your code works. This leads to solving the problem. I am not taking the piss - this really works!! :+D

I think you are going to need more than one for loop to sort this info.

Use constant variables instead of magic numbers like 9 throughout the code

1
2
3
4
5
6
7
8
9
10
11
const unsigned int SIZE = 10;

// hard code the values, until you have it working, then get it to accept input
int arr1[SIZE] = {7, 98, 6, 43, 2, 22, 94, 3, 8, 4};

// canonical for loop, use < with SIZE, not <=
// I prefer not to use i and j for variable names, they look too similar - this can cause problems
for (int pos = 0; pos < SIZE; pos++) {
     if (arr1[pos] < low) {  // this is always true, what is the purpose of it?
                                   // need to compare something else
}


Lines 16 to 18 is a swap function, so investigate making it an actual function.

Try to come up with better variable names - call them by a name for what they actually are.
Last edited on
Topic archived. No new replies allowed.