Summary of various sorting algorithms

Source: Internet
Author: User
Tags benchmark

Stable sorting:

1. Bubble Sorting:

Working principle: Compare Adjacent Elements in sequence, place Decimals in front, and place large numbers in the back. Each scan places the maximum number on the rightmost side. After each scan, the number of scans for the next scan is reduced by one, so that all the scans are placed properly. That is, the first scan: Compare the first number and the second number to determine the size. The decimal number is before, the large number is after, and then compare the second number and the third number to know the last number. The maximum number is at the last position. Second scan: Compare the number from the first to the second to determine the size. The number of digits before the decimal point and the number after the decimal point until the second to the last. Scan n times in the same way. The essence is that a maximum value is captured every scan, so it is called Bubble sorting.

2. Insert sorting:

Working principle: the whole sequence to be sorted is divided into an ordered sequence and an unordered sequence by construction, and an element is selected from the unordered sequence to be inserted into the sequence to be ordered. Note that the first number of the entire sequence is used as an ordered sequence, and the N-1 number is used as an unordered sequence. Select the first number from the unordered sequence and insert it into the sequence. The insertion method is to scan the sequence from right to left. the rightmost number is the largest number in the sequence, which is compared in sequence, if the number to be sorted is smaller than the number to be sorted, move the number to the back, and finally find the proper position of the number to be inserted.

3. Merge Sorting:

Working principle: first, apply for a space with the size of the sum of the two sorted sequences to store the merged sequences. The merge method is to set two pointers respectively, pointing to the first number of two sequences to be sorted. Compare the values pointed to by the two pointers in sequence, place a small number in the applied space, and so on. Until one pointer reaches the end of the sequence. Copy the elements of the remaining sequence to the requested space.

4. Sort buckets:

Working principle: Give a group of numbers and divide them into buckets in the N equal range. That is, if there are 100 buckets, they are divided into 10 buckets with the same size range. Then, each bucket is placed with elements of the corresponding size and sorted in the bucket. Finally, the elements in each bucket are output in the bucket order. You can select bubble, insert, or snapshot for sorting in the bucket.

5. Base sorting

Working principle: Find the maximum value of a group of numbers to be sorted, and place the numbers from low to high to 0 ~ 9 (decimal, if it is binary, It is 0 ~ 1) in a bucket, the result after each sort by bit is used as the input for the next high sort. That is, each sort can be achieved through recursion.

Unstable sorting:

1. Select sorting:

Working principle: first, we will regard the first element of the sequence to be sorted as a sorted number. We will compare it with each subsequent number to determine the size. If it is found to be smaller than it, then, it is exchanged. After each scan, the minimum number is placed at the beginning. And so on. Until all elements are placed in the correct position.

The reason for the instability of This sorting algorithm is that it has an exponential exchange, that is, the exchange operation is not caused by the exchange of adjacent elements. For example, if the elements to be sorted are: {, 1}, the minimum value is changed to {, 2} after being selected for the first time.

2. Hill sorting:

Working principle: Hill sorting is a sort of insert, which is an improvement for the direct insertion sorting algorithm. This method is also called to narrow down incremental sorting.

Sort the sorted arrays according to different incremental sequences and insert and sort them in different incremental modes. When the increment value is 1, it degrades to direct insertion sorting.

Due to incremental changes, this Sorting Algorithm inserts non-adjacent elements. Therefore, it is unstable.

3. Fast sorting:

Working principle: Fast sorting is an improvement of Bubble sorting. Working principle: 1) First, select a number as the benchmark. You can select different benchmarking methods to improve the quick ranking. After selecting the benchmark, the benchmark is exchanged with the first element of the sequence to be sorted. the benchmark is located at the position of the first element to facilitate subsequent operations. 2) scan the sequence to be sorted from the right to the left to find a smaller number than the benchmark and exchange it with the benchmark element, that is, place the number later than the benchmark at the beginning, at this time, the benchmark is switched to the back; 3) then, scan from left to right to find a number larger than the benchmark. Similarly, if the benchmark is exchanged with the benchmark, more than the benchmark element will be placed later. Repeat this process until all the elements greater than the benchmark are placed on the right of the benchmark, and the elements smaller than the benchmark are placed on the left of the benchmark. Finally, place the benchmark in the correct sequence. After each process, the benchmark is sorted. Then, the sequence on the left of the benchmark recursively calls this process, and the sequence on the right of the benchmark recursively calls this process. Until all elements are placed in the correct position.

This algorithm also exchanges non-adjacent elements, so this algorithm is unstable.

4. Heap sorting:

Heap sorting is the feature that records the maximum keyword using the maximum heap (minimum heap. The procedure is as follows:

1) initialize the maximum or minimum heap. The structure of the maximum or minimum heap is that in the Complete Binary Tree, the root node is the maximum or minimum of all nodes.

2) swap the top element (the root node of the Binary Tree) with the last element of the sequence to be sorted, and get the new unordered area 1 ~ N-1-1 elements, and n elements in the ordered area.

3) after switching, the sequence of unordered areas may violate the heap nature (that is, the top element of the heap is the maximum or minimum value ). Therefore, you need to adjust it (that is, by comparing all elements, place the largest element on the top of the heap, that is, the position of the first element of the sequence ). After adjustment, swap the heap top with the last element n-1 to obtain the new unordered Zone 1 ~ N-2 elements. N-1 and N in the ordered area. The whole sorting process is completed until there are n-1 elements in the ordered area.

Heap sorting is also a kind of selection sorting, which is a kind of tree selection sorting. Since each exchange is also the exchange of feilin elements, the positions of the same elements change. Therefore, this algorithm is also unstable.

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.