Insert sort
Insert sorting is implemented as follows:
First, create an empty list to save the ordered series (called "ordered List ").
Extract a number from the original sequence and insert it into the "ordered list" to keep it in order.
Repeat Step 2 until the original number column is empty.
The average time complexity of insertion sorting is in the square level, which is inefficient but easy to implement. With the idea of "gradually expanding the results", it gradually increases the length of the ordered list until its length is equal to the length of the original list.
(From Baidu encyclopedia)
Javascript code
Copy codeThe Code is as follows:
<Script type = "text/javascript">
Var org = [5, 4, 3, 2, 1, 6, 7, 9, 8, 10];
Var tempArr = new Array ();
For (var I = 0; I <org. length; I ++ ){
If (I = 0 ){
TempArr [0] = org [0]; // place the first element in the new sequence
} Else {
For (var j = 0; j <tempArr. length; j ++ ){
If (org [I]> tempArr [j]) {
// If it is the last element of the new sequence, insert
If (j = tempArr. length-1 ){
TempArr [j + 1] = org [I];
Break;
} Else {// if it is not the last element, move it back
Continue;
}
} Else {
// Move the new sequence and insert it
For (var k = tempArr. length-1; k> = j; k --){
TempArr [k + 1] = tempArr [k];
}
TempArr [j] = org [I];
Break;
}
}
}
}
Alert (tempArr );
</Script>