Home > Enterprise >  problem with dynamic array and pointers in selection sort
problem with dynamic array and pointers in selection sort

Time:10-07

i make a program of selection sort using dynamic array and pointers but after running this code i found that array is being sorted if we give the size input like 4 and 6 but does't sort properly if the size input is like 5 and 7 etc ...i also did bubble sort program before this using same technique of pointers and dynamic array but it gave perfect sorted array in all the condition , i also try to debuge the code but still don't understand why this is happening if anyone having idea about this then please help me out .

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main()
{
  int * ptr,temp,min;
  int size,i,j,s;
  printf("Enter the size of array:");
  scanf("%d",&size);

  ptr = (int *)(calloc (size,sizeof(int)));

  if(ptr == NULL)
    printf("No memory");

  else
  {
    printf("\n=== RANDOM ELEMENTS OF ARRAY ===\n");

    for(s=0;s<size;s  )
      *(ptr s) = rand()%100;

    for(s=0;s<size;s  )
      printf("\nElement [%d]  = %d ",s,*(ptr s));

// selection sort algorithm

  for(i=0;i< size-1;i  )
  {
    min = i;
    for(j=i 1;j<size;j  )
    {
      if(*(ptr j) < *(ptr min))
      {
        min = j;
      }
      temp = *(ptr i);
      *(ptr i) = *(ptr min);
      *(ptr min) = temp;
    }
  }

// End of algorithm

    printf("\n\n=======  SORTED ELEMENTS  =======\n\n");
      for(s=0;s<size;s  )
        printf("Element [%d]  = %d \n",s,*(ptr s));


  }

}

CodePudding user response:

Your Selection Sort algorithm seems to be wrong. You have to replace the element in the current index with the minimum after the inner for loop finishes iteration:

  for(i=0;i< size-1;i  )
  {
    min = i;
    for(j=i 1;j<size;j  )
    {
      if(*(ptr j) < *(ptr min))
      {
        min = j;
      }
    }
    temp = *(ptr i);
    *(ptr i) = *(ptr min);
    *(ptr min) = temp;
  }

Now, it should work for all input sizes.

CodePudding user response:

Clearing away a couple of unnecessary confusion factors...

*(ptr   index)

Is identical to

ptr[index]

But the second is much easier to read.

Next, in the following section the variable min is introduced,

  if(*(ptr j) < *(ptr min))
  {
       min = j;
  }

...but is not necessary for a simple sort. Just stick with i and j and the sort will proceed correctly for either even or odd sets of values. Finally, to eliminate the memory leak, call free(ptr); when ptr is no longer needed. Following is a cleaned up version, with corrections.

int main(void)//added void
{
  int * ptr,temp/*,min*/;
  int size,i,j,s;
  printf("Enter the size of array:");
  scanf("%d",&size);

  ptr = calloc (size,sizeof(int));//casting is required in C   
                                  //but unnecessary in C.
                                  //(and can be problematic)
  if(ptr == NULL)
  {
    printf("No memory");
  }
  else
  {
    printf("\n=== RANDOM ELEMENTS OF ARRAY ===\n");

    for(s=0;s<size;s  )
      ptr[s] = rand()%100;

    for(s=0;s<size;s  )
      printf("\nElement [%d]  = %d ",s,ptr[s]);

// selection sort algorithm

    for(i=0;i< size-1;i  )
    {
        for(j=i 1;j<size;j  )//removed if section introducing 'min'
        {
            if(ptr[j] < ptr[i])
            {
                temp = ptr[i];
                ptr[i] = ptr[j];
                ptr[j] = temp;
            }
        }
    }

// End of algorithm

    printf("\n\n=======  SORTED ELEMENTS  =======\n\n");
    for(s=0;s<size;s  )
        printf("Element [%d]  = %d \n",s,*(ptr s));

   free(ptr);
  }
  return 0;//added 
}

BTW, this was tested with size == 3 and

  ptr[0] = 3;
  ptr[1] = 2;
  ptr[2] = 1;

along with several other odd and even counts of random values

  • Related