Original address:
This address: http://www.cnblogs.com/aiweixiao/p/8202360.html
Original 2018-01-02 attention to public number programmer's cultural feelings
1. Overview
Common sorting algorithm, although very basic, but very much, if you can think clearly, quickly write out the code of each algorithm implementation, or need to spend a bit of Kung Fu, today, with everyone in the inventory of some of the commonly used algorithms.
Bubble sort
Insert Sort
Select sort
Hill sort
Heap Sort
Merge sort
Quick Sort
2. Each sort code implementation (PHP version)
Code See GITHUB:HTTP://T.CN/RHJBCU7
2.1 Bubble Sort
1) "Definition": that is, the number in the first position is compared to the number in the second position next to him, if the number is smaller than his neighbor, then the two positions are exchanged, otherwise they are not exchanged. Then the number in the first position is the size of the number in the third position, and it is the small interchange, which is exchanged until the number of the last position is compared. Then, it is the next loop, the number in the second position repeats the comparison exchange operation above, until the entire sequence is transformed into an ordered sequence from small to large.
2) "Code implementation": two-tier for loop is done.
Bubble Sort 2.2 Insert sort
1) "Definition": Select a minimum value from a list of columns to be sorted (you can think of the first number as a sorted sequence), and then select the minimum value from the remaining sorted sequence to be placed in the ordered sequence, sequentially, until the final sequence is a small to large ordered sequence.
2) "Code implementation":
Insert Sort 2.3 Select sort
1) "Definition": Select a minimum value from a list of columns to be sorted, put it in the first position of the new array, continue to select the smallest value from the remaining series to put into the array, repeat the steps above, and take the numbers out to form a new ordered sequence.
2) "Code implementation":
Select Sort Main function
Select Sort Sub function 2.4 Hill sort
1) "Definition": an improvement in the insertion sort, first comparing elements of a certain distance into ordered series, and then comparing the elements that reduce the increment distance (which can be half of the number of elements), until the comparison is an adjacent element, it becomes the insertion sort. So the hill sort is an improvement of the insertion sort.
2) "Code implementation":
Hill sort 2.5 Heap sort
1) "Definition": 1?? Construct the Big Top heap 2?? Exchange Heap top and bottom 3?? Repeat the previous steps in ascending order complete
For more information see: https://www.cnblogs.com/chengxiao/p/6129630.html
2) "Code implementation"
Heap Sort main function heapsort ()
Heap Sort sub-function 2.6 merge sort
1) "Definition": the sequence of columns to be sorted as a single ordered sequence of numbers, and then merge until the final complete sequence is completed.
For more information, refer to: https://www.cnblogs.com/jingmoxukong/p/4308823.html
2) Code implementation:
Main function MergeSort (), two sub-functions Mergepass () and merge ()
Merge sort main function mergesort ()
Merge sort sub-function mergepass ()
Merge sort sub function merges ()
2.7 Quick Sort
1) Definition: The basic idea of the algorithm is:
1. First, a number is taken from the series as the base number.
2. The partitioning process, which puts the number of large numbers on its right, is less than or equal to its number to the left.
3. Repeat the second step for the left and right intervals until there is only one number for each interval
2) Code implementation:
Quick Sort
3. Sort Summary
The stability of various sorts, time complexity, space complexity, stability summary such as:
Comparison of sorting algorithms
--php implementation of the common sorting algorithm