BubbleSort.java



/*BubbleSort.java
This applet will bubble sort an String array list of Last names
< applet
code=BubbleSort
width=400
height=400 >
< /applet >
*/

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

// Main project class
public class BubbleSort extends Applet
{
		TextArea result=new TextArea(25,50);
	
	public void init()
	{
		add(result);
	}

	public void start()
	{
		String stemp; double ftemp;//temporary values
		String name[]={"Jones", "Zeek","Aaron", "Jay"};
		double tran[][]=new double [10][10];
		int howmany = 4; // howmany records (customers)
		byte index=0;
		tran[0][0] = 2.12;   tran[0][1] = 3.14;
		tran[1][0] = 2.73;   tran[1][1] = 6.23;
		tran[2][0] = 15.34;  tran[2][1] = 12.44;
		tran[3][0] = 12.22;  tran[3][1] = 11.56;

//==============================================================		
		//dump Original one and two - dimentional arrays 
		result.appendText("Original List\n");
		result.appendText("-----------------\n");
		for(int i = 0; i <= howmany-1; i++)
		{
			result.appendText("name["+i+"] = "+name[i]+"     ");
			for(int k=0; k <= 1 ; k++)
				
				result.appendText("tran["+i+"]["+k+"] = "+tran[i][k]+"     ");
		
			result.appendText("\n");
		}
		
//======================================================================
		//Implement a simple bubble sort
		for (byte j=0; j < howmany - 1; j++)
		{
			for (byte k=j; k < howmany; k++)
			{
				// Checking to see if names are in proper order
				if (name[j].compareTo(name[k])>0)
				{
					/* If names are out of order, the data
					is swapped here*/

					//swapping names
					stemp=name[j];
					name[j]=name[k];
					name[k]=stemp;
					//swapping transactions
					ftemp=tran[j][0];
					tran[j][0]=tran[k][0];
					tran[k][0]=ftemp;
					ftemp=tran[j][1];
					tran[j][1]=tran[k][1];
					tran[k][1]=ftemp;
				}
			}
		}

//======================================================================
//dump Sorted one and two - dimentional arrays 
		result.appendText("\n\n\nSorted List\n");
		result.appendText("-----------------\n");
		for(int i = 0; i <= howmany-1; i++)
		{
			result.appendText("name["+i+"] = "+name[i]+"     ");
			for(int k=0; k <= 1 ; k++)
				
				result.appendText("tran["+i+"]["+k+"] = "+tran[i][k]+"     ");
		
			result.appendText("\n");
		}
	}
}


Output for BubbleSort.java