File tree 3 files changed +7
-9
lines changed
java1/sorting/insertionSort
javascript/sorting/insertionSort
3 files changed +7
-9
lines changed Original file line number Diff line number Diff line change 3
3
class InsertionSort {
4
4
5
5
private int [] insertionSort (int array []) {
6
- int size = array .length ;
7
6
8
- for (int i = 1 ; i < size ; i ++) {
9
- int key = array [i ];
7
+ for (int i = 1 ; i < array . length ; i ++) {
8
+ int temp = array [i ];
10
9
int j = i - 1 ;
11
10
12
11
// Compare key with each element on the left of it until an element is smaller than it
13
- while ( j >= 0 && key < array [j ]) {
12
+ for (; j > - 1 && array [j ] > temp ; j -- ) {
14
13
array [j + 1 ] = array [j ];
15
- --j ;
16
14
}
17
15
18
- // Place key at after the element just smaller than it.
19
- array [j + 1 ] = key ;
16
+ // Place temp at after the element just smaller than it.
17
+ array [j + 1 ] = temp ;
20
18
}
21
19
return array ;
22
20
}
Original file line number Diff line number Diff line change 1
1
function insertionSort ( array ) {
2
2
for ( let i = 1 ; i < array . length ; i ++ ) {
3
3
let temp = array [ i ] ;
4
- for ( var j = i - 1 ; array [ j ] > temp && j > - 1 ; j -- ) {
5
- array [ j + 1 ] = array [ j ]
4
+ for ( var j = i - 1 ; j > - 1 && array [ j ] > temp ; j -- ) {
5
+ array [ j + 1 ] = array [ j ] ;
6
6
}
7
7
array [ j + 1 ] = temp ;
8
8
}
You can’t perform that action at this time.
0 commit comments