-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort.html
More file actions
59 lines (58 loc) · 1.53 KB
/
Sort.html
File metadata and controls
59 lines (58 loc) · 1.53 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<script>
function bubble(a) {
let sorted = true;
for (let i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) {
sorted = false;
let temp = a[i];
a[i] = a[i+1];
a[i+1] = temp;
// swap a[i] and a[i + 1]
//bubble switches the numbers next to each other
// you have to run bubble multiple times in order to sort an array
}
}
return sorted;
}
function bubbleSort(a){
while(!bubble(a)){
}
return a;
}
function selectionSort(a) {
for (let i = 0; i < a.length; i++) {
// Elements 0...i-1 are the smallest i elements in the array.
// Now find the next smallest element.
let min = Infinity;
for (let j = i; j < a.length; j++) {
if (a[j] < min){
min = a[j];
let temp = a[i];
a[i] = a[j];
a[j] = temp;
// swap a[i] and a[j]
}
}
}
}
function test(a){
console.log('----');
console.log('input ', a);
selectionSort(a);
console.log('output', a);
let ordered = true;
for (let i = 0; i < a.length - 1; i++) {
if (a[i] > a[i + 1]) ordered = false;
}
console.log('is ordered:', ordered);
}
test([8, 2, 5, 7, 1, 7, 22]);
test([1, 2, 3, 4, 5]);
test([5, 4, 3, 2, 1]);
test([100, 100, 100, 100, 100, 100]);
let a = [];
for (let i = 0; i < 100; i++) {
a[i] = Math.floor(Math.random() * 9000) + 1000;
}
test(a);
</script>