-
Notifications
You must be signed in to change notification settings - Fork 0
/
중앙값 구하기.js
39 lines (35 loc) · 919 Bytes
/
중앙값 구하기.js
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
38
39
function solution(array) {
/*
// Bubble sort
for (let i = array.length; i > 0; i--) {
for (let j = 0; j < i - 1; j++) {
if (array[j] > array[j + 1]) {
[array[j], array[j + 1]] = [array[j + 1], array[j]]
}
}
}
*/
/*
// Selection sort
for (let i = 0; i < array.length; i++) {
let min = i;
for (let j = i + 1; j < array.length; j++) {
if (array[j] < array[min]) {
min = j;
}
}
[array[i], array[min]] = [array[min], array[i]];
}
*/
// Insertion sort
for(let i = 1; i < array.length; i++){
let cur = array[i];
let j = i - 1;
while(j >= 0 && array[j] > cur){
array[j + 1] = array[j];
j--;
}
array[j + 1] = cur;
}
return array[Math.floor(array.length / 2)]
}