Fast sorting (detailed explanation) and sorting (detailed explanation)

Source: Internet
Author: User

Fast sorting (detailed explanation) and sorting (detailed explanation)

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Description:

Data to be sorted is divided into two independent parts by one sort. All the data in one part is smaller than all the data in the other part, then, sort the two data parts by using this method. The entire sorting process can be recursive to convert the entire data into an ordered sequence.

The average time complexity of quick sorting is O (NlogN), which is a Sort version of Bubble sorting.

Method: The "binary" principle is used for quick sorting. The steps are as follows:

1) set two variables I and j. When the sorting starts: I = 0, j = n-1; 2) the first array value is used as the comparison value and saved to temp first, that is, temp = A [0]; 3) then j --, search forward, find the value less than temp, because s [I] is saved in temp, so assign A value directly, s [I] = s [j] 4) Then I ++ searches backward and finds the value greater than temp, because the value of s [j] is stored in s [I] in step 1, assign a value directly, s [j] = s [I], and then j --, avoid endless loops (5) Repeat steps 3rd and 4 until I = j. Finally, return the temp value to 6 in s [I]) and then use the "binary" idea, take I as the dividing line, split into two arrays s [0, I-1], s [I + 1, n-1] and start sorting again

Take array 6 4 7 1 2 as an example:

The Code is as follows:

# Include "stdio. h "void find_frst (int * s, int lift, int right) {int I = lift, j = right, temp; // (1) initialize I, j if (lift> = right) return; temp = s [I]; // (2) Compare the values with the first array, save to temp while (I <j) {while (j> I & s [j]> = temp) // (3) j --, find the small value j --; s [I] = s [j]; // Save the small value to while (I <j & s [I] <= temp) on s [I) // (4) I ++, find I ++; s [j --] = s [I]; // Save the cursor to s [j]} s [I] = temp; // (5) Place the Compare value on s [I]/* (6) split into two arrays s [0, I-1], s [I + 1, n-1] and start sorting */find_frst (s, lift, I-1); // left find_frst (s, I + 1, right); // right} int main () {int I = 0, s [100], n; scanf ("% d", & n ); for (I = 0; I <n; I ++) scanf ("% d", & s [I]); find_frst (s, 0, n-1 ); for (I = 0; I <n; I ++) printf ("% 3d", s [I]); // print printf ("\ n ");}

 

Related Article

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.