What is sort? Why use sorting? In fact, we all use a sort of life everywhere, take the dictionary, now, we want to find a word in the dictionary (already know the pronunciation of this word), first, we need to according to the pronunciation of the word, find the location of the directory where it is, and then according to the number of pages of the word accurate positioning to the position of the word
In the process of looking for words, we notice the words, according to the pronunciation found ... Position, based on the position of the word in the page, we use a sort and find in the unconscious case, the writer of the dictionary sorts the whole dictionary, and our job is to find a record according to the dictionary sort method. The following is a detailed description of the relevant content of the sort.
Sorting is a process of processing data, and it always comes together with lookups, because the purpose of sorting is to conveniently find data, and the quality of the order determines the efficiency of the search.
There are a number of methods for sorting, and here I have selected two simple and representative two sort methods to compare with each other. Bubble sort and Direct select sort. I will introduce the methods of sequencing, time complexity, stability and characteristics.
Basic process:
The process of bubbling the sort is to compare each of the two data in a collection, and then put the larger data behind it, so that the last data is always the largest after each process is finished. Process diagram for bubbling sort:
The process of directly selecting a sort is to put the smallest data in a set in a sequence that is already sorted, in the opposite direction of the bubble sort order, starting with the first data. Directly select the sort icon:
Complexity of Time:
It can be seen that the bubble sort is used to the two-layer loop, its time complexity is O (n^2), the direct selection of the sorting is also used in the two-layer loop, its time complexity is O (n^2).
Algorithm Stability:
Stability is a data set, assuming that there are two of data equal, after the end of the sorting, if the position of the two data changes, then the sorting method is not stable. Bubble sort Process total, only a continuous two data will exchange data, that is, two of the same data is not a location exchange, so bubble sort is a stable sorting method. Direct selection of the sorting process, two data at any location can be swapped position, so the direct selection of sorting is an unstable sorting method.
Algorithm |
Average Time complexity |
Worst-case complexity of time |
Complexity of space |
Stability |
Bubble sort |
O (n^2) |
O (n^2) |
O (1) |
Stability |
Direct Select sort |
O (n^2) |
O (n^2) |
O (1) |
Not stable |
Of all the algorithms, none of them is optimal. In terms of time complexity bubble sort and direct selection of sorting, time cost is relatively large o (n^2), with sort data, if the initial sequence is already basically ordered, then the use of bubble sorting method is better, its time complexity close to O (n), in the actual situation, we should choose different algorithm according to different circumstances, If necessary, multiple algorithms can be used together.
Bubble sort vs Direct Select sort