Wednesday, December 1, 2010

Program 2

/**********************************************
*
* Program 2 - Needs cleaning up
*
*********************************************/

package javaapplication10;

/* Zach Krizan
* Data Structures
* September 20, 2010
*/
public class Main
{

public static void main(String[] args)
{
int A [] = new int[10];
populateA(A); //runs method populatA
System.out.println("Before Sorting:"); //display line
printA(A); // runs method printA

int key;
int i;



for (int j=1; j {
key = A[j];
i = j-1;
while ((i > -1) && (A[i] > key))
{
A[i+1] = A[i];
i = i-1;
}
A[i+1] = key;

}

System.out.println("\n" +"After Sorting"); //displays line
printA(A); //runs method printA again and it will be sorted now
}


public static void printA (int [] B) //creates method printA
{
for (int i=0; i < B.length; i++)
{
System.out.println(B[i]);
}
}

public static int[] populateA (int [] B) //creates method populateA
{
for (int i=0; i < B.length; i++) //for loop will create 10 random #s
{
B[i] = (int)(Math.random()*100); //generates random number
}
return B; //returns int B
}


}

No comments:

Post a Comment