39 lines
1.2 KiB
Java
39 lines
1.2 KiB
Java
import java.util.Arrays;
|
|
|
|
public class Sort {
|
|
|
|
private static int[] selectionSort1(int[] array) {
|
|
final int[] hilfsArray = new int[array.length];
|
|
|
|
while (array.length != 0) {
|
|
int indexKleinstesElement = 0;
|
|
for (int index = 1; index < array.length; index++) {
|
|
if (array[index] < array[indexKleinstesElement]) indexKleinstesElement = index;
|
|
}
|
|
|
|
// Add to hilfsarray (at curr pos)
|
|
hilfsArray[hilfsArray.length - array.length] = array[indexKleinstesElement];
|
|
System.out.println("Sortiere.... hilfsarray: " + Arrays.toString(hilfsArray) + " array: " + Arrays.toString(array));
|
|
|
|
// Refill array
|
|
int[] newArray = new int[array.length - 1];
|
|
|
|
// i = index in array, j = index in newArray
|
|
for (int i = 0, j = 0; i < array.length; i++ ) {
|
|
if (i != indexKleinstesElement) {
|
|
newArray[j] = array[i];
|
|
j++;
|
|
}
|
|
}
|
|
array = newArray;
|
|
}
|
|
return hilfsArray;
|
|
}
|
|
|
|
public static void main(String[] args) {
|
|
int[] input = Arrays.stream(args).mapToInt(Integer::parseInt).toArray();
|
|
System.out.println("Input: " + Arrays.toString(input));
|
|
System.out.println("Output: " + Arrays.toString(selectionSort1(input)));
|
|
}
|
|
}
|