Skip to content

Commit 058aa33

Browse files
committed
Update insertion sort
1 parent c136d73 commit 058aa33

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

Diff for: src/java1/sorting/insertionSort/InsertionSort.class

1.32 KB
Binary file not shown.

Diff for: src/java1/sorting/insertionSort/InsertionSort.java

+5-7
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,18 @@
33
class InsertionSort {
44

55
private int[] insertionSort(int array[]) {
6-
int size = array.length;
76

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];
109
int j = i - 1;
1110

1211
// 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--) {
1413
array[j + 1] = array[j];
15-
--j;
1614
}
1715

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;
2018
}
2119
return array;
2220
}

Diff for: src/javascript/sorting/insertionSort/insertionSort.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function insertionSort(array) {
22
for(let i = 1; i < array.length; i++) {
33
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];
66
}
77
array[j+1] = temp;
88
}

0 commit comments

Comments
 (0)