A visual sorting program

Also used acm.java.

This uses selection sort, one of the easier (but a bit inefficient) ones.

 

It checks all the elements in an unsorted array… (marked blue when checking)

visualsort sorting

 

 

and selects the appropriate one, and inserts in the proper order. The result below:

visualsort sorted

 

what  I did was quite inefficient but it worked.

I had two arrays: one an int array to store an actual number array, and the other an array of GRects.

The height value of the GRects would match the numbers in the int array.

In the sort method part that actually sorted stuff, I sorted not the numbers but compared the GRects’ height values instead. The coding got a bit dirty as as result, but programming the bars to change color was much more easier this way.

The fact that the (0,0) point of the acm package lies in the top left created a lot of bugs. In order to pull the bars to the ground, I had to subtract the canvas size and then add the height values.

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s