Heroic Bronze algorithm Ideas
Two arrays: arrays to be sorted disorderArray
and ordered arraysorderArray
Take a number from the array you want to sortdisorderArray[i]
Comparison of each number in sequence and order array
When less than element orderArray[j]
j
move the position and subsequent elements back one
and insert it into position j
.
has never been less than,
is inserted directly into the orderArray[orderArray.length]
Attention
disorderArray[i]
and orderArray[j]
when comparing:
From left to right, conditiondisorderArray[i]<orderArray[j]
From right to left, conditionsdisorderArray[i]>orderArray[j]
If you add equals, you can compare one less time, but do it one more time.
Algorithm implementation
function InsertSort(disorderArray) { var i, j, k; var orderArray = []; //第0个元素不用排序 orderArray[0] = disorderArray[0]; //从第1个元素开始,依次取出待排序数组中的元素 for (i = 1; i < disorderArray.length; i++) { //依次和有序数组元素比较 for (j = 0; j < orderArray.length; j++) { //小于等于元素,则找到了要插入的位置orserArray[j] if (disorderArray[i] < orderArray[j]) { break; } //如果j == orderArray.length,那么disorderArray[i]是最大的 //直接插入到j位置,不用再移动orderArray } //将位置j及之后的元素后移一位,j == orderArray.length时正好跳过循环 for (k = orderArray.length; k > j; k--) { //有序数组会增1,从length+1开始从后向前到j插入到j+1 orderArray[k] = orderArray[k - 1]; } //插入到合适的位置 orderArray[j] = disorderArray[i]; } console.log(orderArray);}var disorderArray = [5, 3, 8, 1, 6, 4];InsertSort(disorderArray);
The idea of unyielding silver algorithm
An array:orderArray
First, it will be orderArray[0]
considered an ordered array
Then orderArray[1]
insert into the ordered array from the beginning
The goal is to insert all the pending parts into the ordered part
Algorithm implementation
function InsertSort(orderArray) { //辅助交换 var temp; //依次取出待排序部分的元素orderArray[i],从1开始 for (var i = 1; i < orderArray.length; i++) { //待排部分和有序部分从右向左进行比较 for (var j = i - 1; j >= 0; j--) { //待排元素大于则插入,找到要插入的位置j+1 if (orderArray[i] >= orderArray[j]) { break; } } temp = orderArray[i]; //待排元素位置到插入位置之间的元素需要移动 for (i; i > j + 1; i--) { orderArray[i] = orderArray[i - 1]; } orderArray[j + 1] = temp; }}var orderArray = [9, 3, 7, 1, 4, 2, 5, 0, 6, 8]InsertSort(orderArray);console.log(orderArray);
Glory Gold Algorithm Ideas
When the rank and order parts are compared
If the pending element is less than the ordered element, the ordered element is moved back
If the pending element is greater than or equal to an ordered element, the position of the element to be sorted is determined and inserted
Jump out of this cycle
The goal is still to insert the pending elements into the ordered part
Attention
Compare conditions to write for
in loops
function InsertSort(orderArray) { //辅助交换 var temp; //依次取出待排序部分的元素orderArray[i],从1开始 for (var i = 1; i < orderArray.length; i++) { temp = orderArray[i]; //待排部分和有序部分从右向左进行比较 for (var j = i - 1; j >= 0 && temp < orderArray[j]; j--) { //待排元素小于则后移 orderArray[j + 1] = orderArray[j]; } //待排元素插入 orderArray[j + 1] = temp; }}var orderArray = [9, 3, 7, 1, 4, 2, 5, 0, 6, 8]InsertSort(orderArray);console.log(orderArray);
The idea of luxurious platinum algorithm
When the rank and order parts are compared
If the pending element is less than the ordered element, the two are exchanged for position and continue to compare
If the pending element is greater than or equal to the ordered element, the position of the element to be sorted is determined, jumping out of this loop
The goal is still to insert the pending elements into the ordered part
Attention
A comparison of only two elements at a time
One of them is a pending element.
This approach may not be directly inserted into the sort
Algorithm implementation
function InsertSort(orderArray) { //辅助交换 var temp; //依次取出待排序部分的元素orderArray[i],从1开始 for (var i = 1; i < orderArray.length; i++) { temp = orderArray[i]; //待排部分和有序部分从右向左进行比较 for (var j = i - 1; j >= 0; j--) { //待排元素小于则交换位置 if (temp < orderArray[j]) { orderArray[j + 1] = orderArray[j]; orderArray[j] = temp; } else { break; } } }}var orderArray = [9, 3, 7, 1, 4, 2, 5, 0, 6, 8]InsertSort(orderArray);console.log(orderArray);
Direct Insert Sort