File tree Expand file tree Collapse file tree 1 file changed +4
-3
lines changed
src/main/java/ir/sk/algorithm/others Expand file tree Collapse file tree 1 file changed +4
-3
lines changed Original file line number Diff line number Diff line change @@ -111,18 +111,19 @@ public static void selectionSort(int[] array) {
111
111
@ Stability
112
112
public static void insertionSort (int [] array ) {
113
113
for (int i = 1 ; i < array .length ; i ++) {
114
+ int key = array [i ];
114
115
int j = i - 1 ;
115
116
116
117
/* Move elements of arr[0..i-1], that are
117
118
greater than key, to one position ahead
118
119
of their current position
119
120
searching and shift at once */
120
- while (j >= 0 && array [j ] > array [ i ] ) {
121
+ while (j >= 0 && array [j ] > key ) {
121
122
// using rotate by condition(array[j] > key), no swap, since shifting has better performance than swap
122
123
array [j + 1 ] = array [j ];
123
- j = j - 1 ;
124
+ j -= 1 ;
124
125
}
125
- array [j + 1 ] = array [ i ] ;
126
+ array [j + 1 ] = key ;
126
127
}
127
128
}
128
129
You can’t perform that action at this time.
0 commit comments