commit 49e78e23326376f2e26e7e74bf4a522d9b66eb18 Author: WickedJack99 Date: Tue Jun 28 17:46:57 2022 +0200 0001_First push, added[sequSearch, binarySearch, exponentailSearch] 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