From bfea9712ec3f15a2d14b047ff2d00dbddc4cbc5b Mon Sep 17 00:00:00 2001 From: Manuel Thalmann Date: Mon, 5 Dec 2022 22:04:24 +0100 Subject: [PATCH] Implement sort methods --- .../java/ch/nuth/zhaw/exbox/SortServer.java | 102 ++++++++++++++---- 1 file changed, 81 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/ch/nuth/zhaw/exbox/SortServer.java b/app/src/main/java/ch/nuth/zhaw/exbox/SortServer.java index 7adab9b..48c5d90 100644 --- a/app/src/main/java/ch/nuth/zhaw/exbox/SortServer.java +++ b/app/src/main/java/ch/nuth/zhaw/exbox/SortServer.java @@ -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> sorter = Map.of( "BUBBLE", this::bubbleSort, "INSERTION", this::insertionSort, "SELECTION", this::selectionSort, - "STREAM", this::streamSort - ); - Map> generator = Map.of( + "STREAM", this::streamSort); + Map> 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)); } }