Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 算法-快速排序的实现问题_C_Algorithm_Logic_Quicksort - Fatal编程技术网

C 算法-快速排序的实现问题

C 算法-快速排序的实现问题,c,algorithm,logic,quicksort,C,Algorithm,Logic,Quicksort,作为初学者,我正在学习算法。在学习快速排序时,我发现有几种方法可以实现快速排序。所以我选择了这种方法来实现快速排序 它选择数组末尾的元素作为轴,并选择第一个元素作为分隔小于或大于轴的值的墙。排序完成后,将轴插入墙的位置并进行分割 下面是我在c中的实现: void simpleqs(int* data,int start,int end) { int wall = start; int pivot = data[end]; //Base case if (start >= end)

作为初学者,我正在学习算法。在学习快速排序时,我发现有几种方法可以实现快速排序。所以我选择了这种方法来实现快速排序

它选择数组末尾的元素作为轴,并选择第一个元素作为分隔小于或大于轴的值的墙。排序完成后,将轴插入墙的位置并进行分割

下面是我在c中的实现:

void simpleqs(int* data,int start,int end)
{
 int wall = start;
 int pivot = data[end];

 //Base case
 if (start >= end)
 {
   return;
 }

 //Sort the array
 for(int e = start;e < end-1; e++)
 {
   if(data[e] < pivot)
   {
    swap(&data[e],&data[wall]);
    wall++;
   }
 }

 //Partition

 if(data[wall] >= pivot)
 {
   swap(&data[wall],&data[end]);
 }

 simpleqs(data,start,wall-1);
 simpleqs(data,wall+1,end);
 }

请帮我修改逻辑,谢谢

你应该走到终点,而不是终点-1

// Sort the array
for (int e = start; e < end; e++)
{
    if (data[e] < pivot)
    {
        swap(&data[e], &data[wall]);
        wall++;
    }
}
//对数组进行排序
对于(int e=start;e
您已经实现了,但是错过了对
(end-1)
-th元素的处理:

for(int e = start;e < end; e++)
for(int e=start;e
感谢您发现我犯的错误并告诉我该方法是什么!
// Sort the array
for (int e = start; e < end; e++)
{
    if (data[e] < pivot)
    {
        swap(&data[e], &data[wall]);
        wall++;
    }
}
for(int e = start;e < end; e++)