First, you must have a certain understanding of the principles of quick sorting. First, you should look at the C language quick sorting code.
void sort(int a[],int size){int i=0,j=size-1,value=a[0];if(size>1){while(i
value){a[j]=a[i];break;}}}a[i]=value;sort(a,i);sort(&a[i+1],size-i-1);}}
Analyze the internal cycle:
While (I
Value) {a [j] = a [I]; break ;}}}
The function of this loop is to place a small number on the left side and a large number on the right side.
Now assume that the initial value is 6 9 7 3 4, j = 4, I = 0. Take 6 as the comparison value and place less than 6 on the left of data 6, put more than 6 on the Right of 6,
Take 6 (that is, the value of a [0]) as the comparison value. When we execute the first loop body:
(The first for loop) from the right to the left to find 6 smaller, the initial value of 6 9 7 3 4, we can find that 4 is smaller than 6, SO 4 is assigned to a [0], some may wonder if the value of a [0] will be overwritten. In fact, at the beginning, 6 has already been assigned the value. You can also find it by code. So the original value is equal to the position of 4, that is, a [4] (the yellow bottom of the third line). This is a "pitfall", which seems to have stored data, however, this data is useless because its value has been saved to a [0] (the original value of a [0] is saved to value ). The array element is 4 9 7 3 4.
(The second for loop) is greater than 6 from left to right, and the elements in the last execution result array are changed to 4 9 7 3 4 4 9 to 6, it is to put 9 in the pit above. The location where the 9 was originally located, that is, a [1], is empty (although it contains useless data) and becomes a pitfall.
The loop body is successfully executed once.
Next, execute the second loop. At the end, the final slot is a [2].
After the while LOOP body is executed, I will be equal to j. This area deserves your attention. And I = 2, which is the location of the slot. After the loop body is executed, a [2] = value;
If no, the array element 4, 3, 6, 7, and 9 takes 6 as nodes. This loop completes the task successfully.
Next we will take 4 3 as the new array, and 7 9 as the new array. Then recursion continues until only one element is separated.
Primary school students have limited abilities.