Quick sorting-Java Implementation

Source: Internet
Author: User

\ (Ghost ^ blog)/for various reasons, I haven't written a blog for a long time, and I always think it takes a long time to write a blog ....

In addition, I felt a little depressed during the entire holiday .... I have increased my interest and learned a bunch of new things during the holidays...

I always feel that I have learned everything, but I am not proficient in everything,

Therefore, we should go back to the language we are most familiar with. Java is a good way to learn first. Learn more if you are interested.

The fastest and average algorithm for fast sorting is O (nlog (n), and it is the fastest among many sort algorithms with the complexity of O (nlog (n, so I want to start with this, like the Arrays in Java. in sort (), quick sorting is used. There is a saying in the book about the Arrays. sort () method in java ):

When n <= 40, it selects the base element is (s0, sn/2, sn-1) of the middle value; n> 40, select the median value of nine elements with the same interval. When n is less than 7, it is switched to insert sorting.

First list the source code, and then analyze it step by step:

private static int partition(int[]arr,int low, int high){    int temp = arr[low];    while(low < high){        while(low

For example, if we want to sort:

Arr = {, 28 };

First, obtain the reference element temp = arr [0] = 46. At this time, low = 0, low

Then arr [low] = 28 <= temp, so low ++ moves to 35. Similarly, low ++ jumps out of the loop at 60, at this time, the value of 60 is larger than temp, because the value of arr [high] has been stored in arr [low], so you can safely put the value of 60 in arr [high.

Because it still meets low

Now, before arr is ready to enter the first large loop, the values are: arr = {28, 35, 60, 94, 12, 60}

We found that 60 appears twice, but don't worry. In the end, a [high] = temp will still be replaced.

In the second loop, low starts from 60, and high starts from the last element.

The following figure shows the changes in each end loop of arr:

Arr = {28, 35, 60}

The final values include: arr = {28, 35, 60}

At this point, we can see that the two sides of the reference element 46 are smaller than it, and the other side is bigger than it, but the rice is completely sorted. How can we sort it well?

What we should use is a principle of sub-governance ~~~~

We can see from the sort (int [] arr, int low, int high) in the program. Suppose we first sort (arr, low, m), this is equivalent to sort (arr, ), recursive call, high> low, actually think sort (arr,) is better, because I don't think it is necessary to put 46 in the following method again, even if it is put ). It is more appropriate to change it to sort (arr, low, m-1), because the element on the left is always smaller than the reference element m, and m is not necessary to be considered.

Then, a sequence will be obtained, and the high <low is known for non-recursive calls, that is, the elements on the left are sorted sequentially ~ Similarly, you can sort the elements on the right ~~~

The best and average time complexity of fast sorting is O (nlogn );

Analysis: The algorithm complexity is related to whether the two subsets separated by partition are balanced. In the best case, if each division is a median value, the sub-sets (n-1)/2 can be divided every time. At n> 1, T (n) = 2 T (n/2) + O (n), so the complexity is O (nlogn ). if it is already a sorted sequence, the Child sets of the 0 and n-1 elements are split. At this time, T (n) = T (n-1) is generated when n> 1) + O (n), so the complexity is O (n ^ 2)


This article from "tired also happy D" blog, please be sure to keep this source http://zhangzhang.blog.51cto.com/6250085/1300975

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.