Fast sort algorithm (Java implementation) __ code

Source: Internet
Author: User

1. Basic Ideas

By sorting the data that will be sorted into two separate parts, a part of all the data is smaller than the other part of all the data, and then the two parts of the data for the rapid sorting, the entire sequencing process can be recursive, so as to achieve the entire data into an ordered sequence.

2, the algorithm steps to get sorted array Select the appropriate number as the Sort datum number (in general, select the first number of arrays or sub arrays). Place the array to be sorted smaller than the datum to the left of the datum number sub1, and the sub2 to the right of the datum number, larger than the base number. If Sub1 or sub2 have only one element, then return directly, otherwise recursively rearrange the sub1 or sub2.

3. Java Code Implementation

public class QuickSort {/** * @Comment fast Sort One * @Author Ron * @Date October 25, 2017 afternoon 12:12:32 * @ret
            Urn */static void Quiksort (int[] sources,int leftindex,int rightindex) {if (Leftindex >= rightindex) {
        If the left subscript is greater than or equal to the lower right subscript, the number of sub array elements obtained after sorting is less than or equal to 1, sorted completes, return directly;

        int startIndex = boundary (Sources,leftindex,rightindex);
        Quiksort (sources,leftindex,startindex-1);
    Quiksort (Sources,startindex+1,rightindex); /** * @Comment binary to sort array * @Author Ron * @Date October 25, 2017 afternoon 12:12:24 * @return * * * Stati
        c int boundary (int[] Sources,int leftindex,int rightindex) {int startindex=leftindex;
        int endindex = Rightindex;

        int basenum = Sources[leftindex];  while (StartIndex < Endindex) {while (StartIndex < Endindex && Sources[endindex] >= basenum)
            {endindex--; } Sources[startiNdex]=sources[endindex];
            while (StartIndex < Endindex && Sources[startindex] <= basenum) {startindex++;
        } Sources[endindex]=sources[startindex];
        } Sources[startindex]=basenum;
    return startIndex;
        public static void Main (string[] args) {int[] sources={3,9,4,1,7,6,2,0,8};
        Quiksort (sources,0,sources.length-1);
        String result= "";
        for (int i = 0; i < sources.length. i++) {result = sources[i]+ "";
    } System.out.println (Result); }
}
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.