【问题标题】:problem with dynamic array and pointers in selection sort选择排序中的动态数组和指针问题
【发布时间】:2021-11-26 17:17:52
【问题描述】:

我使用动态数组和指针制作了一个选择排序程序,但是在运行此代码后,我发现如果我们给出像 4 和 6 这样的大小输入,则数组正在排序,但如果大小输入像 5,则不能正确排序和7等......在此之前我也使用相同的指针和动态数组技术进行了冒泡排序程序,但它在所有条件下都给出了完美的排序数组,我也尝试调试代码但仍然不明白为什么会这样如果有人对此有所了解,请帮助我。

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


  }

}

【问题讨论】:

  • 不是核心问题,但在C 这个演员表是错误的:ptr = (int *)(calloc (size,sizeof(int))); 使用ptr = calloc (size,sizeof(int));
  • @ryyker。据我所知,这是不必要的,不一定是完全错误的
  • 如果原始数组是3, 2, 1,循环的第一次迭代交换2和3给你2,3,1。下一次迭代交换13,但1 永远不会替换2。你的算法不正确。

标签: arrays c sorting pointers dynamic-arrays


【解决方案1】:

清除一些不必要的混淆因素...

*(ptr + index)

等同于

ptr[index]

但第二个更容易阅读。

接下来,在下一节中引入变量min

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

...但对于简单的排序不是必需的。只需坚持使用ij,无论是偶数集还是奇数集,排序都会正确进行。最后,为了消除内存泄漏,当不再需要ptr 时调用free(ptr);。以下是清理后的版本,有更正。

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 
}

顺便说一句,这是用 size == 3 和

测试的
  ptr[0] = 3;
  ptr[1] = 2;
  ptr[2] = 1;

以及其他几个奇数和偶数的随机值

【讨论】:

    【解决方案2】:

    您的Selection Sort 算法似乎是错误的。内部for循环完成迭代后,您必须将当前索引中的元素替换为最小值:

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

    现在,它应该适用于所有输入尺寸。

    【讨论】:

    • 不客气。
    猜你喜欢
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2013-10-31
    • 1970-01-01
    • 2017-09-24
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多