insanely_sane 7 Junior Poster in Training

Hey guys. I am currently just a 11th grade student but since I'm done the whole curriculum for my Computers class already, I recently just started making extra stuff for fun. One of things I'm doing is Making a graphical/visual sorting applet. Something like : http://www.cs.ubc.ca/~harrison/Java/sorting-demo.html

I have my code for insertion, shaker, bubble and selection sorts. But since I haven't done merge sort yet, I was wondering if anyone could help me out and give me the code.
I use an IDE (Ready to program) with Java version 1.4.2 (If that helps ... )
This is what I have so far:

import java.awt.*;
import java.applet.*;

public class SortAnimator extends Applet
{
    Button select, bubble, insert;
    int size = 50;
    int a[] = new int [size];


    Button reset;

    public void init ()
    {
        resize (900, 900);
        for (int i = 0 ; i < size ; i++)
        {
            if (i == 0)
                a [0] = 10;
            else
                a [i] = a [i - 1] + 10;
        }

        // Randomize the array by swapping 2 random elements
        for (int i = 1 ; i <= 1000000 ; i++)
        {
            int index1 = (int) ((Math.random () * (size - 1)));
            int index2 = (int) ((Math.random () * (size - 1)));
            while (index1 == index2)
            {
                index2 = (int) ((Math.random () * (size - 1)));
            }
            int temp = a [index1];
            a [index1] = a [index2];
            a [index2] = temp;
        }
        select = new Button ("selection sort");
        add (select);
        bubble = new Button ("bubble sort");
        add (bubble);
        insert = new Button ("insertion sort");
        add (insert);
        reset = new Button ("Reset array");
        add (reset);


    }


    public boolean action (Event e, Object o)
    {
        if (e.target == select)
        {
            selectionSort (a, size);

        }
        else if (e.target == bubble)
        {
            bubble (a, size);
        }
        else if (e.target == insert)
        {
            insertion (a,size);
        }
        else if (e.target == reset)
        {
            reset ();
        }
        return true;
    }


    public void paint (Graphics g)
    {

        printarray (a, size);
    }


    public void printarray (int a[], int size)
    {
        Graphics g = getGraphics ();
        g.setColor (Color.white);
        g.fillRect (25, 25, 1000, 1000);
        
        int x = 50;
        for (int i = 0 ; i < size ; i++)
        {
            g.setColor (new Color (0,100+a[i]/4,0));
            g.fillRect (50, x, a [i], 10);
            x += 12;
            
        }

    }


    public void selectionSort (int a[], int size)
        //Pre: 0 <= size <= number of elements in the array
        //Post: values in a [0..size-1] are in ascending order
    {
        int numUnsorted = size;
        int index; //general index
        int max; //index of largest value
        int temp;

        while (numUnsorted > 0)
        {
            //determine maximum value in array
            max = 0;
            for (index = 0 ; index < numUnsorted ; index++)
            {
                if (a [max] < a [index])
                    max = index;

            }
            temp = a [max];
            a [max] = a [numUnsorted - 1];
            a [numUnsorted - 1] = temp;
            numUnsorted--;
            for (long i = 0 ; i < 51000000 ; i++)
                ;

            printarray (a, size);

        }
    }


    public void bubble (int a[], int size)
    { //Pre: a is an array with values. It is of size size
        //Post: the values in a are put in ascending order

        int temp;

        for (int i = 0 ; i < size - 1 ; i++)
        {
            for (int j = 0 ; j < size - 1 - i ; j++)
            { // compare the two neighbours
                if (a [j + 1] < a [j])
                { //swap the neighbours if necessary
                    temp = a [j];
                    a [j] = a [j + 1];
                    a [j + 1] = temp;
                    for (long k = 0 ; k < 51000000 ; k++)
                        ;

                    printarray (a, size);

                }

            }
        }

    }


    public void insertion (int data[], int n)
    { //Pre: 0<=n<=data.length
        //Post: Values in data[0..n-1] are in ascending order

        int numSorted = 1;
        int index;
        while (numSorted < n)
        {
            //take the first unsorted value
            int temp = data [numSorted];
            //.. and insert it among the sorted:
            for (index = numSorted ; index > 0 ; index--)
            {
                if (temp < data [index - 1])
                {
                    data [index] = data [index - 1];
                }
                else
                {
                    break;
                }
            }
            //re-insert value
            data [index] = temp;
             for (long k = 0 ; k < 51000000 ; k++)
                        ;
            printarray (data, n);
            numSorted++;
        }
    }
    
    


    public void reset ()
    {
        for (int i = 1 ; i <= 1000000 ; i++)
        {
            int index1 = (int) ((Math.random () * (size - 1)));
            int index2 = (int) ((Math.random () * (size - 1)));
            while (index1 == index2)
            {
                index2 = (int) ((Math.random () * (size - 1)));
            }
            int temp = a [index1];
            a [index1] = a [index2];
            a [index2] = temp;
        }
        printarray (a,size);
    }
    
    
    
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.