the first part of my code is working fine, but the bubble sort part is not running at all, at least I don't believe it is, as I cannot get my code to print the sorted list. I have tried making everything doubles, and adding in to return the list, but I still cannot make it work
This is my code thus far:
public static void main(String[] args) {
// TODO Auto-generated method stub
//create array
double[] list = new double[10];
//Generates 10 Random Numbers in the range 1 -20
for(int i = 0; i < list.length; i ) {
list[i] = (int)(Math.random()* 100 1);
}//end for loop
System.out.println("The unsorted list is: " Arrays.toString(list));
//find max number
double max = -1;
for (int i = 0; i < list.length; i ) {
if (list[i] > max) max = list[i];
}
System.out.println("The largest value is " max);
}
public static double[] bubbleSort(double[] list)
{
double temp;
for (int i = list.length - 1; i > 0; i--)
{
for (int j = 0; j < i; j )
{
if (list[j] > list[j 1])
{
temp = list[j];
list[j] = list[j 1];
list[j 1] = temp;
System.out.println("The sorted list is: " bubbleSort(list) " ");
}
}
}
return list;
}
}
CodePudding user response:
In Java (and most languages), only the "Main" method runs automatically; that is the entry point for your code. Other methods won't run unless you call them.
You should look up "Control flow" to generally understand more of what gets run and when, but for now, you just need to call that method. Calling a method is what you're doing with code like Arrays.toString(list)
, but in this case the method is in the local namespace, so you just need bubbleSort(list)