Selection sort algorithm

I'm having trouble implementing the selection sort algorithm in a program.
I need the program to print this:
This program sorts a set of numbers.

How many values? 6
6
5
4
3
2
1

1, 5, 4, 3, 2, 6
1, 2, 4, 3, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
1, 2, 3, 4, 5, 6
but instead it prints this:
This program sorts a set of numbers.
How many values? 6
6
5
4
3
2
1
6, 5, 4, 3, 2, 1

Can someone help me get from the bottom code to the top code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <iostream>
#include <iomanip>
using namespace std;
void PrintArray(int a[], int size)
{
	for( int i = 0; i < size-1; i++ )
		cout << setw(2) << a[i] << ",";
	cout << setw(2) << a[size-1] << endl;
}
int main ( ) {
    int numval;
    int array[100];
    cout << "This program sorts a set of numbers." << endl;
    cout << "How many values? ";
    cin >> numval;
    for(int i = 0; i < numval; i++) {
            cin >> array[i];
            }
            PrintArray(array, numval);
}


Note that I can't/don't want to change the PrintArray function unless 100% needed, it's something in main I have to add.
I hope someone can help!
bump...
Line 19 is outside of the loop, so if numval is 6 then this is what the code does:
1
2
3
4
for(int i = 0; i < numval; i++) {  // loop 6 times
            cin >> array[i];                   // entering the 6 values into array
}
PrintArray(array, numval);              // then print the array once 

You need to write the selection sort algorithm. Insert calls to PrintArray inside the algorithm at the appropriate spots.
More specific? How would I write the algorithm?
there are hundreds of example.
just type "selection sort" in the search box at the top of this page.
I can't find any that print all the iterations of it, and use that printarray function.
If you have a working selection sort function and your print function then you have all you need.

A selection sort has two loops: outer loop and inner loop. The inner loop selects the next element in the unsorted elements. The outer loop continues until there are no unsorted elements.

So, you should simply call the print function at the end of the outer loop.

Hope this helps.
I have it almost working, but I need it to print the final result number of numbers-1 times, and it's only printing it until it gets the correct result:
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
#include <iostream>
#include <iomanip>
using namespace std;
void PrintArray(int a[], int size)
{
	for( int i = 0; i < size-1; i++ )
		cout << setw(2) << a[i] << ",";
	cout << setw(2) << a[size-1] << endl;
}
 
int main( ) {
	int ind;
    int temp;
	int track;
	int array[100];
    cout << "This program sorts a set of numbers." << endl;
    cout << endl;
    cout << "How many values? ";
    cin >> track;
    for(int i = 0; i < track; i++) {
            cin >> array[i];
            }
            cout << endl;
	for (int i = 0; i < track-1; i++)
	{
	    ind = i;
		
		for (int j = i + 1; j < track; j++)
		{
          //  if(j = i + 1) {
            //     cout << "This is j: " << j << endl;
              //   }

		if (array[j] < array[ind])
                   ind = j;
		}
            if (ind != i)
            {
                 temp = array[i];
                 array[i] = array[ind];
                 array[ind] = temp;
              //   for(int i = 0; i < track-1; i++) {
                 PrintArray(array, track);
            //     }
            }
	}
}

Can someone help me with getting it to print number of numbers (track)-1 times?
call PrintArray(array, track); after sorting work finished.
or write printing code in main().
i.e. you made a function call for 3 line printing algo, but not for more lengthy sorting algo ! call for both or do both in main()
Topic archived. No new replies allowed.