-
Notifications
You must be signed in to change notification settings - Fork 0
/
selectionSort.js
81 lines (68 loc) · 1.66 KB
/
selectionSort.js
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
var lines = [];
var sSortCanvas;
var n;
var j;
var minEleIndex;
var selectionButton;
var selectionLoop;
function setup(){
sSortCanvas = createCanvas(1000, 800);
sSortCanvas.parent("sCanvas");
for (var i = 0; i<width; i++){
lines[i] = random(0, height);
}
n = 0;
j = 0;
minEleIndex = 0;
selectionLoop = false;
console.log(selectionLoop);
selectionButton = document.getElementById("selection");
selectionButton.onclick = function proceedLoop(){
selectionLoop = true
console.log(selectionLoop);
}
}
function draw(){
background(0);
if (selectionLoop){
for (var i = 0; i<lines.length; i++){
if (i%2==0){
stroke(155);
strokeWeight(5);
line(i, height, i, height-lines[i]);
}else{
stroke(0);
strokeWeight(5);
line(i, height, i, height-lines[i]);
}
}
if (n<lines.length){
var minEleIndex = n;
for (var j = n; j < lines.length; j++){
if (lines[minEleIndex]>lines[j]){
minEleIndex = j;
}
}
var temp = lines[n];
lines[n] = lines[minEleIndex];
lines[minEleIndex] = temp;
n+=1;
}else{
noLoop();
}
}
}
function slectionSort(){
for (var i = 0; i < lines.length; i++){
var minEleIndex = i;
for (var j = i; j < lines.length; j++){
if (lines[minEleIndex]>lines[j]){
minEleIndex = j;
}
}
//swap
var temp = lines[i];
lines[i] = lines[minEleIndex];
lines[minEleIndex] = temp;
}
}