Skip to content

Commit bcd6a56

Browse files
author
johhnry
committed
Added Processing language + BogoSort
1 parent 0fb0325 commit bcd6a56

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

book.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@
139139
{
140140
"lang": "f90",
141141
"name": "Fortran90"
142+
},
143+
{
144+
"lang": "processing",
145+
"name": "Processing"
142146
}
143147
]
144148
}

contents/bogo_sort/bogo_sort.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ In code, it looks something like this:
4343
[import:16-18, lang:"nim"](code/nim/bogo_sort.nim)
4444
{% sample lang="ruby" %}
4545
[import:12-16, lang:"ruby"](code/ruby/bogo.rb)
46+
{% sample lang="processing" %}
47+
[import:55-59, lang:"java"](code/processing/bogoSort.pde)
4648
{% endmethod %}
4749

4850
That's it.
@@ -85,6 +87,8 @@ We are done here!
8587
[import, lang:"nim"](code/nim/bogo_sort.nim)
8688
{% sample lang="ruby" %}
8789
[import, lang:"ruby"](code/ruby/bogo.rb)
90+
{% sample lang="processing" %}
91+
[import:1-53, lang:"java"](code/processing/bogoSort.pde)
8892
{% endmethod %}
8993

9094

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
int[] test_array = new int[5];
2+
3+
void setup() {
4+
size(400,400);
5+
stroke(255);
6+
fill(0);
7+
8+
random_array(test_array,0,100);
9+
}
10+
11+
void draw(){
12+
background(255);
13+
14+
int w = width/test_array.length , minimum = min(test_array) , maximum = max(test_array);
15+
for(int i=0;i<test_array.length;i++){
16+
rect(w*i,height,w,-map(test_array[i],minimum,maximum,5,height-5));
17+
}
18+
19+
if (isSorted(test_array)){
20+
noLoop();
21+
println("Finished in "+ millis()/1000 +" seconds.");
22+
}else{
23+
randomize(test_array);
24+
}
25+
}
26+
27+
28+
void random_array(int[] array , int min , int max){
29+
for(int i=0;i<array.length;i++){
30+
array[i] = (int) random(min,max);
31+
}
32+
}
33+
34+
void swap(int[] array, int i, int j) {
35+
int temp = array[i];
36+
array[i] = array[j];
37+
array[j] = temp;
38+
}
39+
40+
41+
void randomize(int[] array) {
42+
for (int i = array.length-1; i>=1 ; i--) {
43+
int j = (int) random(0, i);
44+
swap(array,i,j);
45+
}
46+
}
47+
48+
Boolean isSorted(int[] array){
49+
for(int i=0;i<array.length-1;i++){
50+
if (array[i]>array[i+1]) return false;
51+
}
52+
return true;
53+
}
54+
55+
void bogo_Sort(int[] array){
56+
while (! isSorted(array)){
57+
randomize(array);
58+
}
59+
}

0 commit comments

Comments
 (0)