Skip to content

Added Processing language + BogoSort implementation #436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@
"lang": "lolcode",
"name": "LOLCODE"
},
{
"lang": "pde",
"name": "Processing"
},
{
"lang": "piet",
"name": "Piet"
Expand Down
16 changes: 15 additions & 1 deletion contents/bogo_sort/bogo_sort.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ In code, it looks something like this:
[import:10-14, lang:"crystal"](code/crystal/bogo.cr)
{% sample lang="r" %}
[import:1-6, lang:"r"](code/r/bogo_sort.r)
{% sample lang="pde" %}
[import:55-59, lang:"java"](code/processing/bogoSort.pde)
{% sample lang="scala" %}
[import:12-16, lang:"scala"](code/scala/bogo.scala)
{% sample lang="go" %}
Expand All @@ -75,6 +77,17 @@ That's it.
Ship it!
We are done here!

Here's a visual representation of what's happening:

<p>
<img class="center" src="res/bogo.gif" width="500" />
</p>


You can check out the code for this visualization by changing the language to `Processing`.
Here, we see that the array shuffles about at random until it eventually finds the correct result.
Obviously, bogo sort is almost impossible with a large array.

## Example Code

{% method %}
Expand Down Expand Up @@ -133,13 +146,14 @@ We are done here!
[import, lang:"crystal"](code/crystal/bogo.cr)
{% sample lang="r" %}
[import, lang:"r"](code/r/bogo_sort.r)
{% sample lang="pde" %}
[import:1-53, lang:"java"](code/processing/bogoSort.pde)
{% sample lang="scala" %}
[import, lang:"scala"](code/scala/bogo.scala)
{% sample lang="go" %}
[import, lang:"go"](code/go/bogo_sort.go)
{% endmethod %}


<script>
MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
</script>
Expand Down
59 changes: 59 additions & 0 deletions contents/bogo_sort/code/processing/bogoSort.pde
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
int[] test_array = new int[5];

void setup() {
size(400,400);
stroke(255);
fill(0);

randomArray(test_array,0,100);
}

void draw(){
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dumb question about processing. Does everything require a visual output?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not a requirement but Processing language was created for that kind of task.
I found it nice to visualize the sorting process.

background(255);

int w = width/test_array.length , minimum = min(test_array) , maximum = max(test_array);
for(int i=0;i<test_array.length;i++){
rect(w*i,height,w,-map(test_array[i],minimum,maximum,5,height-5));
}

if (isSorted(test_array)){
noLoop();
println("Finished in "+ millis()/1000 +" seconds.");
}else{
randomize(test_array);
}
}


void randomArray(int[] array , int min , int max){
for(int i=0;i<array.length;i++){
array[i] = (int) random(min,max);
}
}

void swap(int[] array, int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}


void randomize(int[] array) {
for (int i = array.length-1; i>=1 ; i--) {
int j = (int) random(0, i);
swap(array,i,j);
}
}

Boolean isSorted(int[] array){
for(int i=0;i<array.length-1;i++){
if (array[i]>array[i+1]) return false;
}
return true;
}

void bogoSort(int[] array){
while (! isSorted(array)){
randomize(array);
}
}
Binary file added contents/bogo_sort/res/bogo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.