Hey I'm trying to test my selection sort algorithm but all the output I get in the console is just "[I@15db9742" Could someone please explain to me why I'm getting junk output? It is really baffling me, could it possible be a problem with the IDE or is it something in the code?
Thanks
import java.util.Arrays;
public class SelectionSorterTest {
// Factories
// Queries
/**
* Sorts a copy of the input array.
* @param input an array.
* @return a sorted copy of input.
*/
public static int[] sort (int[] input) {
int[] sorted = Arrays.copyOf(input, input.length);
for(int i = 0; i < sorted.length - 1; i++)
{
int minIndex = i;
for(int j = i + 1; j < sorted.length - 1; j++)
{
if(sorted[j] < sorted[minIndex])
{
minIndex = j;
}
}
if(i != minIndex)
{
swap(sorted, minIndex, i);
}
}
return sorted;
}
// Commands
// Private
// disabled constructor
private SelectionSorterTest () { }
private static void swap (int[] arr, int x, int y) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
public static void main(String[] args)
{
int[] Array = {9,7,6,4,3,2,21,1};
System.out.println(sort(Array));
}
}
By using this line System.out.println(sort(Array));
you are printing out an array's address in memory. Use a loop to print the element of that array!
By the way, your algorithm is incorrect because you are missing the last element in the for loop. Remove the -1
part out of the loop to correct it. Thanks