Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // By: Sherol Chen
- // Animating the Sorted Rectangles
- import acm.graphics.*;
- import acm.program.*;
- public class MyVisualSort extends GraphicsProgram{
- private int[] array = {23, 42, 55, 64, 70, 10, 33, 44, 20};
- public static void main(String[] args) {
- new myvisualsort().start(args);
- }
- public void run() {
- while(true)
- {
- for (int i=0; i<array.length; i++)
- for (int j=i; j<array.length-1; j++)
- if ( array[j] > array[j+1])
- {
- swap(array, j, j+1); // generic swap function
- updateGraphics(array); // however you drew it
- }
- }
- }
- // Good old swap function
- private void swap (int[] a, int x, int y)
- {
- int poopstall = a[x];
- a[x] = a[y];
- a[y] = poopstall;
- }
- private void updateGraphics(int[] a)
- {
- removeAll(); // clear canvas
- for (int i=0; i<a.length; i++)
- {
- // just doing basic scaling of the data
- GRect rect = new GRect(i*50 + 200, 0, 10, a[i]*5);
- add(rect);
- }
- pause(200);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement