-
Selection-sort
Hi,
I am writing a program which performs selection sort. but when i run my program it says there are errors generated and will be closed by windows. I am using DevC++. Then i tried the built-in debugger and it's something about segmentation fault. Then i tried running on Borland C++ builder and it says something about memory access violation. Lastly i tried running with Pacific MS-DOS C compiler and it ran, only seem to left out the number 3 which is in the array and added the number '-900' at the beginning of the sentence. Pls tell me what's wrong and how to fix it. Thnx.
Code:
/* selection sort */
#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;
for( i = 0; i < arraySize; i++ )
if ( min > array[ i ] ) {
min = array[ i ];
swapPos = i;
}
if ( arraySize != 1 ) {
temp = array[ 0 ];
array[ 0 ] = min;
array[ swapPos ] = temp;
printf( "%d ", array[ 0 ] );
selSort( &array[ 1 ], arraySize - 1 );
}
}
-
I'm at work, and so I tried this on Devc++ and it compiled and ran ok.......
One thing....
if ( min > array[ i ] )
On the first call to this function, min is uninitialized....
-
yep thnx worked all fine yes yes yes!!
you know what this is the first recursive function that i wrote which actually worked!
However this program only worked when ran under Pacific MS-DOS C compiler, it still generate errors with DEVC++ and also when ran under the command prompt.
A not-related question: DO you like DevC++ and what are some other good IDE's or compilers you'll recommend ?
thnx a lot
-
> DO you like DevC++
Love it - only better IDE I can think of off the top of my head is MSVC++
-
>>A not-related question: DO you like DevC++ and what are some other good IDE's or compilers you'll recommend ?
I downloaded it at work to mess with cuz I dont work in a programming related job.
I use VC++6 at home cuz I'm interested in Windows programming with WinAPI & MFC (though I'm still learning MFC and I have loads of API to learn :))
I must say though that DevC++ has grown on me......quite a bit recently.....I miss the object prompts that VC lets you use and I am not really that familiar with much of its options....I recently downloaded it at home to use as an occasional "alternative" compiler.....so I suppose I must say I like it....and its free too :)
As to advice on compilers........you have more experience than me as I have only used VC and DevC++ ....I once used DJGPP for something...buts that's it for me at this point :(
If you want to try others, then go to Compilers.net and do some downloading......you may find something there you like
-
oops i just noticed that my program has still a little bug, it won't display the largest number in the sorted list! Pls tell me why. Thnx again!
-
hey sorry i just fixed that, by fixing the line
to
-
HI,
One last problem, how come when i compile my program using DevC++, whenever it runs it creates errors. WHen i compile it with pacific ms-dos compiler it doesn't generate errors, but the getch() function doesn't work. I want it because i want to just double click the .exe and run it without it being closed half-sec later.
thnx
really wanna know why devc++ didn't work.
-
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 :)
-
worked thnx!
how can you figure that out ?
-
>>how can you figure that out ?
Sitting down following the code throught he debugger while scratching head and threating to kill computer :)
I should have initialised the values as soon as I copied it to my compiler............Ah well............I've learned something the hard way this time :).....glad it worked though :)