Computer sorting algorithm

Source: Internet
Author: User
Tags benchmark sorts

Features of the algorithm:

1. Have a poor sex. An algorithm should contain a limited number of steps, not infinite. There is a poor value within a reasonable range, if the computer to perform a 1000-year-old algorithm, although it is poor, but beyond the reasonable limit, people do not regard him as an effective algorithm.

2. Certainty. Each step in the algorithm should be deterministic, not ambiguous, and ambiguous. Each step in the algorithm should not be interpreted as a different meaning, but should be very clear. In other words, the meaning of the algorithm should be unique. And should not produce ambiguity.

3. There are 0 or more inputs, the so-called input refers to the implementation of the algorithm is required from the outside to obtain the necessary information.

4. There is one or more outputs. The purpose of the algorithm is to solve, the algorithm without output is meaningless.

5. Validity. Each of the steps in the algorithm should be executed effectively. And get a definite result.

First, the Fast sorting algorithm

Fast sorting is a sort algorithm developed by Donny Holl. Quick sort using the divide-and-conquer strategy to divide a serial into two sub-serial

Algorithm steps

1. Jump out of a sequence of elements into a "benchmark"

2. Reorder the series, where all elements are placed at a lower baseline than the base value, and all elements are placed behind the base value (the same number can be placed on either side). After this partition is launched, the benchmark is positioned in the middle of the sequence. This is called partitioning operation.

3. Recursively sorts sub-columns that are less than the base value element and that are larger than the base value element.

At the bottom of the recursive scenario, the size of the sequence is 0 or one, which is always sorted. Although it is always recursive, the algorithm always exits, because in each iteration, it will at least put an element in his final position.

Second, heap sorting algorithm

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

Algorithm steps

1. Create a heap h[0...n-1]

2. Swap the stack head (maximum) with the end of the heap

3. Reduce the size of the heap by 1 and call Shift_down (0) to adjust the new array top data to the response location

4. Repeat step 2 to know that the heap size is 1

Heap sequencing is an unstable sorting algorithm, and instability occurs at the moment when the heap top element is exchanged with A[i].

  such as sequence: {9, 5, 7, 5}, heap top element is 9, heap sort next swap 9 and second 5, get sequence {5, 5, 7, 9}, then heap adjust get {7, 5, 5, 9}, repeat operation last get {5, 5, 7, 9} thus changing two 5 Relative order.

Third, merge sort

Merge sort is an efficient sorting algorithm that is created on the merge operation.

The implementation of merge sort is divided into recursive implementation and non-recursive (iterative) implementation. Recursive implementation is a typical application of divide-and-conquer strategy in algorithm design, we divide a big problem into small problem and solve it separately. Then use the answers to all the small questions to solve the big problem. The merge ordering of non-recursive (iterative) implementations is first 22 merges, then 88 merges, and continues until the entire array is merged.

Merge sort mainly relies on merge operations. The merge operation refers to the operation of merging two sorted sequences into a sequence, merging the steps

1. Apply the space so that it is the sum of two sorted sequences that are used to store the merged sequence

2. Set two pointers, the initial position is the starting position of two sorted series

3. Compare the elements pointed to by two pointers, select a relatively small element into the merge space, and move the pointer to the next position

4. Repeat step 3 until a pointer reaches the end of the sequence

5. Copy all remaining elements of another sequence directly to the end of the merge sequence

Iv. Insert Sort

The insertion sort is always a simple sorting algorithm. He works very much like we grab poker.

For the last sorted array (hand-picked cards), scan from back to forward in the sorted sequence (hand already ordered), find the response position and insert.

Specific steps

1. Starting with the first element, the element can be thought to have been sorted

2. Take the next element and scan from the back forward in the sequence of ordered elements

3. If the element (sorted) is greater than the new element, move the element to the next position

4. Repeat step 3 until the sorted element is found to be less than or equal to the position of the new element

5. After inserting a new element into the location

6. Repeat steps 2~5

V. Insert sort improvements: two-point insertion sort

