"Basic algorithm" a quick sort of teenage age

Source: Internet
Author: User

Bigmoyan ready to do a continuous article, the introduction of the algorithm as a blueprint, as far as possible with a kind of vivid language to make some classical algorithms clear, Welcome to cheer ~

I am not a computer professional students, the article has errors in the error, but also hope that experts to correct, in this thanked

--------------------------------------------------------------------------------------------------------------- ----------

"Teenage days from left to right: Xu Zhuxian, Kim Tae-yeon, Kim Hao, bluefish, Changali, Zheng, Li Shungui, approves, Tri Shuying"

Sorting though is a trivial matter, but because this operation in the algorithm is used too frequently, and is particularly important, I remember the C language class when learning the first sorting algorithm, the name is cute called bubble bubble (crossed out) sort. The principle is very simple, for example, according to the height of the image of the sister in ascending order, that is more than the sub, Xu Zhuxian 1.68, Kim Tae-yeon 1.58, that she is a ratio, Kim Tae-yeon for the left one, Xu Zhuxian for the left two, then the 2nd number of Kintai and 3rd Kim Shaobi. After this round, the tallest sister was in the end, the left side is the bottom, the right side is the water, Kintai like a bubble bubbling up.

Similar to the bubble sort of the simple rough Sort method also has the insertion sort, chooses the sorting, because these algorithms are too simple, and the efficiency is not very good, we did not introduce each one, after all, today's topic is the quick sort.

Quick Sort (Quicksort) is a sort method based on divide-and-conquer strategy. The so-called divide-and-conquer strategy is divide and conquer. To put the elephant refrigerator, a total of three steps, a split algorithm is also the case:

STEP1: Decomposition (Divide) divides the problem to be solved into a series of sub-problems, the form of sub-problem is the same as the original problem.

STEP2: Settlement (conquer) solves the sub-problem recursively, and if the problem size is small enough, the recursive direct solution is stopped.

STEP3: Merging (Combine) combines the solution of a sub-problem into the solution of the original problem.

Roughly understanding the divide-and-conquer strategy, we first give the idea of a fast-sequencing algorithm, divert, we try to install the Elephant refrigerator.

STEP1: Decomposition: Array a[p to be sorted. R] is decomposed into two sub-arrays a[p...q-1] and A[q+1,r] (possibly empty), so that each element in a[p...q-1] is less than or equal to a[q], and each element in A[q+1,r] is greater than or equal to a[q]. This is written as A[P...R] instead of A[0...R] for the general case, because the sub-arrays are not all starting from 0, for example, when a quick sort is p=q+1 for a[q+1,r.

STEP2: FIX: Quickly sort each sub-array

STEP3: Merge: Do not need to merge, sub-array is sorted and the original array is naturally ordered.

To explain a little bit, the quick sort actually does two things, first, find a point to divide the array into two sub-arrays that meet the requirements. Second, the sub-array is sorted quickly. Of course, in the quick ordering of the sub-array to repeat these two things-to find a sub-array will be divided into two sun array (make up the noun, understanding is good), and second, the sun array quickly sorted. So the descendants of the endless also.

Ah no, of course, there is poor, when a great-grandchild array only a number of times do not need to sort, this is called the "basic situation" in the division of the strategy, the direct solution is good.

OK, the following into the happy "to the sister row height" (Bad laugh) Link, according to my information (I am also pretty serious), from the girls ' age sisters from left to right height in turn:

a=[168,158 (Kim Tae-yeon), 160,167 (son), 167 (Changali), 162,158 (Li Shungui), 163,170]

Okay, I'll make sure the data is true, so you don't have to go back and forth and look at the photos. Confirm = = (think I don't know what you're doing?) )

A sister of the same height I remember its name in parentheses to make a distinction, because the fast algorithm is an unstable algorithm, and we then see its instability by name.

Before the pleasant sort, we find that the core of the fast sort is actually the magic Q, which requires a[p...q-1] to be less than or equal to A[q], and A[Q+1...R] to be greater than or equal to a[q], and that this q is actually pretty hard to find, in teenage days, for the initial sequence, The q that satisfies this condition is only in the last Tri Shuying, because she is 170 centimeters the tallest, her left people are below her, and her right side--sorry no, is an empty sequence.

