Skip to content

Instantly share code, notes, and snippets.

Created February 23, 2011 04:08
Show Gist options
  • Save anonymous/839999 to your computer and use it in GitHub Desktop.
Save anonymous/839999 to your computer and use it in GitHub Desktop.
public static void main ()
{
int randomArray [] = new int [100];
Random num = new Random ();
for (int i = 0; i < 100; i++)
{
randomArray[i] = num.nextInt(100);
if (randomArray[i] < 10)
{
System.out.print(randomArray[i] + " ");
}
else
{
System.out.print(randomArray[i] + " ");
}
if (i!=0 && i%10 == 0)
{
System.out.print("\n");
}
}
selectionSort(randomArray);
System.out.println("\n\nAfter the selection sort: ");
for (int i = 0; i < 100; i++)
{
if (randomArray[i] < 10)
{
System.out.print(randomArray[i] + " ");
}
else
{
System.out.print(randomArray[i] + " ");
}
if (i!=0 && i%10 == 0)
{
System.out.print("\n");
}
}
}
public static int [] selectionSort (int [] randomArray)
{
int min;
int temp;
for (int i = 0; i < randomArray.length-1; i++)
{
min = i;
for (int scan = i + 1; scan<randomArray.length; scan++)
{
if (randomArray[scan] < randomArray[i])
{
min = scan;
}
}
temp = randomArray[min];
randomArray[min] = randomArray[i];
randomArray[i] = temp;
}
return randomArray;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment