Fast sorting algorithm

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.