import java.io.*;
import java.util.Random;
import exe.*;

public class Sort {

    static final String TITLE = null;	// no title
    static int arraySize;	// # of items to sort
    static GAIGSarray items;	// the array of items

    public static void main(String args[]) throws IOException {
   
	// process program parameters and create the show file object
	ShowFile show = new ShowFile(args[0]);
	arraySize = Integer.parseInt(args[1]);

	// define the two structures in the show snapshots
	items = new GAIGSarray(arraySize, true, "BubbleSort", 
			       "#999999", 0.1, 0.1, 0.9, 0.9, 0.07);

	// initialize the array to be sorted & show it
	loadArray();       
	show.writeSnap(TITLE, items);
        
	for (int pass = 1; pass < arraySize; pass++)
	    for (int i = 0; i < arraySize-pass; i++)
		if ((Integer)(items.get(i)) > (Integer)(items.get(i+1)))
		    swap(i, i+1);
        
	// visualization is done
	show.close();                    
    }

    // Load the array with values from 1 to the array size, then
    // shuffle these values so that they appear in random order.
    private static void loadArray () {
	Random rand = new Random();
	for (int i = 0; i < arraySize; i++)
	    items.set(i+1,i);
	for (int i = 0; i < arraySize-1; i++)
	    swap(i, i + (Math.abs(rand.nextInt()) 
			 % (arraySize - i)) );
    }
    

    // Swap two items in the array.
    private static void swap (int loc1, int loc2) {
	Object temp = items.get(loc1);
	items.set(items.get(loc2), loc1);
	items.set(temp, loc2);    
    }

}