In fact, this q isn't what we're looking for, it's constructed. We first stipulate that a[q] is the left one Xu Zhuxian, and then traverse the whole sequence, to Xu Zhuxian as the Center (key), usually lower than her standing on her left, taller than her standing on her right, so after finishing, Xu Zhuxian became the conditions of a[q]. Of course choosing who is the key is casual, for convenience, generally we always choose the first item of the sequence as key.

This process we have a name called Partition, and its pseudo code is as follows:

  

Although it is annoying to look at this kind of article code, but the research of you, do not be a few lines of pseudo-code away yo ~

With the partition, we can be happy to see the sister, remember their height, is this:

[168,158 (Kim Tae-yeon), 160,167 (child), 167 (Changali), 162,158 (Li Shungui), 163,170]

Try to walk on your own, for the initial sequence p=0,r=8, here we use C language from 0 to count the habit.

Ok,key=168,i=9,j=8, this is the initial state.

Start loop:

J=8, Judge 170>168 so I=8,exchange (A[8],a[8]) that is not exchanged

J=7, Judge 163<168, and do nothing.

J=6, Judge 158<168, and do nothing.

Because Xu Zhuxian is the tallest in addition to the 170 Tri Shuying, so the loop actually does nothing, and after the cycle is over j=1,i=8

Then Exchange (A[7],a[0]), return i-1=7

Results No. 0 of Xu Zhuxian (168) and 7th approves (163) Exchange position, return indicator 7

[163,158 (Kim Tae-yeon), 160,167 (child), 167 (Changali), 162,158 (Li Shungui), 168,170]

How about, find the q=7, verify for a[0..6] Everyone is less than 168 tall, and for A[8],a[8]>a[7]

Then for the sub-sequence of sub-sequences recursive call can be, the code is very simple, understand the recursive students can write, as to how to understand recursion, there is a good saying, want to understand recursion, first of all you have to understand recursion.

I wrote this program in Python, and the result of the final return is this.

[158 (Li Shungui), 158 (Kim Tae-yeon), 160,162,163,167 (bluefish), 167 (Changali), 168,170]

Ah, it looks like it's pretty good.

But wait, it's like it's not right.

Compare the original sequence:

a=[168,158 (Kim Tae-yeon), 160,167 (son), 167 (Changali), 162,158 (Li Shungui), 163,170]

The original Changali is on the left side of the Kintai, and the left side of the Li Shungui. After sorting, although the relative order of the Changali and the Li Shungui has not changed, but the relative order of the Jin-tae-yeon is reversed! That is, if we put the name behind the brackets, only to look at height, is not to judge the 158cm is Kintai or Li Shungui!

Remember the bubbling algorithm that we started with? It turns out that if the bubble algorithm is used to rank the order, the final result does not change in this relative order. This sort of the same indicator of the record relative order does not change the nature of the stability of the sorting algorithm, that is, the bubble sort is a stable algorithm, and fast sequencing is an unstable algorithm.

The stability of sorting algorithms is required in some applications, especially when the indicator that participates in sorting is a property of a class entity, the instability algorithm often brings unexpected results, which makes it possible to exchange two class entities that would otherwise not want to be exchanged.

In addition, the quick sort is called the fast sort, because its average complexity is O (NLOGN), which is the most efficient algorithm in all existing sorting algorithms.

Ps.python's code is attached below ~

1a=[['XZX', 168],['Jty', 158],['Jxy', 160],['Lye', 167],['Qyl', 167],['Zxy', 162],['LSG', 158],['Hmy', 163],['Cxy', 170 ]]2 3 defpartition (A,P,R):4Key=a[p][1]5I=r+16      forJinchRange (r,p,-1):7         ifa[j][1]>=Key:8I=i-19a[i],a[j]=A[j],a[i]TenA[i-1],a[p]=a[p],a[i-1] One     returnI-1 A  -  - defquicksort (a,p,r): the     ifp<r: -q=partition (A,P,R) -Quicksort (a,p,q-1) -Quicksort (a,q+1, R) +     return1 -  +Quicksort (a,0,8) A PrintA at  

"Basic algorithm" a quick sort of teenage age

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.