Quick sort memory method

I will take the 863 exam tomorrow, and this time I will be a "model" beat...


Just remember two "cut-off" conditions for quicksort :

1. Loop cut-off condition: when low pointer == high pointer

2. Algorithm cutoff: when all divisions become single (one division determines the position of a sentinel)

The purpose of division : the places pointed by the low pointer are smaller than the sentinel R0, and the places pointed by the high pointer are larger than R0


It is easier to divide the memory process each time : ( this is only a part of the algorithm execution process, it is not the end )


Above, the wording of "empty" is added by individuals for everyone's understanding. In the actual process, it is just not dealt with, and it is just a waste of repetition (an eyesore and a hindrance to understanding)


In fact, so many words and so many explanations are just to say the purpose of the code. After all, this is the process that we can understand~ (I can’t remember if we don’t understand it, we are not machines that execute commands, we need to know The intent of each step and the "why you did it")


Attach the code, and mark the part I explained

#include <iostream> 
using  namespace  std; 
void  Qsort( int  a[],  int  low,  int  high)【"一次划分过程"】
{
     if (low >= high)直至元素“本尊归位”
     {
         return ;
     }
     int  first = low;
     int  last = high;
     int   key = a[first]; /*用字表的第一个记录作为枢轴*/ 【key就是我说的R0】
 
     while (first < last)
     { [ From the right, H is replaced by L once, and L is replaced by one cycle until L=H ]
         while (first < last && a[last] >= key) 【若H又(没跳出循环)交换过了L开始往右找不符合
         {
             --last;
         }
 
         a[first] = a[last]; /*将比第一个小的移到低端*/ 【这就是“H交换”的代码,其上部分为遍历比较】
 
         while (first < last && a[first] <= key)L交换过了,H开始往左找不符合
         {
             ++first;
         }
         
         a[last] = a[first];   /* Move the one larger than the first to the high end */ [This is the code of " L swap ", the upper part of which is the traversal comparison]
     }
     a[first] = key; /*枢轴记录到位*/【L==H,将R02赋值到LH的位置
     Qsort(a, low, first-1); 【例子里的{1},再次划分】
     Qsort(a, first+1, high); 【例子里的{6,5,3}再次划分】
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325938026&siteId=291194637