【问题标题】:Using a pointer for selection sort使用指针进行选择排序
【发布时间】:2015-04-10 01:26:04
【问题描述】:

我正在尝试使用指向数组的指针进行选择排序。

void sort(int size, int *ptr)
{
int temp;
bool swap;
do
{
    swap = false;
    for (int count = 0; count < (size - 1); count++)
    {
        if (*ptr[count] > *ptr[count + 1])
        {
            temp = *ptr[count];
            *ptr[count] = *ptr[count + 1];
            *ptr[count + 1] = temp;
            swap = true;
        }
    }
} while (swap);
}

我收到很多错误说方向非法,因为使用 * 时它必须是一个指针。我在其他方法中使用它很好,这只是它有问题的方法。这是我正在使用的电话。

sort(arraySize, numArray);

一切都被声明并在其他方法中工作。

【问题讨论】:

  • 您看到哪几行错误消息?也可能应该是ptr[count] 而不是*ptr[count]。 ptr 是一个指针,而不是一个指针数组。
  • 修复了,谢谢!
  • 所以将 *ptr[count] 更改为 ptr[count]。如果您使用数组下标,则不需要取消引用指针。在大多数情况下,可以使用 * 运算符或数组下标来取消引用指针。
  • 这看起来像冒泡排序,不是选择排序。对于选择排序,您只需查看元素即可遍历数组以找到最小的元素(尚未交换任何内容)。然后,当您到达数组的末尾,因此您在数组的未排序部分中找到了最小元素时,您将 one 从那里交换到第一个未排序的位置(然后有再排序一项)。
  • @BucketsOstuff 请考虑简单的错误,例如缺少分号。发这样的错误不应该有任何借口。

标签: c++ arrays sorting pointers selection-sort


【解决方案1】:

使用 ptr[] 而不是 *ptr[] 因为, ptr 是指针,如果与 [] 一起使用,则它会像数组一样返回该位置的元素。

void sort(int size, int *ptr)
{
int temp;
bool swap;
do
{
    swap = false;
    for (int count = 0; count < (size - 1); count++)
    {
        if (ptr[count] > ptr[count + 1])
        {
            temp = ptr[count];
            ptr[count] = ptr[count + 1];
            ptr[count + 1] = temp;
            swap = true;
        }
    }
} while (swap);
}

【讨论】:

    【解决方案2】:

    错误在*ptr[count] 这是指针解引用的错误语法。

    ptr[count]*(ptr + count)

    【讨论】:

      【解决方案3】:

      这是编译错误删除版本。

      void sort(int size, int *ptr)
      {
      int temp;
      bool swap;
      do
      {
          swap = false;
          for (int count = 0; count < (size - 1); count++)
          {
              if (ptr[count] > ptr[count + 1])
              {
                  temp = ptr[count];
                  ptr[count] = ptr[count + 1];
                  ptr[count + 1] = temp;
                  swap = true;
              }
          }
      } while (swap);
      }
      

      【讨论】:

        【解决方案4】:

        另请阅读:C++ Using pointers for selection sort function

        更多示例:http://www.codemiles.com/c-examples/c-selection-sort-t2916.html

        *ptr[count] 没有任何意义

        ptr - 指针 *ptr - 值 - 取消引用

        【讨论】:

          【解决方案5】:

          这是正确的结构 使用数组的指针表示法时不能使用*,使用不带'*'的指针名指的是指针数组的0索引

          void sort(int size, int *ptr)
          {
          int temp;
          bool swap;
          do
          {
              swap = false;
              for (int count = 0; count < (size - 1); count++)
              {
                  if (ptr[count] > ptr[count + 1])
                  {
                      temp = ptr[count];
                      ptr[count] = ptr[count + 1];
                      ptr[count + 1] = temp;
                      swap = true;
                  }
              }
          } while (swap);
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-05-06
            • 2023-03-19
            • 1970-01-01
            • 2013-10-31
            • 2021-05-21
            • 1970-01-01
            相关资源
            最近更新 更多