unpredictable results with selection sort

closed account (1Ck93TCk)
Hi,
I had this mostly working, but then I messed with it and now I can't figure out what I did to break it. It's just supposed to take an array of numbers, sort them with a function, then print them using another function.

It's also supposed to output the list each time through the loop so you can see each step in the sort. I had this working and now I'm just getting nonsense results. No compiler errors. Any suggestions?

Thank you, I really appreciate any help!
jmb

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
72
73
74
75
76
77
78
79
80
81
82
83
  #include <iostream>


using namespace std;
const int ARRAYSIZE = 5;

void printVal(int smallToLarge[], int nVals);
void selectionSort(int smallToLarge[], int nVals);

int main()
{

    int smallToLarge[ARRAYSIZE];
    int nVals;
    int num;
    int i;
    int temp;
    int location;
    int smallIndex = 0;

    cout << "This program sorts an array from largest to smallest." << endl;

        for (nVals = 0; nVals < ARRAYSIZE; nVals++)
        {
            cout << "Enter a positive number or zero to quit: ";
            cin >> num;

            if (num <= 0)
            {
                cout << "Goodbye" << endl;
                break;
            }

            else
                smallToLarge[nVals] = num;
        }

        cout << "You have entered "<< nVals <<" values." << endl;
        cout << "The values are: ";

        printVal(smallToLarge, nVals);
        cout << endl;

        cout << "Your numbers sorted from smallest to largest are: ";
        selectionSort(smallToLarge, nVals);

    return 0;
}
void printVal(int smallToLarge[], int nVals)
{
    int j;
    for (j = 0; j < nVals; j++)
        {
            cout << smallToLarge[j] << " ";
        }
}
void selectionSort(int smallToLarge[], int nVals)
{
    int i = 0;
    int smallIndex;
    int loc = 0;
    int temp = 0;

	for(int i = 0; i < nVals - 1; i++)
	{
		//find the smallest
		smallIndex = i;
		for(loc = i+1; loc < nVals; loc++)
		{
			if(smallToLarge[loc] < smallToLarge[smallIndex])
				smallIndex = loc;

		//swap the 2 numbers (i and smallIndex)
		temp = smallToLarge[smallIndex];
		smallToLarge[smallIndex] = smallToLarge[i];
		smallToLarge[i] = temp;
                printVal(smallToLarge, nVals);
		cout << endl;
		}

	}

}
Last edited on
Which way are you trying to sort it? Lines 21 and 44 contradict each other.

What input causes it to fail?

Not that they will cause it to fail, per se, but you are declaring a lot of variables that you never use in main, and you have two separate variables called i in selectionSort().
Last edited on
closed account (1Ck93TCk)
lastchance,

Thank you for responding!

I am trying to sort it from smallest to largest - I hadn't noticed that I wrote one of them backwards, lol. Geez.

I get the failure each time I run it, each time with 5 random numbers.

Right now, the final output is correct, but each iteration of the loop shows numbers being swapped, but not necessarily the right ones.

I'm going to sit down and take a close look at my variables and see if that sorts anything out. I really appreciate your help!

jmb
Oh, I'm embarassed!

Move the closing brace currently on line 79 to line 72 instead.

That should fix it.
Topic archived. No new replies allowed.