快速排序帮助,不知道为什么分区返回索引而不是数组

发布于 2024-11-05 17:04:20 字数 1039 浏览 3 评论 0原文

我想知道是否有人可以帮助我进行快速排序。我了解分区的总体思路,但不确定为什么它返回索引

int partition(int arr[], int left, int right)
{
      int i = left, j = right;
      int tmp;
      int pivot = arr[(left + right) / 2];

      while (i <= j) {
            while (arr[i] < pivot)
                  i++;
            while (arr[j] > pivot)
                  j--;
            if (i <= j) {
                  tmp = arr[i];
                  arr[i] = arr[j];
                  arr[j] = tmp;
                  i++;
                  j--;
            }
      }

      return i;
}

void quickSort(int arr[], int left, int right) {
      int index = partition(arr, left, right);
      if (left < index - 1)
            quickSort(arr, left, index - 1);
      if (index < right)
            quickSort(arr, index, right);
}

我了解整个重新排列部分。这对我来说很有意义,但我不确定为什么分区只返回一个索引。我认为它应该返回一个数组?就像问题是...排序 {1, 12, 5, 26, 7, 14, 3, 7, 2}。我以为它会返回...

1, 2, 5, 7, 3, 14, 7, 26, 12

我想这就是为什么我不理解实际的快速排序功能。但如果有人能以一种易于理解的方式清楚地解释它,我们将不胜感激。多谢!

i was wondering if anyone could help me on quicksort. i understand the general idea for partitioning, but not sure why it returns an index

int partition(int arr[], int left, int right)
{
      int i = left, j = right;
      int tmp;
      int pivot = arr[(left + right) / 2];

      while (i <= j) {
            while (arr[i] < pivot)
                  i++;
            while (arr[j] > pivot)
                  j--;
            if (i <= j) {
                  tmp = arr[i];
                  arr[i] = arr[j];
                  arr[j] = tmp;
                  i++;
                  j--;
            }
      }

      return i;
}

void quickSort(int arr[], int left, int right) {
      int index = partition(arr, left, right);
      if (left < index - 1)
            quickSort(arr, left, index - 1);
      if (index < right)
            quickSort(arr, index, right);
}

i understand the whole rearranging part. it makes sense to me, but im not sure why partition is returning just an index. i thought it was supposed to return an array? like if the problem was to ... Sort {1, 12, 5, 26, 7, 14, 3, 7, 2}. i thought it would return...

1, 2, 5, 7, 3, 14, 7, 26, 12

i guess thats why im not understanding the actual quicksort function. but if someone could help explain it clearly and in an easy to understand way, it would be much appreciated. thanks a lot!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

妄断弥空 2024-11-12 17:04:20

返回的索引仅用于标识快速排序算法中递归的结束。它主要是主元元素的索引,用于识别较小和较大的数字。

AND:您指的是增强的快速搜索算法。在 QuickSearch 算法的基本版本中,不需要返回的索引。

它也适用于:(但慢得多)

void quickSort(int arr[], int left, int right) 
{
      if (left < right)
      {
         int index = partition(arr, left, right);
         quickSort(arr, left, index - 1);
         quickSort(arr, index+1, right);
      } 
}

The index that has been returned is only there to identify the end of recurrsion within the QuickSort algorithm. It's mainly the index of the pivot element that is used to identify the smaller and bigger numbers.

AND: You are referring to an enhanced Quick Search Algorithm. In the basic version of the QuickSearch algorithm the returned index won't be needed.

It would also work with: (but a lot slower)

void quickSort(int arr[], int left, int right) 
{
      if (left < right)
      {
         int index = partition(arr, left, right);
         quickSort(arr, left, index - 1);
         quickSort(arr, index+1, right);
      } 
}
陌伤ぢ 2024-11-12 17:04:20

您的分区函数正在就地修改数组。它返回的整数是值小于主元之前的索引,以及从值大于主元的位置开始的索引。这两个递归调用分别对较小的元素和较大的元素进行排序;递归调用之前测试的条件作为基本情况。

Your partition function is modifying the array in-place. The integer it is returning is the index before which values are smaller than the pivot, and starting at which the values are bigger than the pivot. The two recursive calls are sorting the smaller elements and the larger elements; the condition tested before the recursive calls serves as the base case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文