From 49e78e23326376f2e26e7e74bf4a522d9b66eb18 Mon Sep 17 00:00:00 2001 From: WickedJack99 Date: Tue, 28 Jun 2022 17:46:57 +0200 Subject: [PATCH] 0001_First push, added[sequSearch, binarySearch, exponentailSearch] --- MyList.java | 7 +++++ MyListElement.java | 14 ++++++++++ Pair.java | 14 ++++++++++ search_algorithms.java | 61 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+) create mode 100644 MyList.java create mode 100644 MyListElement.java create mode 100644 Pair.java create mode 100644 search_algorithms.java diff --git a/MyList.java b/MyList.java new file mode 100644 index 0000000..91831bd --- /dev/null +++ b/MyList.java @@ -0,0 +1,7 @@ +public class MyList { + public MyListElement first; + + public MyList() { + first = new MyListElement(); + } +} diff --git a/MyListElement.java b/MyListElement.java new file mode 100644 index 0000000..f5e2685 --- /dev/null +++ b/MyListElement.java @@ -0,0 +1,14 @@ +public class MyListElement { + public Pair data; + public MyListElement next; + + public MyListElement() { + data = null; + next = null; + } + + public MyListElement(Pair data, MyListElement next) { + this.data = data; + this.next = next; + } +} diff --git a/Pair.java b/Pair.java new file mode 100644 index 0000000..8fcb909 --- /dev/null +++ b/Pair.java @@ -0,0 +1,14 @@ +public class Pair { + public Object key; + public Object data; + + public Pair() { + key = null; + data = null; + } + + public Pair(Object key, Object data) { + this.key = key; + this.data = data; + } +} diff --git a/search_algorithms.java b/search_algorithms.java new file mode 100644 index 0000000..7ecbf20 --- /dev/null +++ b/search_algorithms.java @@ -0,0 +1,61 @@ +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; + for (int i = 0; i < oList.size(); i++) { + if (oList.get(i).key == oKey) { + oData = oList.get(i); + break; + } + } + return oData; + } + + //Data has to be sorted by keys > < =. + public static Pair binarySearch(Object oKey, Pair[] oArray) { + Pair 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; + int m = (int)(iLeft + iRight) / 2; + if (m > (int)oKey) { + oData = helpBinarySearch(oKey, iLeft, (m - 1), oArray); + } else if (m < (int)oKey) { + oData = helpBinarySearch(oKey, (m + 1), iRight, oArray); + } else { + oData = oArray[m]; + } + return oData; + } + + //Data has to be sorted by keys > < =. + public static Pair exponentialSearch(Object oKey, Pair[] oArray) { + boolean bSuccess = false; + Pair oData = null; + int iL = 0; + int iR = 0; + for (int i = 0; i < oArray.length; i++) { + int iIndex = ((int)Math.pow(2, i)); + if ((int)oArray[iIndex].key > (int)oKey) { + iR = iIndex; + break; + } else if ((int)oArray[iIndex].key == (int)oKey) { + oData = oArray[iIndex]; + bSuccess = true; + break; + } else { + iL = iIndex; + } + } + if (!bSuccess) { + oData = helpBinarySearch(oKey, iL, iR, oArray); + } + return oData; + } +} \ No newline at end of file