-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinsertionSort.js
More file actions
37 lines (33 loc) · 887 Bytes
/
insertionSort.js
File metadata and controls
37 lines (33 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
function insertSort(arr) {
for (let i = 1; i < arr.length; i++) {
let currentVal = arr[i]
console.log("currentVal: ", currentVal)
for (let j = i-1; j >= 0; j--) {
if (arr[j] > currentVal) { // currentVal 보다 큰 경우
arr[j+1] = arr[j]
if (j == 0) { // 배열의 맨앞까지 온 경우
arr[0] = currentVal
}
} else { // currentVal 보다 작은 경우
arr[j+1] = currentVal
break;
}
}
console.log(arr)
}
}
function insertionSort(arr){
var currentVal;
for(var i = 1; i < arr.length; i++){
currentVal = arr[i];
console.log(currentVal)
for(var j = i - 1; j >= 0 && arr[j] > currentVal; j--) {
arr[j+1] = arr[j]
}
arr[j+1] = currentVal;
console.log(arr)
}
return arr;
}
let arr = [5, 3, 1, 4, 2]
insertSort(arr)