I want to use sorting for array of number, here the code:
package SortAlgorithm;
public abstract class Sort {
abstract int[] sort(int[] array);
void swap(int a,int b){
int temp;
temp = a;
a = b;
b = temp;
}
static void print(int[] array){
for(int i = 0; i < array.length; i++){
System.out.print(array[i]+" ");
}
}
}
public class SelectionSort extends Sort{
private int indexMin = 0;
public int[] sort(int[] arrNum){
int[] numbers = arrNum;
for(int i = 0; i < numbers.length; i++){
indexMin = i;
for(int j = i + 1; j < numbers.length; j++){
if(numbers[j] < numbers[indexMin]){
indexMin = j;
}
}
int temp = numbers[i];
numbers[i] = numbers[indexMin];
numbers[indexMin] = temp;
}
return numbers;
}
}
public class MSorting {
static int[] hasilSort;
public static void main (String[] args){
int[] arrNum = {4,7,5,9,1};
Sort.print(arrNum);
System.out.println();
SelectionSort sortSatu = new SelectionSort();
hasilSort = sortSatu.sort(arrNum);
Sort.print(hasilSort);
System.out.println();
Sort.print(arrNum);
}
}
When I run the code, the output is weird. This is the output I get:
4 7 5 9 1
1 4 5 7 9
1 4 5 7 9
But I want the output like this:
4 7 5 9 1
1 4 5 7 9
4 7 5 9 1
What's wrong??