From ee3f82007a4b353f21b7ded95d796d6a4d632524 Mon Sep 17 00:00:00 2001 From: Florian <38241786+Flonator3000@users.noreply.github.com> Date: Wed, 13 Oct 2021 06:50:42 +0200 Subject: [PATCH] Add Simple Sort (#2545) --- Sorts/SimpleSort.java | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 Sorts/SimpleSort.java diff --git a/Sorts/SimpleSort.java b/Sorts/SimpleSort.java new file mode 100644 index 000000000..b9088ceea --- /dev/null +++ b/Sorts/SimpleSort.java @@ -0,0 +1,46 @@ +package Sorts; + +import static Sorts.SortUtils.*; + +public class SimpleSort implements SortAlgorithm { + + @Override + public > T[] sort(T[] array) { + final int LENGTH = array.length; + + for (int i = 0; i < LENGTH; i++) { + for (int j = i + 1; j < LENGTH; j++) { + if (less(array[j], array[i])) { + T element = array[j]; + array[j] = array[i]; + array[i] = element; + } + } + } + + return array; + } + + public static void main(String[] args) { + // ==== Int ======= + Integer[] a = { 3, 7, 45, 1, 33, 5, 2, 9 }; + System.out.print("unsorted: "); + print(a); + System.out.println(); + + new SimpleSort().sort(a); + System.out.print("sorted: "); + print(a); + System.out.println(); + + // ==== String ======= + String[] b = { "banana", "berry", "orange", "grape", "peach", "cherry", "apple", "pineapple" }; + System.out.print("unsorted: "); + print(b); + System.out.println(); + + new SimpleSort().sort(b); + System.out.print("sorted: "); + print(b); + } +}