Why define two variables for quick sorting

Source: Internet
Author: User
Tags benchmark

Where all have to say quick sort of, what, I also read a lot of others write articles. But a lot of articles are directly said to take two i,j variables, began to compare.

A quick sort, simply put, is to set a benchmark, and then put it on one side, which is smaller than the benchmark. Put aside, whether it is orderly or disorderly. The next step is to repeat the previous operation, which is actually a recursive process.

Why define two variables for quick sorting?

In fact, the fast sort, why call fast sort, fast where, fast in it reduces the number of exchanges, there is also a reason is that the fast sorting is not new open memory, can reduce the use of memory. It is the sort that is done in situ. Perhaps you still do not understand, so let's assume that if you define only one variable i, then compare size, then it will be bubble sort. I just said that the quick sort is done in situ, without the need for additional space resources. It is clearly not possible for you to define a variable individually.

Why define if the base quantity is on this side, then start scanning from the other side first.

Think first if it is on the same side, it is not a bubble sort.

Then I will share the next, I understand the quick sort to everyone, please give us a lot of advice.

First, for example, we get an array of int [] arr=new int () {1,3,5,2,4,8,6,7}. As we have just done,

  classProgram {Static voidMain (string[] args) {            vararr =New int[] { $, A, the, About, -, -, at, Wu }; Quick_sort (arr,0, arr. Length-1); foreach(varIincharr)            {Console.WriteLine (i);        } console.readkey (); }        Static voidQuick_sort (int[] s,intLintr) {//Recursive invocation            if(L <r) {inti = Adjustarray (S, L, R);//first into the excavation pit filling method adjustment s[]Quick_sort (S, l, I-1);//Recursive invocationQuick_sort (s, i +1, R); }        }        /// <summary>        ///adjust array by base number/// </summary>        /// <param name= "arr" ></param>        /// <param name= "left" ></param>        /// <param name= "right" ></param>        /// <returns></returns>        Static intAdjustarray (int[] arr,intLeftintRight ) {            inttemp = Arr[left];//assume that the first is a datum tree             while(Left <Right ) {                //when the left side is less than the right and the base number is less than the right.                 if(Left < right && temp <=Arr[right]) { Right--;//move the pointer like the front                }                //when the left is less than the right and the base number is greater than the number on the right.                 if(Left < right && temp >=Arr[right]) {Arr[left]= Arr[right];//Note that the number on the right is smaller than the base number, so go to the left.left++;//then the next step is to move to the left. + +                }                //when the left side is less than the right and the number of bases is greater than the left.                 if(Left < right && temp >=Arr[left]) { Left++; }                //when the left side is less than the right and the base number is less than the left.                 if(Left < right && temp <=Arr[left]) {Arr[right]=Arr[left]; Right--; }} Arr[left]=temp; returnLeft ; }}

Why define two variables for quick sorting

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.