0001_First push, added[sequSearch, binarySearch, exponentailSearch]

This commit is contained in:
WickedJack99
2022-06-28 17:46:57 +02:00
commit 49e78e2332
4 changed files with 96 additions and 0 deletions

7
MyList.java Normal file
View File

@@ -0,0 +1,7 @@
public class MyList {
public MyListElement first;
public MyList() {
first = new MyListElement();
}
}

14
MyListElement.java Normal file
View File

@@ -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;
}
}

14
Pair.java Normal file
View File

@@ -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;
}
}

61
search_algorithms.java Normal file
View File

@@ -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<Pair> 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;
}
}