/
select-by-min.html
102 lines (71 loc) · 1.94 KB
/
select-by-min.html
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>cuba.js micro JavaScript Framework</title>
<style type="text/css">
h1,#result{
font-size: 90px;
text-align: center
}
</style>
<script type="text/javascript" src="src/cuba.js"></script>
<script type="text/javascript">
var arr = [9,8,7,6,5,4,3,-1,-2,2,1,0],
$ = function( id ) { return cuba.grab(id); }
//if DOM is Ready
cuba.ready(function() {
draw()
cuba.select('#start').on('click', function( event ){
cuba.stopPropagation( event )
bubbleSort()
})
cuba.select('#reset').on('click', function( event ){
cuba.stopPropagation( event )
reset()
})
})
function bubbleSort() {
doIteration(0, 1, arr.length-1, function(i,j){
var aux;
if( i == j) return
if(arr[i] > arr[j]) {
aux = arr[i]
arr[i] = arr[j]
arr[j] = aux
}
draw()
})
}
function doIteration(i,j,max,callback) {
var k = i,
min = arr[i];
for(a=j;a<=max;a++) {
if(arr[a] < min) {
min = arr[a]
k = a
}
}
console.log(i+'-'+k)
callback(i,k)
i++;
if(i<max) window.setTimeout(function(){
doIteration(i,j+1,max,callback)
}, 1200)
}
function draw() {
cuba.select('#result').html( arr.join(",") )
}
function reset() {
arr = [9,8,7,6,5,4,3,-1,-2,2,1,0]
cuba.select('#result').html( arr.join(",") )
}
</script>
</head>
<body>
<h1>Select By Min Sort Visualization</h1>
<a href="#" id="start">START</a> | <a href="#" id="reset">RESET</a>
<div id="result"></div>
</div>
</body>
</html>