简体   繁体   English

用C编程的快速排序

[英]Quick sort programmed in C

I am reading ANSI C by K&R. 我正在阅读K&R的ANSIC。 I came across the qsort program. 我遇到了qsort程序。 I want a little help. 我需要一点帮助。 Suppose I have 9 elements with index 0->8. 假设我有9个索引为0-> 8的元素。 Please read the comments to see if I am understanding it correct or not. 请阅读评论,看看我是否理解正确与否。 Thanks a lot for you efforts 非常感谢您的努力

void qsort(int v[] , int left, int right)
{
int i, j, last;
void swap(int v[], int i, int j);   

if(left >= right)               /*if the array has only one element return it*/
      return;
swap(v,left, (left+right)/2); /* now, left=(left+right)/2= 0+8/2= 4 we have 4 as left*/
last= left;   /* putting left = last of the first partition group i.e. last=4*/

for(i=left+1; i<=right,i++)  /* now looping from 4+1=5 to 8 with increment of 1*/
     if(v[i] < v[left])       /*if value at 5th is less than value at 4th */
          swap(v, ++last, i);  

I have problem in this last swap step. 我在最后的交换步骤中遇到问题。 As my values suggest swap ++4 ie to i ie 4+1= 5 (swapping 5 position with 5?). 正如我的价值观所示,将++ 4换为i即4 + 1 = 5(用5交换5位)。 How can I understand this? 我该怎么理解? There must be a swapping between 4 and 5, not 5 and 5 is it? 必须在4和5之间进行交换,不是5和5?

code continues 代码继续

swap(v,left, last);
qsort(v,left,last-1);
qsort(v,last+1,right);
}

Firstly, you have a small misconception about the swap function. 首先,您对交换功能有一个小的误解。 Let's say the prototype of the function is - 假设该函数的原型是-

swap(int array[], int i, int j)

The swap function swaps the numbers at location array[i] and array[j]. swap函数在位置array [i]和array [j]处交换数字。 So, the swap function swaps the elements in the array. 因此,swap函数交换数组中的元素。 So, the line - 所以,线-

swap(v, left, (left + right) / 2);

Means that, the middle element in the array is swapped with the leftmost element. 表示数组中的中间元素与最左边的元素交换。 Clearly, the quicksort is taking the middle element as the pivot. 显然,快速排序将中间元素作为关键。 This swap has no effect on the local variables or parameters. 此交换对局部变量或参数没有影响。 According to your data input example, the value of 'left' = 0, and the value of right = '8', even after the swapping. 根据您的数据输入示例,即使交换后,“ left”的值也等于0,right的值等于“ 8”。 This is where you got confused. 这是您感到困惑的地方。 The elements of array are swapped, not the values of variables. 数组的元素被交换,而不是变量的值。 So, now, the line - 所以,现在,这条线-

last = left;

makes, 'last' point to the location of the pivot ('left'), so here the value of 'last' = 0 not 4. So, the loop, 使得'last'指向枢轴的位置('left'),所以这里'last'的值= 0而不是4。因此,循环

for(i = left + 1; i <= right; i++) 

Runs from i = 1 to 8. BTW, you forgot the semicolon..! 从i = 1到8。顺便说一句,您忘记了分号..! Then, the line, 然后,线,

if(v[i] < v[left])

checks if the current element ('v[i]') is less than the pivot ('v[left]') or not. 检查当前元素('v [i]')是否小于枢轴('v [left]')。 Then, accordingly swaps the lesser elements as in the line, 然后,相应地交换行中的较小元素,

 swap(v, ++last, i);

from the location (last + 1) till where ever it increments to. 从位置(最后+1)到它增加到的位置。 So, the elements to the left of 'last' are less than pivot and the elements to the right are greater. 因此,“最后一个”左侧的元素小于枢轴,而右侧的元素则更大。 I think you are missing another line, where we bring the pivot back to the middle which was at the location 'v[left]' during the execution of the algorithm. 我认为您遗漏了另一行,在执行算法的过程中,我们将枢轴移回到了中间位置[v [left]'。 Then, the recursive calls play their roles. 然后,递归调用发挥作用。 If you are looking for help with quicksort, this is a good place to start ! 如果您正在寻求Quicksort的帮助, 是一个不错的起点!

I hope my answer has helped you, if it did, let me know..! 希望我的回答对您有所帮助,如果有,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM