0

我不明白为什么这段代码不太正确。它对数组的大部分进行排序,但有些数字不合适。任何帮助将非常感激。

int partition(int* an_array, int from, int to)
{
    int pivot = an_array[(from + to) / 2];
    int left = from;   
    int right = to;   
    while(left < right)
    {
        while(an_array[left] < pivot)
        {
            left = left + 1; 
        }//end while loop       
        while(an_array[right] > pivot)
        {
            right = right - 1; 
        }//end while
        if(left < right)
        {
            swap_numbers(an_array[left], an_array[right]);
            left = left + 1; 
            right = right - 1; 
        }//end if 
    }//end while loop
    return right; 
}//end partition function 


void quick_sort(int* an_array, int from, int to)
{
    if(from >= to) return; 
    int p = partition(an_array, from, to); 
    quick_sort(an_array, from, p); 
    quick_sort(an_array, p + 1, to); 
}//end quick_sort
4

1 回答 1

0

您忘记添加:

swap_numbers(an_array[from], an_array[right]);

就在之前

return right;
于 2013-10-13T21:02:01.270 回答