-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathRadix Sort.html
More file actions
53 lines (47 loc) · 1.18 KB
/
Radix Sort.html
File metadata and controls
53 lines (47 loc) · 1.18 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<script>
function radixSort(arr) {
let bucket = [],
len = arr.length,
max = arr[0],
loop, str, i, j, k, t;
for (let num of arr) {
if (num > max) {
max = num
}
}
loop = String(max).length;
for (i = 0; i < len; i++) {
bucket[i] = []
}
for (i = 0; i < loop; i++) {
for (j = 0; j < len; j++) {
str = String(arr[j]);
if (str.length >= i + 1) {
k = str[str.length - i - 1];
bucket[k].push(arr[j])
} else {
bucket[0].push(arr[j])
}
}
arr.splice(0, len);
for (j = 0; j < len; j++) {
t = bucket[j].length;
for (k = 0; k < t; k++) {
arr.push(bucket[j][k])
}
bucket[j] = []
}
}
console.log(arr)
}
radixSort([36, 9, 0, 25, 1, 49, 64, 16, 81, 4])
</script>
</body>
</html>