the common internal sorting algorithms are: Insert Sort, hill sort, select sort, bubble sort, merge sort, quick sort, heap sort, cardinality sort and so on. Summarize with a picture:
650) this.width=650; "Src=" https://s2.51cto.com/wyfs02/M00/95/C5/wKioL1kZfUqAW6T8AAHcPIbdiTU442.png-wh_500x0-wm_ 3-wmp_4-s_1544906761.png "title=" 12.png "alt=" Wkiol1kzfuqaw6t8aahcpibditu442.png-wh_50 "/>
Select Sort
Select Sort is a simple and intuitive sorting algorithm, which is the time complexity of O (n) regardless of what data goes in. So when it's used, the smaller the data, the better. The only advantage might be that you don't take up extra memory space. In layman's terms, who is the youngest of you, standing on the last side of the queue, and then continue to the rest of the unordered array of the smallest of you who will be out of line, stand to the last side of the queue, until the last one, continue to stand to the last side, so that the array has a sequence, from small to large.
1. Algorithm steps
A, first find the smallest (large) element in the unordered sequence, and store it in the starting position of the sort sequence
B, Then continue looking for the smallest (large) element from the remaining unsorted elements and place it at the end of the sorted sequence.
C, repeat the second step until all the elements are sorted.
2. Motion Diagram Demo
650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M00/95/C5/wKioL1kZfYiR-GAjAAAs5uKwBWE463.png-wh_500x0-wm_ 3-wmp_4-s_825999891.png "title=" 13.png "alt=" Wkiol1kzfyir-gajaaas5ukwbwe463.png-wh_50 "/>
3.JavaScript Code implementation
Functionselectionsort (arr) { var len =arr.length; var minIndex, temp; for (var i =0; i < len -1; i++) { minIndex = i; for (var j = i +1; j < len; j++) { if (Arr[j] < arr[minindex]) { // Find the smallest number minIndex = j; // Save the index of the minimum number } } temp = arr[i]; arr[i] = arr[ minindex]; arr[minindex]= temp; } return arr;}
4.Python Code implementation
Defselectionsort (arr): For I InRange (Len (arr)-1): for J InRange (I+1, Len (arr)): if ARR[J] < Arr[i]: Arr[i], arr[j] = Arr[j], Arr[i] return arr
5. Java Code Implementation
Publicstaticvoidselectsort (int[] numbers) { int size = numbers.length; //array length int temp = 0 ; //intermediate variable for (int i = 0 ; i <size ; i++) { int k = i; //location to be determined //Select the number for (int) that should be in the first position. j = size -1 ; j> i ; j--) { if (Numbers[j] < numbers[k]) { k = j; } } //Exchange two numbers temp = numbers[i]; numbers[i] = numbers[k]; numbers[k] = temp; } }
6. Go Code Implementation
Funcselectionsort (Arr []int) []int { length:=len (arr) fori:=0; i < length-1; i++ { min:= i forj:= i + 1; j < length; j++ { if arr[min] > arr[j] { min = j } } arr[i], arr[min] = arr[min], arr[i] } return arr}
Hope to communicate with the technology, there is an interest can add QQ invite into the group: 525331804 full stack technology development QQ Group:581993430
This article is from the "Technology-aware" blog, so be sure to keep this source http://liuzhiying.blog.51cto.com/5850988/1925993
"Quick sort" of JavaScript, Python Java, go algorithm series