diff --git a/Main.java b/Main.java index cfd6ce2..9366930 100644 --- a/Main.java +++ b/Main.java @@ -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()); } } diff --git a/MyList.java b/MyList.java index 9af9163..e9fe87c 100644 --- a/MyList.java +++ b/MyList.java @@ -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; + } } diff --git a/MyTest.java b/MyTest.java new file mode 100644 index 0000000..b9a5645 --- /dev/null +++ b/MyTest.java @@ -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); + } +} diff --git a/lib/hamcrest-core-1.3.jar b/lib/hamcrest-core-1.3.jar new file mode 100644 index 0000000..9d5fe16 Binary files /dev/null and b/lib/hamcrest-core-1.3.jar differ diff --git a/lib/junit-4.13.2.jar b/lib/junit-4.13.2.jar new file mode 100644 index 0000000..6da55d8 Binary files /dev/null and b/lib/junit-4.13.2.jar differ diff --git a/search_algorithms.java b/search_algorithms.java index c426928..22e1a12 100644 --- a/search_algorithms.java +++ b/search_algorithms.java @@ -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 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]; diff --git a/sort_algorithms.java b/sort_algorithms.java index 94e39dc..5cca608 100644 --- a/sort_algorithms.java +++ b/sort_algorithms.java @@ -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) { + + } }