【问题标题】:C++ - Implementing quicksort using unsigned integers only?C++ - 仅使用无符号整数实现快速排序?
【发布时间】:2014-01-28 18:13:56
【问题描述】:

我有

std::vector<unsigned int> numbers;

那是用数字填充的,需要在不使用 int 索引的情况下快速排序 - 只允许 unsigned int 作为向量索引。

当我将所有“int”转换为“unsigned int”时,我看到的所有快速排序示例都会中断 因为在某些情况下索引可能为 -1(由于 j--)。

编辑:这是一个例子

void quickSort(std::vector<unsigned int> &numbers, unsigned int left, unsigned int right) {
        unsigned int i = left, j = right;
        unsigned int tmp;
        unsigned int pivot = numbers.size()/2;

        /* partition */
        while (i <= j) {
              while (numbers[i] < pivot)
                    i++;
              while (numbers[j] > pivot)
                    j--;
              if (i <= j) {
                    tmp = numbers[i];
                    numbers[i] = numbers[j];
                    numbers[j] = tmp;
                    i++;
                    j--;
              }
        };

        /* recursion */
        if (left < j)
              quickSort(numbers, left, j);
        if (i < right)
              quickSort(numbers, i, right);
  }

修改版:http://diogopinho.hubpages.com/hub/easy-quicksort-with-examples

Segfaults 上面的示例,因为如果 j 是 unsigned int 并变为 0,则 j-- 变为一个巨大的数字 (0xffffffff) 并且 (i

【问题讨论】:

  • 如果您看到错误的来源,您应该能够自己解决问题。不看代码就很难猜到。
  • 我会这样做: 1. 给自己一个 C++ 标准库实现,其中std::sort 被实现为快速排序; 2.std::sort(numbers.begin(), numbers.end());
  • 什么是0-1的情况?显示您无法解决如何替换的特定代码。我没有立即明白为什么 QuickSort 实现会在任何地方使用负数。
  • quicksort()需要有符号值的理由为零。如果你没有找到任何东西,那么你 (a) 看起来不够努力,并且 (b) 自己从未尝试过。因此,请展示 是如何做到的,以及如何无法使用 unsigned s。只。此外,给定 std::vector&lt;&gt; 并且不允许使用 std::sort&lt;&gt; 我会首先使用迭代器来实现它。
  • 如果您的参考代码通过减去它们来比较 int,那么它做错了,因为有符号算术的行为在溢出时是未定义的。

标签: c++ algorithm sorting quicksort


【解决方案1】:

如果您查看链接到的包含枢轴描述的页面,则会错误地实现它。这会导致找不到主元并且 j 小于 0。如果主元被正确选择为包含在该范围内的数字,我认为该算法也适用于无符号整数。

【讨论】:

    【解决方案2】:

    这个帖子很老了,但是如果有人仍然需要,他可以找到一个可以容忍无符号整数索引的实现here

    int partition(int *a,int start,int end)
    {
        int pivot=a[end];
        //P-index indicates the pivot value index
    
        int P_index=start;
        int i,t; //t is temporary variable
    
        //Here we will check if array value is 
        //less than pivot
        //then we will place it at left side
        //by swapping 
    
        for(i=start;i<end;i++)
        {
            if(a[i]<=pivot)
            {
                t=a[i];
                a[i]=a[P_index];
                a[P_index]=t;
                P_index++;
            }
         }
         //Now exchanging value of
         //pivot and P-index
          t=a[end];
          a[end]=a[P_index];
          a[P_index]=t;
    
         //at last returning the pivot value index
         return P_index;
     }
     void Quicksort(int *a,int start,int end)
     {
        if(start<end)
        {
             int P_index=partition(a,start,end);
                 Quicksort(a,start,P_index-1);
                 Quicksort(a,P_index+1,end);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-05
      • 2011-03-29
      • 2017-02-18
      • 2017-05-09
      • 1970-01-01
      • 2019-11-20
      • 1970-01-01
      相关资源
      最近更新 更多