This is a creation in Article, where the information may have evolved or changed.
Common internal sorting algorithms are: insert sort, hill sort, select sort, bubble sort, merge sort, quick sort, heap sort, cardinality sort , etc. Summarize with a picture:
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. Continue looking for the smallest (large) element from the remaining unsorted elements, and then place it at the end of the sorted sequence.
C, repeat the second step until all the elements are sorted.
2. Dynamic Diagram Demonstration
3. JavaScript Code Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
functionselectionSort(arr) { varlen =arr.length; varminIndex, temp; for(vari =0; i < len -1; i++) { minIndex = i; for(varj = i +1; j < len; j++) { if(arr[j] < arr[minIndex]) { // 寻找最小的数 minIndex = j; // 将最小数的索引保存 } } temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex]= temp; } returnarr; } |
4. Python Code Implementation
1 2 3 4 5 6 |
defselectionSort(arr):
fori inrange(len(arr)-1):
forj inrange(i+1, len(arr)):
ifarr[j] < arr[i]:
arr[i], arr[j] =arr[j], arr[i]
returnarr
|
5. Java Code Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
publicstaticvoidselectSort(int[] numbers){ intsize = numbers.length; //数组长度 inttemp = 0; //中间变量 for(inti = 0; i <size ; i++){ intk = i; //待确定的位置 //选择出应该在第i个位置的数 for(intj = size -1; j> i ; j--){ if(numbers[j] < numbers[k]){ k = j; } } //交换两个数 temp = numbers[i]; numbers[i] = numbers[k]; numbers[k] = temp; } } |
6. Go Code Implementation
1 2 3 4 5 6 7 8 9 10 11 12 13 |
funcselectionSort(arr []int) []int { length:=len(arr) fori:=0; i < length-1; i++ { min:= i forj:= i + 1; j < length; j++ { ifarr[min] > arr[j] { min = j } } arr[i], arr[min] = arr[min], arr[i] } returnarr } |
Hope to communicate with the technology, there is interest can add QQ invite into the group: 525331804 full stack technology development QQ Group: 581993430
Login to the http://www.learnbo.com/Academy website
Or follow our official Weibo blog, and more!
This article is from the "Technology-aware" blog, so be sure to keep this source http://liuzhiying.blog.51cto.com/5850988/1925997