Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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_Gridview Sorting - Fatal编程技术网

C 快速排序程序不工作?

C 快速排序程序不工作?,c,algorithm,gridview-sorting,C,Algorithm,Gridview Sorting,这应该是快速排序算法的一个实现。但当我运行它时,它会一直运行,不会显示任何内容。我曾试图找出问题,但现在太累了。请帮忙 #include <stdio.h> void quicksort(int arr[], int pivotIndex,int right); int partition(int a[],int left,int right); int main() { int arr[5] = {5, 4,2, 3, 6}; int left = 0;

这应该是快速排序算法的一个实现。但当我运行它时,它会一直运行,不会显示任何内容。我曾试图找出问题,但现在太累了。请帮忙

#include <stdio.h>

void quicksort(int arr[], int pivotIndex,int right);
int partition(int a[],int left,int right);

int main()
{
   int arr[5] = {5, 4,2, 3, 6};    
   int left = 0;
   int right = 4;

   quicksort(arr, left, right);

   for (int i = 0; i < 5; i++)
   {
       printf("%d ", arr[i]);
   }
   return 0;    
}

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

int partition(int a[],int left,int right)
{

    int i = left;
    int j = right;
    int pivotIndex = left;
    int temp;

    while (i < j)
    {
        while (a[pivotIndex] <=a[j])
        {
            j--;
        }
        if (a[pivotIndex] > a[j])
        {
            temp = a[pivotIndex];
            a[pivotIndex] = a[j];
            a[j] = temp;
            pivotIndex = j;
        }

        while (a[pivotIndex] <= a[j])
        {
            i++;
        }
        if (a[pivotIndex] < a[j])
        {
            temp = a[pivotIndex];
            a[pivotIndex] = a[j];
            a[j] = temp;
            pivotIndex = i;
        }        
    }
    return pivotIndex;
}
#包括
无效快速排序(int arr[],int pivotIndex,int right);
int分区(inta[],int左,int右);
int main()
{
int-arr[5]={5,4,2,3,6};
int左=0;
int右=4;
快速排序(arr、左、右);
对于(int i=0;i<5;i++)
{
printf(“%d”,arr[i]);
}
返回0;
}
无效快速排序(int-arr[],int-left,int-right)
{
if(左<右)
{
int pivotNewIndex=分区(arr,左,右);
快速排序(arr,左,pivotNewIndex-1);
快速排序(arr,pivotNewIndex+1,右);
}
}
int分区(int a[],int左,int右)
{
int i=左;
int j=右;
int pivotIndex=左;
内部温度;
而(i
if (left < right)
if(左<右)
将始终为真,因为您从不修改左变量或右变量(您通过值将它们传递给其他函数,因此您正在修改副本)

您使用永远不变的左/右值递归地执行此测试

注:我不知道这是否是你的程序/算法的唯一问题(我不测试)

应该是

while (i<j && a[pivotIndex] <=a[j])
{
    j--;
}
if (a[pivotIndex] > a[j])
{
    temp = a[pivotIndex];
    a[pivotIndex] = a[j];
    a[j] = temp;
    pivotIndex = j;
}

while (i<j && a[pivotIndex] >= a[i])
{
    i++;
}
if (a[pivotIndex] < a[i])
{
    temp = a[pivotIndex];
    a[pivotIndex] = a[i];
    a[i] = temp;
    pivotIndex = i;
}

while(我认为这很有效)。