JavaScript sorting algorithm of choice

Process:
the starting position of the first cycle is defined as the minimum default position again
from the next position of the start position, execution cycle
if the value is less than the value at the position on the storage position of the index
on an index value stored in the position
loop end, comparing the stored index, if the index is the starting position
and if not, the exchange value of the two positions on
the minimum cycle will be present, is placed in the starting position of the cycle of
performing multiple cycles to complete the sorting
procedure:

var arr=[....]
// 外层循环,实现排序循环次数,次数是单元个数 -1
for(j=0;j<arr.length-1;j++){
// 先默认起始位置就是最小值位置,存储起始位置的索引,也就是 j
      // 之前的起始位置,不参与下一次循环
var min=j;
// 默认的最小值位置是当前循环的起始位置,是j
// 比较,要从下一个位置开始,内层循环起始,是比较的起始位置+1开始循环 
for(i=j;i<arr.length;i++){
// 如果有单元的数值,小于存储的索引对应的数值
if(arr[min]>arr[i]){
//变量中,存储当前较小值的索引下标
// 不是做数据交换,是存储这个数值位置的索引下标
min=i
}
}
// 内层循环执行完毕,存储的索引下标如果不是起始的索引j
// 就交换 min中存储的索引下标对应的数值 和 j索引下标应的数值
if(min!=j){
var a=0;
a=arr[i];
arr[i]=arr[min];
arr[min]=a;
}
}
console.log(arr);

Core:
find the minimum index, the starting position of the data exchange
to go first index, the exchange of data
optimization:
1. out before the comparison value does not participate in the next comparison
2.n units, n-1 times as long as Comparative

Selection sort:
If the order of occurrence of the problem, have to perform the operation assignment index
like the cycle is completed, judgment is performed, to make a data exchange

Published 21 original articles · won praise 3 · Views 324

Guess you like

Origin blog.csdn.net/qq_44531034/article/details/104979596