For the insertion sort, if the cost of the comparison operation is larger than the swap operation, the binary lookup method can be used to reduce the number of comparison operations, and we become the two-point insertion sort.

When n is large, the comparison of the binary insertion sort is much better than the worst case of a direct insert sort, but it is worse than the best case for direct insertion, when the initial sequence of elements is already close to ascending, the direct insert sort is less than the two-point insertion sort comparison. The binary insertion sort element moves the same number of times as the direct insert sort, depending on the element's initial sequence.

First, determine the median position of the target array as 45 digits.

Because of the 45>35, so the binary array and take the left Antimeron array as the search target, the left half of the middle element is 23

Because 23<35, so binary, select the right half of the word group as the search target

And 35<36, so the position of 35 is between 23 and 36.

Six, insert sort more efficient improvement: Hill sort

Hill sort, also called descending incremental sort, is a more efficient and improved version of the insertion sort. The hill sort is an unstable sorting algorithm.

The hill sort is based on the following two-point nature of the insertion sort, which proposes an improved method:

1. Insertion sequencing is efficient in the case of almost sequenced data operations, i.e. the efficiency of linear sequencing can be achieved

2. But the insertion sort is generally inefficient because the insertion sort can only move data one at a time

Hill sort by dividing all the elements of the comparison into several areas to improve the performance of the insert sort, so that an element can be a one-time move toward the final position of a big step, and then the algorithm to take smaller steps to sort, the final step of the algorithm is the normal insertion sort, but to this step, The data that needs to be sorted is almost already lined up. (So the insertion sort is faster)

Suppose there is a very small data at the end of an array that has been sorted in ascending order. If you use a sort of complexity O (n^2) (bubble sort or direct insert sort), you might compare and swap n times to move the data to the correct location. The hill sort moves the data in a larger stride size, so small data is only a few comparisons and exchanges to the right place.

Hill Sort is an unstable sorting algorithm, although the one-time insertion sort is stable and does not change the relative order of the same elements, but during different insertion sorts, the same elements may move in their own insertion sort and finally their stability will be disrupted.

  For example, the sequence: {3, 5, 8, 7, 2, 8, 1, nine, 6},h=2 are divided into two sub-sequences {3, 10, 7, 8, 20} and {5, 8, 2, 1, 6}, not sorted before 8 in the second subsequence, and now on two sub-sequences Insert Sort, get {3, 7, 8, 10, 20} and {1, 2, 5, 6, 8}, i.e. {3, 1, 7, 2, 8, 5, 10, 6, 20, 8}, two 8 of relative order changed.

Vii. Choice of sorting

Sorting is also a simple and intuitive sorting algorithm, and he works by first finding the smallest (large) element in the unordered sequence, storing it in the starting position of the sort sequence, and then continuing to look for the smallest (large) element from the remaining unordered sequence and putting it at the end of the sorted sequence. And so on

Eight, bubble sort

Bubble sort is a simple sorting algorithm, and it's the first sorting algorithm that I've learned, which repeats the elements to be sorted, compares adjacent two elements at a time, and swaps them if their order is wrong, until there are no elements in the swap order.

Implementation steps:

1. Compare adjacent elements, if the previous one is larger than the latter, put both of them in the swap position

2. Do the same work for each pair of adjacent elements, from the beginning of the first pair to the end of the last pair, after this step is done, the final element will be the largest 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 no number is required to compare.

Nine, the improvement of bubble sort: cocktail sort

Cocktail sort, also called directional bubble sort, is an improvement of bubble sort, the difference between this algorithm and bubble sort is from low to high and then high to low, and bubble sort only from low to high to compare each element in the sequence he can get a bit better performance than bubble sort.

In the case of sequences (2,3,4,5,1), cocktail ordering requires only one sequence of visits to complete the sort, but it takes four times to use bubble sorting. But in the chaotic sequence of the state, cocktail ordering and bubble sorting efficiency is very poor.

Each sorting method performance

Computer sorting algorithm

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.