-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort-select.html
More file actions
33 lines (28 loc) · 906 Bytes
/
sort-select.html
File metadata and controls
33 lines (28 loc) · 906 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
</body>
<script>
const select_sort = (array) => {
let len = array.length
// 一个长度为n的数组 实际上只需要循环 n-1次,最后选完n-1个最小值,剩下的肯定是最大值。所以是len-2
for (let i = 0; i < len - 2; i++) {
console.log('i:', i);
for (let j = i; j < len - 1; j++) {
console.log('j:', j);
if (array[i] > array[j]) {
[array[i], array[j]] = [array[j], array[i]]
}
}
}
return array
}
console.log('select_sort: ', select_sort([1, 3, 3, 4, 5, 2, 6]));
</script>
</html>