0004_Fourth push edited[sequentialSearchList for custom List-type, error at helpBinarySearch], added JUnit-testing

This commit is contained in:
Meruemon
2022-07-02 19:45:07 +02:00
parent 42fb244b29
commit 6ecc372964
7 changed files with 82 additions and 17 deletions

View File

@@ -1,11 +1,6 @@
public class Main {
public static void main(String[] args) {
//Test MyList
MyList list = new MyList();
list.add(new MyPair(2,2));
list.remove(1);
//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)};
@@ -24,7 +19,7 @@ public class Main {
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());
//sort_algorithms.myQuickSortInSitu(arrPairs2);
//System.out.println(arrPairs2[0].toString() + arrPairs2[1].toString() + arrPairs2[2].toString() + arrPairs2[3].toString());
}
}

View File

@@ -1,5 +1,6 @@
public class MyList {
public MyListElement first;
private int size = 0;
public MyList() {
first = null;
@@ -19,6 +20,7 @@ public class MyList {
}
node.next = elem;
}
size++;
}
//Inserts elem at index.
@@ -35,6 +37,7 @@ public class MyList {
i++;
}
}
size++;
}
//Removes element with key from list.
@@ -54,6 +57,7 @@ public class MyList {
}
}
}
size--;
}
//Return MyList as String.
@@ -67,4 +71,8 @@ public class MyList {
}
return listAsString;
}
public int getSize() {
return this.size;
}
}

23
MyTest.java Normal file
View File

@@ -0,0 +1,23 @@
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class MyTest {
@Test
public void testSequentailSearch() {
MyList list = new MyList();
list.add(new MyPair(1,3));
list.add(new MyPair(2,4));
assertEquals(3, search_algorithms.sequentielSearchList(1, list).data);
assertEquals(4, search_algorithms.sequentielSearchList(2, list).data);
}
@Test
public void testBinarySearch() {
MyPair[] pairs = {new MyPair(1,3), new MyPair(2,4)};
assertEquals(3, search_algorithms.binarySearch(1, pairs).data);
assertEquals(4, search_algorithms.binarySearch(2, pairs).data);
}
}

BIN
lib/hamcrest-core-1.3.jar Normal file

Binary file not shown.

BIN
lib/junit-4.13.2.jar Normal file

Binary file not shown.

View File

@@ -3,13 +3,14 @@ import java.util.List;
public class search_algorithms {
//Data doesnt have to be sorted.
public static MyPair sequentielSearch(Object oKey, List<MyPair> oList) {
public static MyPair sequentielSearchList(Object oKey, MyList oList) {
MyPair oData = null;
for (int i = 0; i < oList.size(); i++) {
if (oList.get(i).key == oKey) {
oData = oList.get(i);
break;
}
MyListElement elem = oList.first;
while ((elem.next != null) && (elem.data.key != oKey)) {
elem = elem.next;
}
if (elem.data.key == oKey) {
oData = elem.data;
}
return oData;
}
@@ -24,9 +25,9 @@ public class search_algorithms {
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) {
if (oArray[m].getKeyAsInt() > (int)oKey) {
oData = helpBinarySearch(oKey, iLeft, (m - 1), oArray);
} else if (m < (int)oKey) {
} else if (oArray[m].getKeyAsInt() < (int)oKey) {
oData = helpBinarySearch(oKey, (m + 1), iRight, oArray);
} else {
oData = oArray[m];

View File

@@ -131,5 +131,43 @@ public class sort_algorithms {
return new MyPair[2];
}
/**
* TODO
* @param arr
*/
public static void myQuickSortInSitu(MyPair[] arr) {
myQuickSortInSituHelp(arr, 0, (arr.length - 1));
}
/**
* TODO
* @param arr
* @param left
* @param right
*/
private static void myQuickSortInSituHelp(MyPair[] arr, int left, int right) {
int smallerPivotIndexRight = -1;
int unpartitionedAreaLeft = left;
int pivot = right;
for (int k = 0; k < arr.length; k++) {
if (arr[unpartitionedAreaLeft].getKeyAsInt() <= arr[pivot].getKeyAsInt()) {
MyPair store = arr[unpartitionedAreaLeft];
arr[unpartitionedAreaLeft] = arr[smallerPivotIndexRight + 1];
arr[smallerPivotIndexRight + 1] = store;
smallerPivotIndexRight++;
unpartitionedAreaLeft++;
} else {
unpartitionedAreaLeft++;
}
}
MyPair store = arr[pivot];
arr[pivot] = arr[smallerPivotIndexRight + 1];
arr[smallerPivotIndexRight + 1] = store;
myQuickSortInSituHelp(arr, 0, smallerPivotIndexRight);
myQuickSortInSituHelp(arr, smallerPivotIndexRight + 2, right);
}
private static void myHeapsortInSitu(MyPair[] arr) {
}
}