Quick sort algorithm.

After talking about the sequence table, let's take a look at its application: quick sort algorithm.

The following is the algorithm on the data:

public void QuickSort(SeqList<int> sqList,int low, int high)
{
        int i=low;
        int j=high;
        int temp=sqList[low];
        while(low<high)
       {
              while(low<high && sqList[high]>=temp)
             {
                     --high;
             }
             sqList[low]=sqList[high]
             ++low
             while(low<high&&sql[low]<=temp)
             {
                   ++Low
             }
             sqList[high]=sqList[low];
            --high;
        }
       sqList[low]=temp;
 
       if(i<low-1)
      {
       QuickSort(sqList,i,low-1); 
      }
      if(j>low+1)
      {
             QuickSort(sqList,high+1,j);
      }


}

 

 

Here is the modified code:

 

public void QuickSort(SeqList<int> sqList,int low, int high)
{
        int i=low;
        int j=high;
        int temp=sqList[low];
        while(low<high)
       {
              while(low<high && sqList[high]>=temp)
             {
                     --high;
             }

             if(low<high)

             {
                  sqList[low]=sqList[high]
                  ++low

             }
             while(low<high&&sql[low]<=temp)
             {
                   ++Low
             }

            if(low<high)

            {
                   sqList[high]=sqList[low];
                   --high;

            }
        }
       sqList[low]=temp;
 
       if(i<low-1)
      {
       QuickSort(sqList,i,low-1); 
      }
      if(j>low+1)
      {
             QuickSort(sqList,high+1,j);
      }


}

 

It can be seen that only the two sentences after the inner while loop are added with an if judgment condition. What is this asking?

First look at the following test case:

Suppose the sqlist passed in is 1, 3, 2. If you use the code on the data, the result you will get is 3, 1, 2.

The reason is that when the first while statement of the second layer is executed after entering the while loop of the first layer, the end of while is caused by high==low, not because the condition of sqList[high]>=temp is not satisfied, that is, when At the end of this while statement, high==low points to the first element 1, exit the while loop and execute the following statements:

sqList[low]=sqList[high]
++low

Here sqList[low]=sqList[high] is correct (sqList[low]=sqList[high]=1), the error lies in this ++low, after ++low, sqList[high] remains unchanged or points to the first Element 1, but sqList[low] changes to point to the second element 3.

When the program enters the second while statement block, low points to the second position, but high points to the first position, that is, high<low does not meet the conditions for entering the second while statement block, skip the second statement block directly . The program continues to execute after the second while statement block

sqList[high]=sqList[low];
--high;

When sqList[high]=sqList[low] is executed, sqList[high]=sqList[low]=3; the value of sqList is 3, 3, 2. Continue to execute --high, high is out of the index range of sqList. At this time, the execution of the second layer is completed, the program returns to the first layer, and the judgment condition is low<high. From the previous analysis, it is obvious that the condition is not established, and the while statement of the first layer will also roll out. The program continues to execute

 sqList[low]=temp;
At this time, sqList becomes 3, 1, 2.

 

For the following two if statements, since low=i+1 and j=low+1 do not meet the conditions, they will be skipped directly, so that the entire algorithm is executed, and the result of the sorting is 3, 1, 2.

 

This will not happen for the modified code.

Guess you like

Origin blog.csdn.net/tianyueWindbg/article/details/5858744