The fast sequencing of data structure and algorithm practice of Hark

Source: Internet
Author: User

---restore content starts---

Objective

Fast sorting is the most common, but also the easiest way to test in the interview, here to summarize

Algorithm description

In fact, this is very clear: http://blog.csdn.net/morewindows/article/details/6684558

But I still intend to follow my own logic to describe again, if you do not understand, then go to see the post of the great God.

A quick sort of practice is also the idea of divide and conquer, the flow is:

1, if you do is a descending sort; take the first number of the array as the base, and from right to left find a number greater than or equal to the base, and put it to the left of the base. Then, from left to right, a number smaller than the cardinality is found and placed to the right of the base.

2, then take the base as the center point, and then the left side of the base of the order of 1. Then proceed to the right of the base to sort in 1. How to recursively, and finally get sort results

3, one thing to explain, according to the Great God's interpretation, according to the basis of the digital exchange, using the excavation method for Exchange, for example:

1), we have array int[] Arraydata = {5, 9, 6, 7, 4, 1, 2, 3, 8};

2), in the first round, we use 5 as the base to sort the left and right, then 5 of the index 0 is the first pit. We need to find a number to fill the pit.

3), first right-to-left search. OK, we found 3, which is index 7. Let's write 3 to Arraydata[0] (2 dug in the pit yo). Then arraydata[7] is the pit to be filled.

4), and then search from left to right, at which point the Start Lookup index is arraydata[1]. Looking to the right is smaller than 5, we find that it is 4 and then put 4 in arraydata[7] (3 dug pits). Then arraydata[4] is the pit to be filled (arraydata[4] is the index of the number 4)

5), then search from right to left. Start Index is 6 ... Then it is pits, digging pits, left to right to find ... And so on

6), the final i==j, then jump out of the loop

Code

is using the Java

/* Quick Sort */public class QuickSort {public static void main (string[] args) {int[] Arraydata = {5, 9, 6, 7, 4, 1, 2, 3, 8 }; Quicksortmethod (Arraydata); for (int integer:arraydata) {System.out.print (integer); System.out.print ("");}} public static void Quicksortmethod (int[] arraydata) {Sort (arraydata, 0, arraydata.length-1);} public static void Sort (int[] arraydata, int beginindex, int endIndex) {if (Beginindex < endIndex) {int i = Adjust (arra Ydata, Beginindex, EndIndex); Sort (Arraydata, Beginindex, i-1); Sort (Arraydata, i + 1, endIndex);}} Returns the position of the base after the final sort public static int Adjust (int[] arraydata, int beginindex, int endIndex) {int i = beginindex;int J = Endinde X;int temp = arraydata[i]; Cardinality while (I < j) {//right-to-left, looking for numbers larger than temp while (I < J && Arraydata[j] <= temp) {j--;} if (I < j) {Arraydata[i] = arraydata[j];//Find the number on the right that is larger than the temp base, and place it in the left index i++;//The Left index plus 1, start looking to the right for a number smaller than the temp cardinality}while (i < J & amp;& Arraydata[i] > Temp) {//to the right to look for numbers smaller than the temp base i++;} if (I < j) {ArraydATA[J] = arraydata[i];j--;}} Arraydata[i] = Temp;return i;}}

Final result

Algorithmic complexity: O (NLOG2N)

Space complexity: O (LOG2N)

  

Reference

http://blog.csdn.net/morewindows/article/details/6684558

---restore content ends---

The fast sequencing of data structure and algorithm practice of Hark

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.