Thread: Selection-sort

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    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 );
       }
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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....

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    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

  4. #4
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    > DO you like DevC++

    Love it - only better IDE I can think of off the top of my head is MSVC++

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>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

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    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!

  7. #7
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    hey sorry i just fixed that, by fixing the line

    if sizeArray != 1
    to

    if sizeArray != 0

  8. #8
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    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.

  9. #9
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    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

  10. #10
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    worked thnx!

    how can you figure that out ?

  11. #11
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    >>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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Insertion and selection sort
    By Dashing Boy in forum C Programming
    Replies: 4
    Last Post: 08-29-2006, 04:42 PM
  2. Selection Sort problem #2
    By Twigstar in forum C++ Programming
    Replies: 7
    Last Post: 07-11-2005, 07:27 PM
  3. Selection Sort help
    By Twigstar in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2005, 08:39 PM
  4. Selection Sort
    By Bleueyes515 in forum C++ Programming
    Replies: 3
    Last Post: 09-30-2002, 08:33 PM
  5. selection sort records of chars
    By hew in forum C++ Programming
    Replies: 8
    Last Post: 04-23-2002, 03:49 PM