/ /クイックソートプログラムが機能しない[終了]-c、アルゴリズム、gridview-sorting

クイックソルトプログラムが動作しない? [閉じる] - c、アルゴリズム、グリッドビューソート

これは、クイックソートアルゴリズム。しかし、実行すると、何も表示されずに永遠に続きます。私は問題を見つけようとしましたが、今は疲れすぎています。助けてください。

#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;
}

回答:

回答№1は0

おそらく(テストしません)

すべきである

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;
}

回答№2の場合は1

テスト

if (left < right)

変更しないため、常に真になります。 また 変数(値によって他の関数に渡すため、コピーを変更します)。

このテストは、変更されないleft / rightの値で再帰的に実行しています。

PS .:これがあなたのプログラム/アルゴリズムの唯一の問題かどうかわかりません