From 42fb244b29c1382caf14d9146adec4bff33ba037 Mon Sep 17 00:00:00 2001 From: Meruemon Date: Sat, 2 Jul 2022 15:32:22 +0200 Subject: [PATCH] 0003_Third push added[several sortingalgorithms] --- Main.java | 26 +++++++- MyList.java | 4 +- MyListElement.java | 4 +- Pair.java => MyPair.java | 10 ++- search_algorithms.java | 16 ++--- sort_algorithms.java | 132 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 174 insertions(+), 18 deletions(-) rename Pair.java => MyPair.java (64%) diff --git a/Main.java b/Main.java index 32a8b13..cfd6ce2 100644 --- a/Main.java +++ b/Main.java @@ -1,10 +1,30 @@ public class Main { public static void main(String[] args) { + //Test MyList MyList list = new MyList(); - list.add(new Pair(2,2)); + list.add(new MyPair(2,2)); list.remove(1); - list.insert(new Pair(3,3), 0); - + //list.insert(new MyPair(3,3), 0); System.out.println(list.toString()); + + //Test BubbleSortInSitu + MyPair[] arrPairs = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)}; + sort_algorithms.myBubbleSortInSitu(arrPairs); + System.out.println(arrPairs[0].toString() + arrPairs[1].toString() + arrPairs[2].toString() + arrPairs[3].toString()); + + //Test BubbleSortNotInSitu + MyPair[] arrPairs2 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)}; + MyPair[] arrPairs3 = sort_algorithms.myBubbleSortNotInSitu(arrPairs2); + System.out.println(arrPairs3[0].toString() + arrPairs3[1].toString() + arrPairs3[2].toString() + arrPairs3[3].toString()); + System.out.println(arrPairs2[0].toString() + arrPairs2[1].toString() + arrPairs2[2].toString() + arrPairs2[3].toString()); + + //Test SelectionSortInSitu + MyPair[] arrPairs4 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)}; + sort_algorithms.mySelectionSortInSitu(arrPairs4); + System.out.println(arrPairs4[0].toString() + arrPairs4[1].toString() + arrPairs4[2].toString() + arrPairs4[3].toString()); + + // + sort_algorithms.myInsertionSortInSitu(arrPairs2); + System.out.println(arrPairs2[0].toString() + arrPairs2[1].toString() + arrPairs2[2].toString() + arrPairs2[3].toString()); } } diff --git a/MyList.java b/MyList.java index bc80046..9af9163 100644 --- a/MyList.java +++ b/MyList.java @@ -6,7 +6,7 @@ public class MyList { } //Adds elem to list. - public void add(Pair pair) { + public void add(MyPair pair) { MyListElement elem = new MyListElement(pair, null); //Case 1: List is empty if (this.first == null) { @@ -22,7 +22,7 @@ public class MyList { } //Inserts elem at index. - public void insert(Pair pair, int index) { + public void insert(MyPair pair, int index) { MyListElement node = this.first; MyListElement pre = this.first; if (index == 0) { diff --git a/MyListElement.java b/MyListElement.java index 215c1bd..7c4c0ee 100644 --- a/MyListElement.java +++ b/MyListElement.java @@ -1,5 +1,5 @@ public class MyListElement { - public Pair data; + public MyPair data; public MyListElement next; public MyListElement() { @@ -7,7 +7,7 @@ public class MyListElement { next = null; } - public MyListElement(Pair data, MyListElement next) { + public MyListElement(MyPair data, MyListElement next) { this.data = data; this.next = next; } diff --git a/Pair.java b/MyPair.java similarity index 64% rename from Pair.java rename to MyPair.java index bf5ed3e..7a872a0 100644 --- a/Pair.java +++ b/MyPair.java @@ -1,13 +1,13 @@ -public class Pair { +public class MyPair { public Object key; public Object data; - public Pair() { + public MyPair() { key = null; data = null; } - public Pair(Object key, Object data) { + public MyPair(Object key, Object data) { this.key = key; this.data = data; } @@ -16,4 +16,8 @@ public class Pair { public String toString() { return ("(" + this.key.toString() + "," + this.data.toString() + ")"); } + + public int getKeyAsInt() { + return (int)this.key; + } } diff --git a/search_algorithms.java b/search_algorithms.java index 7ecbf20..c426928 100644 --- a/search_algorithms.java +++ b/search_algorithms.java @@ -3,8 +3,8 @@ import java.util.List; public class search_algorithms { //Data doesnt have to be sorted. - public static Pair sequentielSearch(Object oKey, List oList) { - Pair oData = null; + public static MyPair sequentielSearch(Object oKey, List oList) { + MyPair oData = null; for (int i = 0; i < oList.size(); i++) { if (oList.get(i).key == oKey) { oData = oList.get(i); @@ -15,14 +15,14 @@ public class search_algorithms { } //Data has to be sorted by keys > < =. - public static Pair binarySearch(Object oKey, Pair[] oArray) { - Pair oData = null; + public static MyPair binarySearch(Object oKey, MyPair[] oArray) { + MyPair oData = null; oData = helpBinarySearch(oKey, 0, (oArray.length - 1), oArray); return oData; } - private static Pair helpBinarySearch(Object oKey, int iLeft, int iRight, Pair[] oArray) { - Pair oData = null; + private static MyPair helpBinarySearch(Object oKey, int iLeft, int iRight, MyPair[] oArray) { + MyPair oData = null; int m = (int)(iLeft + iRight) / 2; if (m > (int)oKey) { oData = helpBinarySearch(oKey, iLeft, (m - 1), oArray); @@ -35,9 +35,9 @@ public class search_algorithms { } //Data has to be sorted by keys > < =. - public static Pair exponentialSearch(Object oKey, Pair[] oArray) { + public static MyPair exponentialSearch(Object oKey, MyPair[] oArray) { boolean bSuccess = false; - Pair oData = null; + MyPair oData = null; int iL = 0; int iR = 0; for (int i = 0; i < oArray.length; i++) { diff --git a/sort_algorithms.java b/sort_algorithms.java index cb6e3be..94e39dc 100644 --- a/sort_algorithms.java +++ b/sort_algorithms.java @@ -1,3 +1,135 @@ public class sort_algorithms { + + /** + * BubbleSort in-situ + * Bubbles with every loop the largest element to the end, area where elements are looked at decreases by 1 each time + * so largest will not be looked at anymore. + * + * Example: + * 0, 2, 4, 7, 3, 1 + * 0, 2, 4, 3, 1, 7 + * 0, 2, 3, 1, 4, 7 + * 0, 2, 1, 3, 4, 7 + * 0, 1, 2, 3, 4, 7 + * + * @param arr + */ + public static void myBubbleSortInSitu(MyPair[] arr) { + for (int i = 0; i < arr.length; i++) { + for (int j = 0; (j + 1) < arr.length - i; j++) { + if (arr[j].getKeyAsInt() > arr[j + 1].getKeyAsInt()) { + MyPair store = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = store; + } + } + } + } + + /** + * BubbleSort not in-situ + * @param arr + */ + public static MyPair[] myBubbleSortNotInSitu(MyPair[] arr) { + //Has to create new array and copy data + MyPair[] sortedSequence = new MyPair[arr.length]; + for (int i = 0; i < arr.length; i++) { + sortedSequence[i] = arr[i]; + } + //Sorting starts here + for (int i = 0; i < sortedSequence.length; i++) { + for (int j = 0; (j + 1) < sortedSequence.length - i; j++) { + if (sortedSequence[j].getKeyAsInt() > sortedSequence[j + 1].getKeyAsInt()) { + MyPair store = sortedSequence[j]; + sortedSequence[j] = sortedSequence[j + 1]; + sortedSequence[j + 1] = store; + } + } + } + return sortedSequence; + } + + /** + * + * @param arr + */ + public static void mySelectionSortInSitu(MyPair[] arr) { + for (int i = 0; i < arr.length; i++) { + int smallestIndex = arr.length - 1; + int smallestKey = arr[arr.length - 1].getKeyAsInt(); + for (int j = (arr.length - 1); j >= i; j--) { + if (arr[j].getKeyAsInt() < smallestKey) { + smallestIndex = j; + smallestKey = arr[j].getKeyAsInt(); + } + } + MyPair store = arr[i]; + arr[i] = arr[smallestIndex]; + arr[smallestIndex] = store; + } + } + + /** + * + * @param arr + * @return + */ + public static MyPair[] mySelectionSortNotInSitu(MyPair[] arr) { + //Has to create new array and copy data + MyPair[] sortedSequence = new MyPair[arr.length]; + for (int i = 0; i < arr.length; i++) { + sortedSequence[i] = arr[i]; + } + //Sorting starts here + for (int i = 0; i < sortedSequence.length; i++) { + int smallestIndex = sortedSequence.length - 1; + int smallestKey = sortedSequence[sortedSequence.length - 1].getKeyAsInt(); + for (int j = (sortedSequence.length - 1); j >= i; j--) { + if (sortedSequence[j].getKeyAsInt() < smallestKey) { + smallestIndex = j; + smallestKey = sortedSequence[j].getKeyAsInt(); + } + } + MyPair store = sortedSequence[i]; + sortedSequence[i] = sortedSequence[smallestIndex]; + sortedSequence[smallestIndex] = store; + } + return sortedSequence; + } + + /** + * 7, 4, 2, 0, 1, 3 + * 7 4, 2, 0, 1, 3; i = 0; j = 0 + * 4, 7 2, 0, 1, 3; i = 1; j = 0, j = 1; + * 2, 4, 7 0, 1, 3 + * 0, 2, 4, 7 1, 3 + * 0, 1, 2, 4, 7 3 + * 0, 1, 2, 3, 4, 7 + * TODO + * @param arr + */ + public static void myInsertionSortInSitu(MyPair[] arr) { + for (int i = 0; i < arr.length; i++) { + int index = 0; + for (int j = 0; j <= i; j++) { + if (arr[j].getKeyAsInt() >= arr[i].getKeyAsInt()) { + index = j; + } + } + MyPair store = arr[index]; + arr[index] = arr[i]; + arr[i] = store; + } + } + + /** + * TODO + * @param arr + * @return + */ + public static MyPair[] myInsertionSortNotInSitu(MyPair[] arr) { + return new MyPair[2]; + } + }