跪求大大幫我修改快速排序法的程式

void quicksort(int left,int right,int array[])

{

if(left<right)

{

int i,j,temp;

for( ; ; )

{

for(i=left+1;array[left]>array[i];i++);

for(j=right;array[j]>array[left];j--);

if(i>=j)

{

break;

}

else

{

temp = array[i]; array[i] = array[j]; array[i] = temp;

}

}

temp = array[j]; array[j] = array[left]; array[left] = temp;

quicksort(left,j-1,array);

quicksort(j+1,right,array);

}

}

請問到底哪裡錯了 完全看不出來~"~

2 Answers

Rating
  • 1 decade ago
    Favorite Answer

    void quicksort(int left,int right,int array[])

    {

    if(left<right) // line 2 – bad style. Change to if(left >=right) return;

    {

    int i,j,temp;

    for( i=1+left, j=right;i<=j; ) // line 4

    // move i=1+left from line 5 to here

    // move j=right from line 6 to here

    // add loop condition i<=j

    {

    for(i=left+1;array[left]>=array[i];i++); // line 5

    // move i=1+left to line 4;

    // change > to >=

    // need to range check i

    // I would use while loop instead

    // suggestion: while(i<= j && array[left]>=array[i])++i;

    for(j=right; array[j]>array[left];j--); // line 6

    // move j=right to line 4

    // need to range check j

    // I would use while loop instead

    // suggestion: while(i<=j && array[j]>array[left])--j;

    if(i>=j) {break;}else{ // line 7

    // suggestion move the condition check to line 4

    // change condition from i>=j to i>j

    temp = array[i]; array[i] = array[j]; array[i] = temp;

    }

    }

    If(j > left) { // missing

    temp = array[j]; array[j] = array[left]; array[left] = temp;} // missing

    quicksort(left,j-1,array);

    quicksort(j+1,right,array);

    }

    }

  • re
    Lv 4
    1 decade ago

    for( ; ; )

    裡面是什麼完全不知道...

Still have questions? Get your answers by asking now.