Java interview is often asked the 8 large sorting algorithm (analysis + program) __ Code

Source: Internet
Author: User
Java interview is often asked for the 8 large sorting algorithm (analysis + program)

Classification:

1 Insert Sort (direct insert sort, hill Sort)
2 Exchange sort (bubble sort, quick sort)
3 Select sort (Direct select sort, heap sort)
4) Merge sort
5) Allocation sort (cardinal sort)
Maximum required secondary space: merge sort
Minimum required auxiliary space: heap Sort
Average Speed fastest: quick sort

Instability: Quick sort, hill sort, heap sort.

1. Direct Insertion Sort

Basic idea: In the set of numbers to be sorted, suppose that the front (n-1) [n>=2] number is already a row

In a good order, now you have to insert the nth number into the preceding ordered number so that the number of n

It's a good order, too. Repeat the loop until all the order is sorted.

2. Hill sort

The hill sort, also called the descending increment sorting algorithm, is a more efficient version of the insertion sort. Hill sort is a non stable sort algorithm.
The hill sort is based on the following two-point nature of the insertion order:
The insertion sort is efficient in the operation of data which is almost already sorted out, that is, the efficiency of linear sorting can be achieved.
However, the insertion sort is generally inefficient because the insertion sort can only move data one bit at a time.
Take a positive integer D1 < n, put all the D1 records in one group, insert the sort directly in each group, and then D2 < D1, repeat the grouping and sorting operations, until Di = 1, where all records are sorted in a group.

3. Simple selection Sort

Basic idea: In the set of numbers to be sorted, select the smallest one number and the number of the first position exchange;

Then, in the remaining number, find the smallest interchange with the second position, so that it loops to the penultimate number and the last number comparison.

4. Heap Sorting

Basic idea: Heap sequencing is a kind of tree-shape selection, which is an effective improvement to the direct selection sort.

The heap is defined as follows: A sequence with n elements (h1,h2,..., HN), which is called a heap only if it satisfies (hi>=h2i,hi>=2i+1) or (hi<=h2i,hi<=2i+1) (i=1,2,..., N/2). Only the heap that satisfies the former condition is discussed here. As you can see from the definition of the heap, the top element of the heap (that is, the first element) must be the maximum item (the large top heap). A complete binary tree can visually represent the structure of a heap. The top of the heap is the root and the other is Zuozi and right subtree. Initially, the sequence of numbers to be sorted is considered to be a sequential two-tree, which adjusts their storage order to make it a heap, at which point the number of root nodes of the heap is the largest. The root node is then exchanged with the last node of the heap. Then readjust the number of front (n-1) to make it a heap. And so on, until there are only two nodes in the heap, and exchange them, and finally get an ordered sequence of n nodes. From the description of the algorithm, heap sequencing requires two processes, one is to build a heap, and the other is to exchange positions between the top of the heap and the last element of the heap. So the heap sort has two function components. The first is to build the seepage function of the heap, and the second is to call the function of the infiltration function to realize the sort.

Build heap:


Swap, kicking out the maximum number from the heap


Pile up the remaining nodes and swap the maximum number of kicks.


And so on: The last two remaining nodes in the heap are swapped, kicked out, sorted and finished.

5. Bubble sort

The basic idea: in order to sort of a group of numbers, the current has not yet lined up in the range of the whole number, from top to bottom of the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. That is, whenever two adjacent numbers are compared and found to be in reverse order, they are interchanged.

6. Quick Sort

Basic idea: Select a datum element, usually select the first element or the last element, through a scan, the backlog sequence is divided into two parts, one part is smaller than the datum element, the other is greater than or equal to the datum element, when the datum element is in the correct position after it is sorted, and then recursively sorts the two parts divided by the same method.

7. Merge sort

Basic Sort: Merge (merge) sorting method is to combine two (or more than two) ordered tables into a new ordered table, that is, to divide the sequence into several subsequence, each subsequence is ordered. Then the ordered Subsequence is merged into the whole ordered sequence.

8, Cardinal Order

Basic idea: Unify all the values to be compared (positive integers) to the same number of digits, preceded by a short number of 0. Then, start at the lowest bit, and then sort it one at a time. This is done from the lowest bit to the highest order, and the sequence becomes an ordered series.


Results


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.