#include <stdio.h>
#define ARRAY_SIZE 10
void lomuto (int A[], int l, int r, int smallerAtLeft)
{
if (smallerAtLeft == 1) //move elements smaller than pivot to the left and the greater ones to the right
{
int tmp, tmp2,pivot,i,j;
pivot = A[r];
i = l-1;
for (j =0; j<r-1; j++)
{
if (A[j] <= pivot)
{
i++;
tmp = A[i];
A[i] = A[j];
A[j] = tmp;
}
}
tmp2 = A[i+1];
A[i+1] = A[r];
A[r] = tmp2;
}
if (smallerAtLeft == 0) //move elements smaller than pivot to the right and the greater ones to the left
{
int tmp3, tmp4,pivot,i,j;
pivot = A[r];
i = l-1;
for (j=0; j<r-1; j++)
{
if (A[j]>= pivot)
{
i++;
tmp3 = A[i];
A[i] = A[j];
A[j] = tmp3;
}
}
tmp4 = A[i+1];
A[i+1] = A[r];
A[r] = tmp4;
}
}
void quicksort (int A[], int l, int r, int ascending)
{
lomuto (A,l,r,ascending);
}
int main()
{
int testarray;
int testArray[ARRAY_SIZE] = {4, 2, 5, 3, 6, 7, 8, 1, 0};
quicksort (testarray,0,8,1);
return testarray;
}
晚上好。 通常我會搜索幾乎每一個論壇,並在我的代碼中尋找最深的線索。 但是這一次我沒有找到可以幫助我的答案。如果有人能告訴我爲什麼code-exe停止工作,但在編譯過程中屏幕上沒有顯示錯誤,我會非常感激。 我們必須實現lomuto分區的快速排序算法。如果變量「smallerAtLeft」等於1,則數組應該按增加的屬性排序,並且如果它等於0則遞減。C-Lomuto Quicksort Exe不起作用
此外,我們必須實現無效的功能,就像您在代碼中看到的一樣。包含lomuto的「lomuto-fct」和「quicksort-fct」。
也許這個反向Lomuto線程會幫助其他一些人也對未來..
你有2個變量拼寫不同。你傳遞錯誤1.這應該會導致編譯器錯誤。 –
你對你顯示的代碼有什麼問題?你想問什麼問題?請花些時間閱讀[幫助頁面](http://stackoverflow.com/help),尤其是名爲[「我可以問些什麼話題?」]的章節(http://stackoverflow.com/help/)討論話題)和[「我應該避免問什麼類型的問題?」](http://stackoverflow.com/help/dont-ask)。另請[請閱讀如何提出好問題](http://stackoverflow.com/help/how-to-ask)。 –
@JohnnyMopp你的意思是什麼變數?我和j? –