This repository has been archived by the owner on Feb 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorting.pde
79 lines (71 loc) · 1.84 KB
/
sorting.pde
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
import processing.sound.*;
Float[] toSort = new Float[500];
int swapCount = 0;
int compCount = 0;
int timeWarp = 1;
SinOsc sine;
SortingAlg alg;
long startTime;
long endTime;
void setup(){
colorMode(HSB, 360, 100, 100, 100);
size(1000,600);
ArrayList<Float> list = new ArrayList<Float>();
for (int i = 1; i < toSort.length+1; i++){
list.add((float)i/toSort.length*height);
}
for (int i = 0; i < toSort.length; i++){
int ind = floor(random(0,list.size()));
toSort[i] = list.get(ind);
list.remove(ind);
}
//Change this line to use another algorithm to sort your data
startTime = System.currentTimeMillis();
alg = new Mergesort<Float>(toSort);
//alg.execute();
//endTime = System.currentTimeMillis();
//println(endTime-startTime);
sine = new SinOsc(this);
sine.play();
}
void draw(){
background(51);
for (int timer = 0; timer < timeWarp; timer++){
alg.executeStep();
}
sine.freq(alg.freqToPlay()/height * 400 + 100);
if(alg.finished){
noLoop();
println("finished");
sine.stop();
}
float w = (float) width / toSort.length;
if (w<5){
noStroke();
}else{
stroke(0);
strokeWeight(1);
}
for (int i = 0; i < toSort.length; i++){
float x = w * i;
float curval = toSort[i];
fill(alg.colorForIndex(i));
noStroke();
if (alg.finished) fill(100,10,100);
rect(x,height-curval,w,curval);
}
fill(200);
textSize(20);
textAlign(LEFT,TOP);
text("Swaps: " + ((Integer) swapCount).toString(), 10, 5);
text("Comps: " + ((Integer) compCount).toString(), 10, 25);
text("Frame: " + ((Integer) frameCount).toString(), 10, 45);
text("Warp: " + ((Integer) timeWarp).toString(), 10, 65);
}
void mouseWheel(MouseEvent event){
timeWarp -= event.getCount();
if (timeWarp > 500){
timeWarp -= event.getCount()*9;
}
timeWarp = constrain(timeWarp,1,5000);
}