Overview of sorting algorithms

Source: Internet
Author: User

Referring to the sorting algorithm we need to know two frequently mentioned concepts:
(1) Stability of sorting algorithms: the so-called "stability" refers to the two identical elements that appear in the array to be sorted, and the relative maintenance remains unchanged after sorting. For example: the array to be sorted is arr[] = {1,4,3,1}, the element becomes arr_new[] = {1,1,4,3} after ordering, and the first in Arr_new is the second of the first 1,arr_new in Arr is the second 1 in Arr, This is what we say about this sort of stability.
(2) in-situ sorting: The so-called in situ ranking refers to, do not apply for extra space to assist the sorting algorithm, but on the original data to be sorted directly on the comparison, exchange, mobile and other operations

Quick Sort

Algorithm idea: Such a sort of fast sorting, select the first element in the array arr[0] as the basis, after iterating through the array, so that the first element in the array into the correct position, that is, the element to the left of the position is less than or equal to arr[0], the right side of the element is greater than or equal to arr[0]. Then, the elements on the left and right of the position are sorted quickly, so that the entire array is sorted.

Bubble sort

Algorithm principle: The bubble sort is completed by N-1, the first sub-order from the 1th number to the number of n-i+1, if the number of I is greater than the number of i+1, then the exchange of these two numbers, in fact, so after I second son order to make the 1th number to the N-i + 1 number between the largest number of exchange to n-i+1 Position. In fact, the bubble sort can be optimized, that is, when the second son of the order does not occur when the exchange of elements, it is stated that the array has been sequenced, the subsequent sub-sorting will not be done.

Select sort

Algorithm principle: The so-called selection of the order after n-1 selection, when making the first choice, is from the 1th element to the element n-i+1 select the largest element and the n-i+1 position of the element exchange, such as the 1th choice to make the largest element to the last position of the array. Note that the exchange of data occurs only once per selection in the selection sort.

Insert Sort

Algorithm principle: The array to be sorted into: ordered and unordered areas. Then each time the first data from the unordered area is inserted into the correct position of the ordered area, the sort is finalized.

Heap Sort

Algorithmic thinking: The so-called heap sequencing is achieved using the idea of a complete binary tree. The first thing to mention is the maximum heap, where each parent node in the maximum heap (full binary tree two) is greater than or equal to the value of the two son node, and it is clear that the heap top is the maximum value of the element, and then the top element of the heap is exchanged with the last element in the heap (the element with the largest number of nodes The maximum value falls to the position of the array's arr[n-1], and then the former N-1 element continues to be treated as above, so that the heap is sorted n-1 times.

Merge sort

Algorithm principle: The idea of merging sort is divided, dividing a sorted array into two smaller arrays, then sorting them separately, and then merging the smaller arrays of two rows in order to get the sorted result of the original array. It should be noted that this combination of two sequential numbers and a good algorithm, the time complexity is O (n1+n2). N1, N2, respectively, are the lengths of the two decimal groups.

Overview of 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.