Sorry about the delay....I had to sit in front of VC++ to use the debugger..... I stared and stared and couldnt see it ......but I think I have it now....
on the line if ( min > array[ i ] ) { ..... it never computes true if the lowest number is the first in the array...therefore if you look at the original array,
Code:
{ 3, 6, 1, 2, 4, 7, 5, 9, 10, 8 };
this wont matter until the third pass when 3 is sat at the start and 1 & 2 have already passed.......therefore the conditional statement never executes and swapPos is not initalised on that pass.......therefore, if when you declared the variable at the top of the function block, the value in the segment of memory represented by setPos was -8564 or something...the line
Code:
array[ swapPos ] = temp;
would be
Code:
array[ -8564 ] = temp;
That's where the runtime error came from........
The long and short of it........set swapPos to 0 at the start of the function and it seems to work
Also, I found the same problem you had with it not showing the biggest member in the array, so I added....
Code:
else printf( "%d ", array[ 0 ] );
.....at the end
This should compile and run......
Code:
#include <stdio.h>
void selSort( int array[], int arraySize );
int main()
{
int array[ 10 ] = { 3, 6, 1, 2, 4, 7, 5, 9, 10, 8 };
int i;
printf( "Original array of numbers: \n " );
for ( i = 0; i < 10; i++ )
printf( "%d ", array[ i ] );
printf( "\n" );
printf( "Array after sorted by 'selection-sort': \n" );
selSort( array, 10 );
printf( "\n" );
return 0;
}
void selSort( int array[], int arraySize )
{
int min, temp, i;
int swapPos = 0;
min = array[0];
for( i = 0; i < arraySize; i++ ){
if ( min > array[ i ] ) {
min = array[ i ]; //Find smallest member
swapPos = i; //Find position of smallest member
}
}
if ( arraySize != 1 ) {
temp = array[ 0 ]; // General swap
array[ 0 ] = min;
array[ swapPos ] = temp;
printf( "%d ", array[ 0 ] );
selSort( &array[1], arraySize - 1 ); //shift pointer up 1, size down 1
}
else printf( "%d ", array[ 0 ] ); // Only executes if 1 member in the array
}
If it still doesnt run properly......back to the debugger