/
bubble-sort.html
93 lines (64 loc) · 1.73 KB
/
bubble-sort.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
<!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,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,0,arr.length-1,function(i,j){
var aux;
if(arr[j] > arr[j+1]) {
aux = arr[j]
arr[j] = arr[j+1]
arr[j+1] = aux
}
draw()
})
}
function doIteration(i,j,max,callback) {
callback(i,j)
if(++j>=max) {
j = 0
i++
}
if(i<max) window.setTimeout(function(){
doIteration(i,j,max,callback)
},200)
}
function draw() {
cuba.select('#result').html( arr.join(",") )
}
function reset() {
arr = [9,8,7,6,5,4,3,2,1,0]
cuba.select('#result').html( arr.join(",") )
}
</script>
</head>
<body>
<h1>Bubble Sort Visualization</h1>
<a href="#" id="start">START</a> | <a href="#" id="reset">RESET</a>
<div id="result"></div>
</div>
</body>
</html>