today I read the legendary quick sort on the internet, and then try to implement it recursively in C #: (This is descending)
voidMain () {int[] arraytosort=New[]{1,1,2,3,5,6,7, -,Ten, -,3, +,1,0,5,2,4,3}; Helper Helper=NewHelper (); varresult=Helper. Splitarray (Arraytosort); stringshow=string. Join (",", result. Select (r=>r.tostring ()). ToArray ()); Console.WriteLine (show);} Public classhelper{ Public int[] Splitarray (int[] Array) { stringshow=string. Join (",", array. Select (r=>r.tostring ()). ToArray ()); Console.WriteLine ("Initial array:"+show); if(Array. length<=1){ returnArray; } intPosition=array. length/2; varvaluep=Array[position]; Array[position]=array[0]; array[0]=Valuep; Show=string. Join (",", array. Select (r=>r.tostring ()). ToArray ()); Console.WriteLine ("For the first time manually swapping results:"+show); intcompareposition=0; for(inti=compareposition+1; I<array. length;i++) {Console.WriteLine ("the baseline values to compare at this time are:"+array[compareposition]+"the base value subscript compareposition is:"+compareposition+"/Comparison values are:"+array[i]+"The comparison value subscript i is"+i); if(array[compareposition]<Array[i]) {Console.WriteLine ("base value less than comparison value swap position"); varTempvalue=Array[i]; for(intj=i;j>compareposition;j--) {Array[j]=array[j-1]; } Array[compareposition]=Tempvalue; Compareposition=compareposition+1; Show=string. Join (",", array. Select (r=>r.tostring ()). ToArray ()); Console.WriteLine ("results after position change:"+show); } Else{Console.WriteLine ("base value is greater than or equal to the comparison value does not change position"); }} Show=string. Join (",", array. Select (r=>r.tostring ()). ToArray ()); Console.WriteLine ("The result of grouping is larger than the base value"+show+"at this point compareposition:"+compareposition); int[] Arraysmall=New int[Array. length-1-Compareposition]; Console.WriteLine ("the length of the Arraysmall is:"+arraysmall.length); int[] arraylarge=New int[compareposition]; if(arraylarge.length>0){ for(intI=0; i<arraylarge.length;i++) {Arraylarge[i]=Array[i]; } } if(arraysmall.length>0){ for(intI=0; i<arraysmall.length;i++) {Arraysmall[i]=array[compareposition+1+i]; Console.WriteLine ("the I of Arraysmall is:"+Arraysmall[i]); } } vararrayleft=Splitarray (Arraylarge); vararrayright=Splitarray (Arraysmall); varresult=New int[Array. Length]; Array.copy (Arrayleft,0, result,0, arrayleft.length); Result[arrayleft.length]=Valuep; Array.copy (Arrayright,0, result,arrayleft.length+1, arrayright.length); returnresult; }
Sure enough himself or vegetables, debugging a half-day only to adjust, do not know the list is not better
Fast sorting algorithm