Thread: Selection sort

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    2

    Selection sort

    i have two logics for selection sortind...pointer p is for arrays base address.
    1st logic:

    Code:
    void selectionSort(int *p)
    {
      int i,j,temp;
      for(i=0;i<24;i++)
      {
        for(j=1;j<25;j++)
        {
          if(*(p+i)>*(p+i+j))
          {
            temp=*(p+i);
            *(p+i)=*(p+i+j);
            *(P+i+j)=temp;
          }
       }
     }
    }
    2nd logic:

    Code:
    void selectionSort(int *p)
    {
      int i,j,temp;
      for(i=0;i<24;i++)
      {
        for(j=i+1;j<25;j++)
        {
          if(*(p+i)>*(p+j))
          {
            temp=*(p+i);
            *(p+i)=*(p+j);
            *(P+j)=temp;
          }
       }
     }
    }

    My question is---logic 1 is not working correctly...i am not able to find out any error.....if anybody?????plzzz explain the fault

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Consider this:
    Code:
      for(i=0;i<24;i++)
      {
        for(j=1;j<25;j++)
        {
          if(*(p+i)>*(p+i+j))
    If p is the start of your array, and i is 20, and j is 20 ... p[40] is way off the end of your array.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    2
    i got it....thnxxx a lot!

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Hard way...
    Code:
    void selectionSort(int *p)
    {
      int i,j,temp;
      for(i=0;i<24;i++)
      {
        for(j=1;j<25;j++)
        {
          if(*(p+i)>*(p+i+j))
          {
            temp=*(p+i);
            *(p+i)=*(p+i+j);
            *(P+i+j)=temp;
          }
       }
     }
    }
    Easy way....
    Code:
    void selectionSort(int *p)
    {
      int i,j,temp;
      for(i=0;i<24;i++)
      {
        for(j=1;j<25;j++)
        {
          if(p[1] > p[j]))
          {
            temp= p[i];
            p[i] = p[j]);
            p[j] =temp;
          }
       }
     }
    }

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    None of those implementations posted are actually Selection Sort.
    Onet of them is actually Bubble Sort, and the rest are just broken.
    The inner loop of seletion sort does not perform a swap. It simply records the position of the lowest (or highest) item, then after that loop it swaps that item into position. Take a look at the implementation on Wikipedia.

    The original code listed as "1st logic" does not work because in the expression *(p+i+j), i+j exceeds 24 and so is a buffer overrun. Not that the size should be hard-coded to 25 to begin with, of course.
    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"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. The 2d selection sort
    By Techgique in forum C++ Programming
    Replies: 3
    Last Post: 08-12-2011, 11:05 PM
  2. Selection Sort help
    By Twigstar in forum C++ Programming
    Replies: 5
    Last Post: 06-26-2005, 08:39 PM
  3. Selection Sort
    By hopeolicious in forum C++ Programming
    Replies: 2
    Last Post: 04-28-2005, 07:42 AM
  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 Nutshell in forum C Programming
    Replies: 10
    Last Post: 01-11-2002, 04:32 AM

Tags for this Thread