I'm a programming beginner and I have question regarding a return value from a function.
I´m studying Java.
I have attached code from my book that features a classic Selection Sort.
Now obviously the code from the book works. However, these three lines in main function are the basis of my question:
int []a=new int[]{1,9,2,8,3,7,4,6,5};
sort(a);
if(ascending(a)) System.out.println("Works");
So my question is:
In line 2, how can I retrieve a sorted a[] if sort() function is void?
And shouldn´t the line be: a = sort(a)?
public class SelectionSort
{
public static void main(String[]args)
{
int []a=new int[]{1,9,2,8,3,7,4,6,5};
sort(a);
if(ascending(a)) System.out.println("Virðist virka");
else System.out.println("Virkarekki");
}
public static void sort(int[]a)
{
if(a.length<2) return;
int i=0;
while(i!=a.length)
{
int k=i+1;
while(k!=a.length)
{
if(a[k]<a[i])
{
int tmp=a[i];
a[i]=a[k];
a[k]=tmp;
}
k++;
}
i++;
}
}
public static boolean ascending(int[]a)
{
if(a.length<2) return true;
int i=1;
while(i!=a.length)
{
if(a[i-1]>a[i]) return false;
i++;
}
return true;
}
}