Implement sort methods

This commit is contained in:
Manuel Thalmann 2022-12-05 22:04:24 +01:00
parent 677b73ee6e
commit bfea9712ec

View file

@ -16,15 +16,50 @@ public class SortServer implements CommandExecutor {
}
public void bubbleSort(int[] a) {
// TODO Implement Aufgabe 1
for (int max = a.length - 1; max >= 0; max--) {
boolean isSorted = true;
for (int i = 0; i < max; i++) {
if (a[i] > a[i + 1]) {
isSorted = false;
swap(a, i, i + 1);
}
}
if (isSorted) {
break;
}
}
}
public void insertionSort(int[] a) {
// TODO Implement Aufgabe 3
for (int k = 0; k < a.length; k++) {
int item = a[k];
int newIndex;
for (newIndex = k; newIndex > 0 && a[newIndex - 1] > item; newIndex--) {
a[newIndex] = a[newIndex - 1];
}
a[newIndex] = item;
}
}
public void selectionSort(int[] a) {
// TODO Implement Aufgabe 3
for (int i = 0; i < a.length; i++) {
int minIndex = i;
for (int j = i; j < a.length; j++) {
if (a[j] < a[i]) {
minIndex = j;
}
}
if (i != minIndex) {
swap(a, i, minIndex);
}
}
}
public void streamSort(int[] a) {
@ -34,25 +69,42 @@ public class SortServer implements CommandExecutor {
}
public boolean isSorted(int[] a) {
// TODO Implement Aufgabe 1
for (int i = a.length - 2; i > 0; i--) {
if (a[i] > a[i + 1]) {
return false;
}
}
return true;
}
public int[] randomData() {
int[] a = new int[dataElems];
// TODO Implement Aufgabe 1
for (int i = 0; i < dataElems; i++) {
a[i] = (int) (Math.random() * DATARANGE);
}
return a;
}
public int[] ascendingData() {
int[] a = new int[dataElems];
// TODO Implement Aufgabe 1
for (int i = 0; i < dataElems; i++) {
a[i] = i;
}
return a;
}
public int[] descendingData() {
int[] a = new int[dataElems];
// TODO Implement Aufgabe 1
for (int i = 0; i < dataElems; i++) {
a[i] = dataElems - (i + 1);
}
return a;
}
@ -70,25 +122,27 @@ public class SortServer implements CommandExecutor {
long startTime = System.currentTimeMillis();
long endTime = startTime;
// TODO Implement Aufgabe 1 und 2 (Tipp: siehe Consumer für Aufruf von Sortiermethode)
// TODO Implement Aufgabe 1 und 2 (Tipp: siehe Consumer für Aufruf von
sorter.accept(a);
endTime = System.currentTimeMillis();
// Sortiermethode)
elapsed = (double)(endTime - startTime);
if (!isSorted(b)) throw new Exception ("ERROR not sorted");
elapsed = (double) (endTime - startTime);
if (!isSorted(b))
throw new Exception("ERROR not sorted");
return elapsed;
}
public String execute(String arg) {
public String execute(String arg) {
Map<String, Consumer<int[]>> sorter = Map.of(
"BUBBLE", this::bubbleSort,
"INSERTION", this::insertionSort,
"SELECTION", this::selectionSort,
"STREAM", this::streamSort
);
Map<String, Supplier<int[]>> generator = Map.of(
"STREAM", this::streamSort);
Map<String, Supplier<int[]>> generator = Map.of(
"RANDOM", this::randomData,
"ASC", this::ascendingData,
"DESC", this::descendingData
);
"DESC", this::descendingData);
String[] args = arg.toUpperCase().split(" ");
dataElems = Integer.parseInt(args[2]);
@ -103,12 +157,18 @@ public class SortServer implements CommandExecutor {
public static void main(String[] args) {
SortServer sorter = new SortServer();
String sort;
sort = "BUBBLE RANDOM 10000"; System.out.println(sorter.execute(sort));
sort = "SELECTION RANDOM 10000"; System.out.println(sorter.execute(sort));
sort = "INSERTION RANDOM 10000"; System.out.println(sorter.execute(sort));
sort = "BUBBLE RANDOM 10000";
System.out.println(sorter.execute(sort));
sort = "SELECTION RANDOM 10000";
System.out.println(sorter.execute(sort));
sort = "INSERTION RANDOM 10000";
System.out.println(sorter.execute(sort));
sort = "BUBBLE ASC 10000"; System.out.println(sorter.execute(sort));
sort = "SELECTION ASC 10000"; System.out.println(sorter.execute(sort));
sort = "INSERTION ASC 10000"; System.out.println(sorter.execute(sort));
sort = "BUBBLE ASC 10000";
System.out.println(sorter.execute(sort));
sort = "SELECTION ASC 10000";
System.out.println(sorter.execute(sort));
sort = "INSERTION ASC 10000";
System.out.println(sorter.execute(sort));
}
}