0001_First push, added[sequSearch, binarySearch, exponentailSearch]
This commit is contained in:
7
MyList.java
Normal file
7
MyList.java
Normal file
@@ -0,0 +1,7 @@
|
||||
public class MyList {
|
||||
public MyListElement first;
|
||||
|
||||
public MyList() {
|
||||
first = new MyListElement();
|
||||
}
|
||||
}
|
||||
14
MyListElement.java
Normal file
14
MyListElement.java
Normal 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
14
Pair.java
Normal 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
61
search_algorithms.java
Normal 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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user