Java Sorting algorithm

Source: Internet
Author: User
1. Defines
by comparison to determine the input sequence <a 1 , a 2 ,.., a n > The ranking algorithm of the relative order between elements is called the comparison sort algorithm.


2. Algorithm explanation
        (1)   Select sort:
      &nb sp;    choice of the basic idea is to treat the sort of record sequence n-1 over the processing, the first time processing is to be l[i. N] Swap position between the smallest and l[i]. In this way, after I process, the position of the first I record is already correct.
        (2): Bubble sort
        The simplest sort method is the bubble sort method. The basic idea of this approach is to think of the elements to be sorted as "bubbles" that are vertically arranged, and that the smaller elements are lighter so that they float. In the bubble sort algorithm we have to deal with this "bubble" sequence several times. The so-called one-time processing, is to check this sequence from the bottom up, and always notice the order of two adjacent elements is correct. If two adjacent elements are found to be in the wrong order, the "light" elements are below, exchanging their positions. Obviously, after the process, the "lightest" element floats to its highest position, and after two times, the "minor" element floats to a high position. In the second pass, you do not have to check because the element at the highest position is already the "lightest" element. Generally, the first time I handle it, you do not have to check the elements above the high position, because after the previous i-1, they have been properly sorted
        (3)   Insert sort
      The basic idea is that after i-1 the process, l[1..i-1] has arranged the order. The l[i] is inserted in the appropriate position of the l[1..i-1], making l[1..i] a sequence of sequence. To achieve this goal, we can use the sequential comparison method. First comparison of L[i] and l[i-1], if l[i-1]≤l[i], then l[1..i] is sorted out, and the first-time processing is over, otherwise the location of the swap l[i] and l[i-1] continues to compare l[i-1 and l[i-2] until a position J (1≤j≤i-1) is found, Make L[j]≤l[j+1]
      (4)   Quick Sort

The basic idea of a quick sort is based on divide and conquer strategy of the. L[p for the entered subsequence. R], if the scale is small enough to sort directly, otherwise it will be handled in three steps:

    • Decomposition (Divide): l[p the input sequence. R] divided into two non-empty sequence l[p. Q] and L[Q+1..R] to make l[p. The value of any element in Q] is not more than the value of any element in L[Q+1..R].
    • Recursive Solution (Conquer): by recursive call fast sorting algorithm for l[p. Q] and L[Q+1..R] are sorted.
    • Merge: Because the sort of the decomposed two subsequence is done in place, the l[p. Q] and L[Q+1..R] are sorted out without any computational l[p required. R] is lined up.


<

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.