Ask a quick sort question to reply to the discussion (solution)
$ Base_num will not be overwritten,
This is related to the php variable scope. the variables in the function are valid only in the function.
That is to say, $ base_num in quick_sort of recursive calls is independent.
This is different from js. Of course, the local variable can be declared using var in js.
Each execution is a local variable in the function, which is changed unless it is a static or global variable.
$ Base_num will not be overwritten,
This is related to the php variable scope. the variables in the function are valid only in the function.
That is to say, $ base_num in quick_sort of recursive calls is independent.
This is different from js. Of course, the local variable can be declared using var in js.
You mean, for example, 3, 1, 5, 4.
Wear 3 for the first time and take it out as a ruler, 4 to compare it with it, less than it in the left array, greater than it in the right array, $ left = array (1 ); $ right = array (5, 4 );
When $ right recurses again, take 5 out as the ruler and compare 4 with it. The value 3 and 5 of the scale can last in the last line of code.
Return array_merge ($ left_array, array ($ base_num), $ right_array.
This is clear.
Print_r (quick_sort (array (6, 1, 5, 8, 7, 4, 2, 3, 0); function quick_sort ($ arr) {// determine whether to continue with $ length = count ($ arr); if ($ length <= 1) {return $ arr;} // if no return is returned, it indicates that more than one element in the array needs to be sorted // select a ruler // select the first element $ base_num = $ arr [0]; // You can traverse all elements except the ruler, put two arrays according to the size relationship // Initialize two arrays $ left_array = array (); // $ right_array = array () smaller than the ruler (); // for ($ I = 1; $ I <$ length; $ I ++) {if ($ base_num> $ arr [$ I]) {// put the left array $ left_array [] = $ arr [$ I];} else {// put the right array $ right_array [] = $ arr [$ I];} printf ("in [% s] B [% d] L [% s] R [% s]
", Join (',', $ arr), $ base_num, join (',', $ left_array), join (',', $ right_array )); // sort the array on the left and right respectively. // call this function recursively and record the result $ left_array = quick_sort ($ left_array ); $ right_array = quick_sort ($ right_array); // merge the left, ruler, and right return array_merge ($ left_array, array ($ base_num), $ right_array );}
in[6,1,5,8,7,4,2,3,0] B[6] L[1,5,4,2,3,0] R[8,7]in[1,5,4,2,3,0] B[1] L[0] R[5,4,2,3]in[5,4,2,3] B[5] L[4,2,3] R[]in[4,2,3] B[4] L[2,3] R[]in[2,3] B[2] L[] R[3]in[8,7] B[8] L[7] R[]Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 )
This is clear.
Print_r (quick_sort (array (6, 1, 5, 8, 7, 4, 2, 3, 0); function quick_sort ($ arr) {// determine whether to continue with $ length = count ($ arr); if ($ length <= 1) {return $ arr;} // if no return is returned, it indicates that more than one element in the array needs to be sorted // select a ruler // select the first element $ base_num = $ arr [0]; // You can traverse all elements except the ruler, put two arrays according to the size relationship // Initialize two arrays $ left_array = array (); // $ right_array = array () smaller than the ruler (); // for ($ I = 1; $ I <$ length; $ I ++) {if ($ base_num> $ arr [$ I]) {// put the left array $ left_array [] = $ arr [$ I];} else {// put the right array $ right_array [] = $ arr [$ I];} printf ("in [% s] B [% d] L [% s] R [% s]
", Join (',', $ arr), $ base_num, join (',', $ left_array), join (',', $ right_array )); // sort the array on the left and right respectively. // call this function recursively and record the result $ left_array = quick_sort ($ left_array ); $ right_array = quick_sort ($ right_array); // merge the left, ruler, and right return array_merge ($ left_array, array ($ base_num), $ right_array );}
in[6,1,5,8,7,4,2,3,0] B[6] L[1,5,4,2,3,0] R[8,7]in[1,5,4,2,3,0] B[1] L[0] R[5,4,2,3]in[5,4,2,3] B[5] L[4,2,3] R[]in[4,2,3] B[4] L[2,3] R[]in[2,3] B[2] L[] R[3]in[8,7] B[8] L[7] R[]Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 )
This is clear.
Print_r (quick_sort (array (6, 1, 5, 8, 7, 4, 2, 3, 0); function quick_sort ($ arr) {// determine whether to continue with $ length = count ($ arr); if ($ length <= 1) {return $ arr;} // if no return is returned, it indicates that more than one element in the array needs to be sorted // select a ruler // select the first element $ base_num = $ arr [0]; // You can traverse all elements except the ruler, put two arrays according to the size relationship // Initialize two arrays $ left_array = array (); // $ right_array = array () smaller than the ruler (); // for ($ I = 1; $ I <$ length; $ I ++) {if ($ base_num> $ arr [$ I]) {// put the left array $ left_array [] = $ arr [$ I];} else {// put the right array $ right_array [] = $ arr [$ I];} printf ("in [% s] B [% d] L [% s] R [% s]
", Join (',', $ arr), $ base_num, join (',', $ left_array), join (',', $ right_array )); // sort the array on the left and right respectively. // call this function recursively and record the result $ left_array = quick_sort ($ left_array ); $ right_array = quick_sort ($ right_array); // merge the left, ruler, and right return array_merge ($ left_array, array ($ base_num), $ right_array );}
in[6,1,5,8,7,4,2,3,0] B[6] L[1,5,4,2,3,0] R[8,7]in[1,5,4,2,3,0] B[1] L[0] R[5,4,2,3]in[5,4,2,3] B[5] L[4,2,3] R[]in[4,2,3] B[4] L[2,3] R[]in[2,3] B[2] L[] R[3]in[8,7] B[8] L[7] R[]Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 7 [8] => 8 )
This is intuitive! Thank you ~