具有Hoare分区方案的Quicksort算法返回原始未排序列表

问题描述 投票:1回答:2

我尝试使用Hoare的分区方案实现Quicksort算法,但是当我在测试列表{412, 123, 57, 12, 1, 5}上运行它然后将其打印出来时,我按原始顺序获取数字。有人能帮助我发现我做错了吗?以下是我的实施。

void Quicksort::sort(std::vector<int> &list)
{
   sort(list, 0, list.size() - 1);
}

void Quicksort::sort(std::vector<int> &list, int left, int right)
{
   if (left - right <= 1) return; // no need to partition a single element
   int pivot = left + (right - left) / 2; // <=> (left+right)/2, but avoids overflow
   int endIndex = partition(list, pivot, left, right); 
   sort(list, 0, endIndex - 1);
   sort(list, endIndex + 1, list.size() - 1);
}

int Quicksort::partition(std::vector<int> &list, int pivot, int left, int right)
{
   while (true)
   {
      while (list[left] < list[pivot])
         left++;
      while (list[right] > list[pivot])
         right--;
      if (left != right)
         std::swap(list[left], list[right]);
      else
         return left;
   }
}

要在列表{412, 123, 57, 12, 1, 5}上调用Quicksort算法,我使用以下代码:

std::vector<int> numbers = {412, 123, 57, 12, 1, 5};
Quicksort::sort(numbers);
for (int i = 0; i < numbers.size(); i++)
   std::cout << numbers[i] << "\n";

控制台输出是

412
123
57
12
1
5

编辑

在修复了应该是if (left - right <= 1)的bug if (right - left <= 1)后,程序遇到错误Segmentation fault: 11。这让我相信我正在尝试访问一些超出范围的东西。

c++ algorithm sorting quicksort
2个回答
4
投票

算法的分区部分没有以正确的方式实现。特别是,left可能会变得比right更大

if (left != right)
     std::swap(list[left], list[right]);
//             ^^^^^^^^^^

可以访问超出范围的向量。

请看以下代码段:

int partition(std::vector<int> &list, int left, int right)
{
   // I'm calculating the pivot here, instead of passing it to the function
   int pivot = list[left + (right - left) / 2];
   while (true)
   {
      while (list[left] < pivot)
         left++;
      while (list[right] > pivot)
         right--;

      // Stop when the pivot is reached 
      if (left >= right)
         return right;

      // Otherwise move the elements to the correct side 
      std::swap(list[left], list[right]);
   }
}

void sort(std::vector<int> &list, int left, int right)
{
    // Execute only if there are enough elements
   if (left < right) 
   {
       int pivot = partition(list, left, right);

       // As NiVer noticed, you have to limit the range to [left, right]
       sort(list, left, pivot - 1); 
       sort(list, pivot + 1, right);
   }
}

可测试的HERE

考虑使用迭代器以更一般的方式实现这些功能。


2
投票

我相信代码的问题(或至少一个问题)是这样的:

sort(list, 0, endIndex - 1);
sort(list, endIndex + 1, list.size() - 1);

这始终考虑整个列表,而不仅仅是剩余未分类的部分。您应该使用限制索引leftright

sort(list, left, endIndex - 1);
sort(list, endIndex + 1, right);
© www.soinside.com 2019 - 2024. All rights reserved.