2017-03-31 67 views
0
#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線程會幫助其他一些人也對未來..

+0

你有2個變量拼寫不同。你傳遞錯誤1.這應該會導致編譯器錯誤。 –

+0

你對你顯示的代碼有什麼問題?你想問什麼問題?請花些時間閱讀[幫助頁面](http://stackoverflow.com/help),尤其是名爲[「我可以問些什麼話題?」]的章節(http://stackoverflow.com/help/)討論話題)和[「我應該避免問什麼類型的問題?」](http://stackoverflow.com/help/dont-ask)。另請[請閱讀如何提出好問題](http://stackoverflow.com/help/how-to-ask)。 –

+0

@JohnnyMopp你的意思是什麼變數?我和j? –

回答

0

我不認爲你明白從main返回值是什麼,它的用途。它通常是成功和失敗的指標,典型值0取得成功,失敗率小。在<stdlib.h>頭文件中甚至有爲此定義的宏:EXIT_SUCCESS and EXIT_FAILURE

如果你想看到你所需要的排序數組打印它:

printf("Sorted array = {"); 
for (unsigned i = 0; i < ARRAY_SIZE; ++i) 
{ 
    printf(" %d", testArray[i]); 
} 
printf(" }\n"); 

當然,這需要你的實際數組傳遞給你的排序功能。

+0

謝謝先生,我真的很感謝你的幫助!..我很抱歉我在C中的不好的知識.. –