Bubble Sort Int Array Problem
the error says int cannot be deferenced at java line 11
here is the code.
how do i fix that?
Code :
public class SortInt
{
public static void sort(int cases[]) //Sorting fuction, the array of nums is passed to it
{
int temp; //a temp var is need to swtich the values in the array
for(int i = 0; i < cases.length - 1; i++) //Start of the bubble sort, you need to take one away from
{ //the legnth of the array b/c you dont need to go throw the
for(int j = 0; j < cases.length - 1; j++) //array that many times. Also if it whould go passt the end of
{ //the array in the second loop wich whould not been good.
if(cases[j].compareTo(cases[j+1]) > 0) //This part checks to see if the number before it is bigger
//You must use the compareTo() method of a string
{ //Note: chage the < opprator to > to sort from samllest to lagergets
temp = cases[j]; //Sotres the value of nums[ii] in the temp var for use latter
cases[j] = cases[j+1]; //puts the value of the bigger number where the lesser one was
cases[j+1] = temp; //puts the value of the lesser var back in the array where the
} //biger one was.
}
}
}
public static void main(String[] args)
{
//declares an array to be sorted
int[] cases = {200000 , 300000 , 100000 ,400000 , 500000 , 750000 , 1000000,
1 , 2 , 5 ,400 ,500,750, 1000 ,
5000 , 10000 , 25000 , 10 , 25 , 50 , 75,
100 , 50000 , 75000 };
System.out.println("Before sort():\n");
for(int i = 0; i < cases.length; i++) //for loop to show all the values of the array
{
System.out.println(cases[i]); //uses the for loop index var to slect an array entreay
}
sort(cases); //sends the array to the sort() fuction
System.out.println("\nAfter sort():\n");
for(int i = 0; i < cases.length; i++) //for loop to show the realostes of the sorting
{
System.out.println(cases[i]);
}
}
}
Re: Bubble Sort Int Array Problem
Please post the full contents of the error message you get when you compile this program.
Where is the line with the error? Which is Line 11???