Quick sort of getting started with sorting algorithms (Java Implementation)

Source: Internet
Author: User

Fast sorting is also a sort of sorting algorithm. Quick sort and Merge sort is complementary: merge sort sorts the array into two sub-arrays, merges the ordered Subarray to sort the entire array, requires an extra array, and the quick sort is ordered when two sub-arrays are ordered, the entire array is natural and orderly, and the fast sort does not produce extra arrays.

For decimal groups (n<=20), quick sorting is better than inserting a sort. Therefore, the decimal group recommends using a different sort.

A quick sort can be made up of the following steps:

1. Returns if the number of elements in the array s is 0 or 1.

2. Take any element of S in V, called the hub element.

3. Divide the remaining elements in S (except the pivot element) into two parts, some of which are less than V, placed on the left side of V, and partly greater than V, on the right side of V.

4. The left and right parts will continue to be recursively sorted quickly.

Details:

  1. Select Pivot Element

    a common and ignorant choice is to select the first element to use as a pivot element. If the input is random, then this is acceptable, and if the input is pre-ordered or reversed, such a split is meaningless, because all elements are larger or smaller than the first element, and this will only be divided into a portion. This time it takes two times, but in fact nothing is done.

The general practice is to use the median of the three elements in the left, right, and center positions (the number of intermediate sizes) as the pivot element.

  2. Split Policy

    What you do in the split phase is to move elements that are smaller than the pivot element to the left of the array, and move elements that are larger than the pivot element to the right of the array.

First assume that all elements are different. By exchanging the pivot element with the last element, the pivot element leaves the data segment to be split. I start with the first element, J starts with the second last element. When I is on the left side of J, move I to the right, moving over those elements that are less than the pivot element (the elements should be placed to the left of the array) and move J to the left, moving the elements larger than the pivot element. When I and J stop, it shows that I point to a larger element than the pivot element, and J points to a smaller element than the pivot element. If I is smaller than J, then exchange these two elements, that is, I point to the large element (relative to the pivot element) to the right of the array, J points to the small element to the left of the array, and then I and J continue to move. If I is greater than J, the description has moved to the end, at this point I left are smaller than the pivot element, I the right side (including i) are larger than the pivot element. Finally, the element i is swapped with the hub element.

If I and J encounter the same element as the pivot element, you should also stop.

  Code:

This blog reference "Data structure and algorithm analysis Java language description"

Quick sort of getting started with sorting algorithms (Java Implementation)

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.