Help with selection sort

I've been working on this code for a while. I've got almost everything down except for the selection sort. It does not sort and displays garbage for empID instead like follow in the output file:

PAYROLL PROCESSING

Employees in order entered: (original order)

Employee Number Hours Worked PayRate per Hour Wages
-858993460 37.00 100.50 3718.50
-858993460 20.00 7.50 150.00
-858993460 35.00 12.50 437.50
-858993460 10.00 250.00 2500.00
-858993460 4.00 700.00 2800.00
-858993460 40.00 10.00 400.00
-858993460 18.00 27.50 495.00

Employees in ascending order: (supposed to be selection sort)

Employee Number Hours Worked PayRate per Hour Wages
-858993460 37.00 100.50 3718.50
-858993460 20.00 7.50 150.00
-858993460 35.00 12.50 437.50
-858993460 10.00 250.00 2500.00
-858993460 4.00 700.00 2800.00
-858993460 40.00 10.00 400.00
-858993460 18.00 27.50 495.00


After hours of fiddling with the code, I really don't understand how it can be corrected. I would love to have some guidance. Below is my code, bolded parts are related to selection sort. Thank you!!



 
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
static int pos_of_min_empID( const long empID[], int from, int size )
{
    int pos = from ;
    for( int i = from+1 ; i < size ; ++i )
        if( empID[i] < empID[pos] ) pos = i ;
    return pos ;
}

static void do_swap( long& a, long& b ) { long temp = a ; a = b ; b = temp ; }
static void do_swap( double& a, double& b ) { double temp = a ; a = b ; b = temp ; }

void selectionSort( long empID[], double hours[], double payRate[],
                    double wages[], int size )
{
	for( int startScan = 0; startScan < (size-1) ; ++startScan )
	{
		const int minIndex = pos_of_min_empID( empID, startScan, size ) ;

		do_swap( empID[minIndex], empID[startScan] ) ;
		do_swap( hours[minIndex], hours[startScan] ) ;
		do_swap( payRate[minIndex], payRate[startScan] ) ;
		do_swap( wages[minIndex], wages[startScan] ) ;
	}
}

http://coliru.stacked-crooked.com/a/1a1c2118f03745fe
It works perfectly, thank you! How do I get the order index to display in the output?
> How do I get the order index to display in the output?

I do not quite understand this question; could you clarify?
Perhaps an example (output before sorting and output after the sort) would help.
Sorry it was my mistake! The coliru didn't load before so I wasn't sure how to output the sorted IDs, but it's okay now :D The only remaining issue is the output file displays some junk right before the sorted IDs, and I'm not sure why. It's like this:

PAYROLL PROCESSING

Employees in order entered:

Employee Number Hours Worked PayRate per Hour Wages
7846164 37.00 100.50 3718.50
6528446 20.00 7.50 150.00
1254789 35.00 12.50 437.50
2478591 10.00 250.00 2500.00
9634587 4.00 700.00 2800.00
3326459 40.00 10.00 400.00
1234567 18.00 27.50 495.00

Employees in ascending order:

Employee Number Hours Worked PayRate per Hour Wages
-858993460-92559631349317830736831783200707727132248687965119994463780864.00-92559631349317830736831783200707727132248687965119994463780864.008567285355521620057524459483991459480170871131320943796410311216433197316881456677415593080196293059213543335420088419352576.00
-858993460-92559631349317830736831783200707727132248687965119994463780864.00-92559631349317830736831783200707727132248687965119994463780864.008567285355521620057524459483991459480170871131320943796410311216433197316881456677415593080196293059213543335420088419352576.00
-858993460-92559631349317830736831783200707727132248687965119994463780864.00-92559631349317830736831783200707727132248687965119994463780864.008567285355521620057524459483991459480170871131320943796410311216433197316881456677415593080196293059213543335420088419352576.00
-858993460-92559631349317830736831783200707727132248687965119994463780864.00-92559631349317830736831783200707727132248687965119994463780864.008567285355521620057524459483991459480170871131320943796410311216433197316881456677415593080196293059213543335420088419352576.00


(there are some more lines like this but I have to remove due to character limit on this post)

1234567 18.00 27.50 495.00
1254789 35.00 12.50 437.50
2478591 10.00 250.00 2500.00
3326459 40.00 10.00 400.00
6528446 20.00 7.50 150.00
7846164 37.00 100.50 3718.50
9634587 4.00 700.00 2800.00

Last edited on
Line 96 should perhaps be:
1
2
// for (int b = 0; b < SIZE; ++b)
for (int b = 0; b < count; ++b)
That edit somehow removes the sorted numbers and keeps the junk :o
Topic archived. No new replies allowed.