Thread: what's wrong with this selection sort program code

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    113

    Question what's wrong with this selection sort program code

    Code:
    # include <stdio.h>
    
    int selection (int sort[], int n)
    {
      int i,j;
    
      for (i=0; i<n; i++)
      {
        for (j=i+1; j<n; j++)
        { 
          if (sort[j] < sort[i])
            sort[i] = sort[j];
        }
      }
    
    return ;
    }
    
    int main (void)
    {
      int a[50],n,i;
      
      printf ("Enter number of elements\n");
      scanf ("%d",&n);
    
      printf ("Enter elements\n");
      for (i=0;i<n;i++)
      scanf ("%d",&a[i]);
    
      selection (a,n);
      
      for (i=0;i<n;i++)
      printf("%d \t",a[i]);
      printf("\n");
    }
    I am getting the following output.. Its weird
    PHP Code:
    Enter number of elements
    5
    Enter elements
    3 5 4 1 2
    1     1     1     1     2 

  2. #2
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    You're supposed to swap the values instead of just assigning sort[i] the value of sort[j].
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Actually why there is a need to swap ?
    Actually, if you observe my code,
    Code:
    if (sort[j] < sort[i])
            sort[i] = sort[j];
    if any element a[j] is less than the first element a[i], a[i] element is replaced with a[j] element.. thats what we wanted right !

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes and what happens to the element that is replaced?

    A[i] = 10
    A[j] = 20

    a[j] = a[i] ... both now equal 10 ... and what happened to the 20?

    Yes you need to swap... not merely assign... that's why you got all those 1s...

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    That is not selection sort, it's well... barely close.
    Also, it would not compile because it contains a return statement that returns nothing, for a non-void function.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    In selection sort, you're supposed to find the maximum or minimum element and swap it in place. This creates a sorted subarray, and you just keep doing that until there are no more unsorted elements.

  7. #7
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    Oh! I got the point what you are saying ..
    That means I need to assign a[j] value to a[i] and a[i] value to a[j] .. right ?
    it this right >>
    Code:
    if (a[j] < a[i])
    {
       swap = a[i]; 
       a[i] = a[j];
       a[j] = swap;

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    For the above logic i have re written ,, I am getting the following output..
    Code:
    Enter number of elements
    5
    Enter elements
     3 5 4 2 1
    1 	2 	4 	5 	15269876
    why there is still an error ??

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    The above program looks like this after re writing the code
    Code:
    # include <stdio.h>
    
    int selection (int sort[], int n)
    {
      int i,j,swap;
    
      for (i=0; i<n; i++)
      {
        for (j=i+1; j<n; j++)
        { 
          if (sort[j] < sort[i])
           // sort[i] = sort[j];
            swap = sort[i]; 
            sort[i] = sort[j];
            sort[j] = swap;
        }
      }
    
    return ;
    }
    
    int main (void)
    {
      int a[50],n,i;
      
      printf ("Enter number of elements\n");
      scanf ("%d",&n);
    
      printf ("Enter elements\n");
      for (i=0;i<n;i++)
      scanf ("%d",&a[i]);
    
      selection (a,n);
      
      for (i=0;i<n;i++)
      printf("%d \t",a[i]);
      printf("\n");
    }

  10. #10
    Registered User
    Join Date
    Jan 2011
    Posts
    113
    I got it guys .. Thanks for Helping .

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You're still not finding the maximum or minimum number. Find the biggest or smallest thing with one loop, then right when your done with that, swap it into its sorted position (on the left or right of the array). Then that element is sorted; you do that for each element.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by suryak View Post
    I got it guys .. Thanks for Helping .
    Well no you didn't actually.
    You got something that sorts, but it isn't Selection Sort. Selection Sort performs the swap in the outer loop, not within the inner loop.
    The sorting algorithm you have, whilst often stumbled upon, doesn't have a well-known name. I call it "Simple Sort".
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    76
    Quote Originally Posted by iMalc View Post
    Well no you didn't actually.
    You got something that sorts, but it isn't Selection Sort. Selection Sort performs the swap in the outer loop, not within the inner loop.
    The sorting algorithm you have, whilst often stumbled upon, doesn't have a well-known name. I call it "Simple Sort".
    Hi Imalc, that is how I learned selection sort as well, sort in the inner loop. Would you mind explaining how to do the sort in the outer loop?

  14. #14
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by dolfaniss View Post
    Hi Imalc, that is how I learned selection sort as well, sort in the inner loop. Would you mind explaining how to do the sort in the outer loop?
    iMalc doesn't need to explain it if you manage to Google "selection sort". The Wikipedia article shows it nicely.

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    76
    Quote Originally Posted by anduril462 View Post
    iMalc doesn't need to explain it if you manage to Google "selection sort". The Wikipedia article shows it nicely.
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-01-2010, 02:36 PM
  2. Selection Sort
    By hopeolicious in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2005, 07:42 AM
  3. Selection Sort
    By hopeolicious in forum C++ Programming
    Replies: 0
    Last Post: 03-13-2005, 12:17 PM
  4. merge sort and selection sort and time spent on both
    By misswaleleia in forum C Programming
    Replies: 3
    Last Post: 06-04-2003, 02:24 PM
  5. Selection Sort
    By volk in forum C++ Programming
    Replies: 3
    Last Post: 05-08-2003, 06:54 AM