Quick sort of Android learning experience

Source: Internet
Author: User

Quick Sort (Quicksort)

is an improvement of the bubble sort, its basic idea is: by a trip to sort the data will be sorted into two separate parts, one part of all the data is smaller than the other part of all the data, and then this method to the two parts of the data are quickly sorted, the entire sorting process can be recursive, To achieve the entire data into an ordered sequence.

Set to sort the array is a[0] ... A[n-1], the first arbitrary selection of data (usually the first number of the array) as the key data, and then all the smaller than the number of it in front of it, all the larger than its number is placed behind it, this process is called a fast sort of a trip.

It is important to note that fast sorting is not a stable sorting algorithm, that is, the relative position of multiple identical values may change at the end of the algorithm.

A quick sorting algorithm is: 1) Set two variables I, J, when the sort begins: I=0,j=n-1;2) takes the first array element as the key data, assigns the value to Key, i.e. Key=A[0];3) forward search from J, that is, after the start of a forward search (j--), find the first less than KeyValue A[j], assign a[j] to a[i];4) from I to the backward search, that is, from the front to start backward search (i++), find the first one greater than KeyA[i], assign a[i] to a[j];5) repeat 3rd, 4 steps, until i=j; (3,4 step, no qualifying value is found, 3 in a[j] is not less than Key, 4 A[i] not greater than KeyChange the value of J, I so that j=j-1,i=i+1 until it is found. Locate the value that matches the condition, and the J pointer position does not change when I exchange it. In addition, I==J this process must be exactly when the i+ or J completes, at which time the loop ends).Sort Demo
Suppose the user enters an array like this:
subscript 1 3 4 5
data 6 2 3 9
Create a variable i=0 (point to the first data), j=5 (point to the last data), k=6 (assigns the value of the first data). We took the data from subscript 0, so we needed to find a number to replace him. Since we're going to move all the numbers that are smaller than 6 to the left, we can start looking for a number smaller than 6 and go from right to left. Don't worry, we'll find it in order. Continuously decreasing the value of J, we found that the subscript 3 data is smaller than 6, so the 3 is moved to subscript 0 (actually I point to the position. I should be used in the code because the next loop will also be recycled without the second loop:
Subscript 0 1 2 3 4 5
Data 3 2 7 3 8 9
I=0 j=3 k=6 Because the variable k has stored the subscript 0 data, so we can rest assured that the subscript 0 is covered. As a result, subscript 3, although there is data, but the equivalent of no, because the data has been copied to another place. So we find another data to replace him. It's going to be bigger than K, and I'm going to find it from the back. Sliding scale variable I, found that subscript 2 is the first larger than the K, so with the subscript 2 of the data 7 to replace the J-pointing subscript 3 data, the data state into the following table:
Subscript 0 1 2 3 4 5
Data 3 2 6 7 8 9
i=2 j=3 k=6 Repeat the above steps, decrement the variable J. At this point, we found I and J "Meet": they all point to subscript 2. So, the end of the cycle, the K fill the subscript 2, that is, the result. If I and J do not meet, then sliding scale I find large, not yet, and then diminishing J to find small, so repeated, continuous circulation. Attention to judgment and search is carried out at the same time. Note:The quick sort does not directly get the final result, only the number smaller than K and K is divided into the two sides of K. (You can imagine I and J are two robots, the data is the size of the stone, first take the stone in front of me to set aside the maneuver space, and then they took turns to choose than k big than k small stone throw to the opposite, finally in their middle take away that piece of stone put back, so than this stone large throw to J that side, The small one was thrown to the side of I. Just this time good luck, throw once just neat platoon. In order to get the final result, you need to perform this step again on an array of subscript 22 edges, and then decompose the array until the array is no longer decomposed (only one data) to get the correct result.

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.