A
awbachorski
I am tring to get this preogram to sort an array.
/*
*This program sorts integers from lowest to highest
*/
public class syntaxfive{
public static void main(String[] args){
int[] a = new int[20];
//Initialize the array to random numbers between 0 and 99
for (int i = 0; i < a.length; i++)
a = (int)(Math.random()*10);
print(a);
sort(a);
print(a);
}
public static void sort(int[] a){
int itemToInsert;
boolean stillLooking;
// On the kth pass, insert item k into its correct position
among
// the first k entries in array.
// On the kth pass, insert item k into its correct position
among
// the first k entries in array.
for (int k = 1; k < a.length; k--){
// Walk backwards through list, looking for slot to insert a[k]
int j = k - 1;
itemToInsert = a[k];
stillLooking = true;
while ((k >= 0) || stillLooking )
if (itemToInsert > a[j])
{
a[k + 1] = a[j];
k++;
}
else
stillLooking = false;
// Upon leaving loop, j + 1 is the index
// where itemToInsert belongs
a[k - 1] = itemToInsert;
}
}
static public void print(int[] a){
for (int i = 0; i < a.length; i++)
System.out.print(a);
System.out.println("");
}
}
// Adam W. Bachorski
// 1. Added { to void sort.
// 2. Changed else to if.
// 3. Added (int) before math.random to make it an integer.
// 4. Added print(a) to print().
// 6. Changed double itemTo Insert to int.
// 7. Changed a = (int)(Math.random()*10); to produce an int from
1-9.
// 8. Added brakcets to the if statement.
/*
*This program sorts integers from lowest to highest
*/
public class syntaxfive{
public static void main(String[] args){
int[] a = new int[20];
//Initialize the array to random numbers between 0 and 99
for (int i = 0; i < a.length; i++)
a = (int)(Math.random()*10);
print(a);
sort(a);
print(a);
}
public static void sort(int[] a){
int itemToInsert;
boolean stillLooking;
// On the kth pass, insert item k into its correct position
among
// the first k entries in array.
// On the kth pass, insert item k into its correct position
among
// the first k entries in array.
for (int k = 1; k < a.length; k--){
// Walk backwards through list, looking for slot to insert a[k]
int j = k - 1;
itemToInsert = a[k];
stillLooking = true;
while ((k >= 0) || stillLooking )
if (itemToInsert > a[j])
{
a[k + 1] = a[j];
k++;
}
else
stillLooking = false;
// Upon leaving loop, j + 1 is the index
// where itemToInsert belongs
a[k - 1] = itemToInsert;
}
}
static public void print(int[] a){
for (int i = 0; i < a.length; i++)
System.out.print(a);
System.out.println("");
}
}
// Adam W. Bachorski
// 1. Added { to void sort.
// 2. Changed else to if.
// 3. Added (int) before math.random to make it an integer.
// 4. Added print(a) to print().
// 6. Changed double itemTo Insert to int.
// 7. Changed a = (int)(Math.random()*10); to produce an int from
1-9.
// 8. Added brakcets to the if statement.