Common sorting algorithms

Source: Internet
Author: User
Tags benchmark

This article introduces several kinds of algorithms commonly used in software engineering.

1. Insert Sort

The basic idea of inserting a sort is to insert a record to be sorted each time, by its keyword size, into a sequence that has been sorted before, until all records are inserted.

For the concept of insertion sorting and its principles, you can refer to the learning path of the sorting algorithm-insert sort (concept article)

Inserting sort segments can be divided into three cases.

Direct insertion sort--the way of sorting algorithm learning--Direct insertion sort

Binary insertion sort--the way of sorting algorithm learning--binary insertion sort

Table Insert Sort--the way to learn the sorting algorithm--Table Insert Sort

2. Hill sort

Hill sort is named after the algorithm's designer's name, and its production is improved on the basis of the insertion sort, which can be said to be a special sort of insertion.

Here's a look at the nature of the insertion sort:

First of all, the insertion sorting algorithm is efficient when it is used to manipulate the ordered data, and can achieve the efficiency of linear sequencing.

Second, when you sort by inserting the sort, you can only move one data per trip. So this sort of method is comparatively inefficient.

Based on this nature, Hill Sort's designers invented the hill sorting algorithm, the basic idea is:

First, the entire sequence of records to be sorted into a number of sub-sequences of the direct insertion of the order, the method of splitting the sub-sequence is to set an increment, when each sub-sequence of the present time, the increment minus half (divided by 2, rounding), the sequence of sub-sequences again. In turn, when the records in the entire sequence are basically orderly, then the whole record is inserted into the order of the direct insertion, at this time the increment is reduced to 1, because the direct insertion order in the case of the basic order of elements (according to the above 1th, close to the best case), the efficiency is very high.

The concrete realization Everybody may refer to "The Sorting algorithm study road--Hill sort"

3. Merge sort

Merge sort is an effective sorting algorithm based on merging operation, which is a very typical application of the partition method (Divide and Conquer). The ordered Subsequence is merged to obtain a fully ordered sequence, i.e., the order of each subsequence is ordered, and then the sequence of sub-sequences is ordered. If two ordered tables are combined into an ordered table, they are called two-way merging.

The most common implementation of merge sort is realized by recursive function, of course, the bottom of recursive function is also the stack mechanism. Therefore, the method of merging sorting can also use the stack instead of using recursion.

In fact, we can refer to the way of sorting algorithm learning-merge sort and the way of sorting algorithm learning-merge sort (non-recursive implementation)

4. Quick Sort

Fast sequencing is a sort of algorithm developed by Donny Holl. On average, order n items to be compared 0 (NLOGN) times. In the worst case scenario, a 0 (N2) comparison is required, but this is not a common situation. In fact, fast sequencing is usually much faster than other 0 (NLOGN) algorithms because its internal loop (Innerloop) can be implemented efficiently on most architectures.

Quick sort uses the divide-and-conquer (Divideandconquer) strategy to divide a serial (list) into two sub-serial (sub-lists).
The implementation steps are as follows

1. Select an element from the series, called the "benchmark" (this benchmark is found in many ways, where we use the first element as the benchmark)
2, reorder the series, all elements are smaller than the benchmark value placed in front of the benchmark, all elements are larger than the benchmark value of the base (the same number can be on either side). After the partition exits, the datum is in the middle of the sequence. This is called partition (partition) operation.
3. The middle position of the datum obtained from the second step divides the array into two parts, recursively (recursive) sorts the sub-sequences that are smaller than the base value elements and the sub-sequences that are larger than the base value elements.
4, repeat the second step, until the number of sub-series is 1

The same common implementation is implemented using recursive functions. So you can also use a non-recursive way is also through the stack to achieve.

We can refer to the way of sorting algorithm learning-quick sort and the way to learn the sorting algorithm ———— fast sequencing (non-recursive implementation)

5. Heap Sequencing

Heap Ordering (Heapsort): Refers to a sort algorithm designed using the data structure of the heap. A heap is a structure that approximates a complete binary tree and satisfies the properties of the heap at the same time: that is, the key value or index of the child node is always less than (or greater than) its parent node.

From the last sentence of the definition above, we can also draw two concepts-the big top pile, the small top heap.

The so-called Big Top heap is the child node's key value or index is always smaller than its parent node. The sequence derived from the large top heap is incremented.

The so-called small top heap is the child node's key value or index is always greater than its parent node. The sequence derived from the small top heap is decremented.

Let's take a look at the idea of heap sequencing

Using the top of a small top heap is a feature that records the maximum keyword, making it easy to select the largest record from the unordered order at a time.

The basic idea is (the Big Top heap): 1) The initial order to sort the key sequence (R1,R2....RN) is constructed into a large top heap, the heap is the initial unordered area, 2) The top element of the heap r[1] and the last element R[n] Exchange, this time to get a new unordered area (R1,R2,...... RN-1) and the new ordered area (Rn), and satisfies the r[1,2...n-1]<=r[n]; 3) due to the new heap top r[1] may violate the nature of the heap, it is necessary to the current unordered zone (R1,R2,...... RN-1) adjusts to the new heap, then swaps the r[1] with the last element of the unordered zone, resulting in a new unordered area (r1,r2....rn-2) and a new ordered area (RN-1,RN). This process is repeated until the number of elements in the ordered area is n-1, and the entire sorting process is complete. The procedure is as follows: 1) Initialize the heap: R[1..N] is constructed as a heap, 2) swaps the top element of the current unordered area (R[1]) with the last record of the interval, and then adjusts the new unordered extents to the new heap. So for heap sequencing, the most important two operations are to construct the initial heap and adjust the heap, in fact, the construction of the initial heap is actually the process of adjusting the heap, but the initial heap is to construct all the non-leaf nodes are adjusted.

Specific steps and implementation you can refer to the "Sorting algorithm learning path-heap sequencing"

6. Select Sort

Select Sort is a simple and intuitive sorting algorithm. The basic idea is to select a maximum (or minimum) element at the end of an unordered sequence (note: Here is the end of the unordered sequence, which can be thought of as the starting position of the ordered sequence).

The implementation steps are as follows

1) First find the smallest (large) element in the unordered sequence, and place it in the starting position of the sort sequence
2) continue to find the smallest (large) element from the remaining unsorted elements, and then place it at the end of the sorted sequence.
3) Repeat the second step until all the elements are sorted.

Detailed introduction and code implementation you can refer to the "Sorting algorithm learning path-select Sort"

7. Bubble sort

Bubble sort is also a simple and intuitive sorting algorithm. The idea is that it repeatedly visits the sequence to be sorted, compares two elements at a time, and swaps them if their order is wrong. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. The algorithm is named because the smaller elements will slowly "float" through the switch to the top of the sequence.

Steps to bubble sort

1) compare adjacent elements. If the first one is bigger than the second one, swap them both.
2) for each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair. When this is done, the final element will be the maximum number.
3) Repeat the above steps for all elements except the last one.
4) Repeat the above steps each time for less and fewer elements until there is no pair of numbers to compare.

Detailed introduction and code implementation you can refer to the "Sorting algorithm learning path-bubble sort"

Above is my learning algorithm in the sorting algorithm, of course, these are not complete. Follow-up will continue to update the algorithm, I hope you continue to pay attention.

Common sorting algorithms

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.