Brett Brett - 3 months ago 23
Java Question

Sort array from smallest to largest using java

I'm supposed to create an array and sort the numbers from smallest to largest. Here is what I have so far:

public class bubbleSort {

public static void sort (int [] arrayName){
int temp;
for (int i = 0; i < arrayName.length-1; i++)
{
if(arrayName[i] > arrayName[i+1])
{
temp=arrayName[i];
arrayName[i]=arrayName[i+1];
arrayName[i+1]=temp;
i=-1;
}
}
}

public static void main(String[] args) {
int [] arrayName = new int[10];
for (int i = 0; i < arrayName.length; i++) {
arrayName[i] = (int)(Math.random()*100);
}

System.out.println(sort(arrayName));
}
}


I am getting an error on the last line where I'm trying to print it out. What am I doing wrong?

Answer

Your sort(int[] array) method returns nothing. It is void, therefore you cannot print its return.