diff --git a/.idea/codeStyles/Project.xml b/.idea/codeStyles/Project.xml index 6d799a0..565c347 100644 --- a/.idea/codeStyles/Project.xml +++ b/.idea/codeStyles/Project.xml @@ -24,29 +24,5 @@ + diff --git a/.idea/modules.xml b/.idea/modules.xml index 772bec3..94a3e64 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -5,6 +5,10 @@ + + + + diff --git a/.idea/modules/algorithms_main.iml b/.idea/modules/algorithms_main.iml index 3c50b0f..5b059b5 100644 --- a/.idea/modules/algorithms_main.iml +++ b/.idea/modules/algorithms_main.iml @@ -1,12 +1,9 @@ - + - - - - + diff --git a/.idea/modules/algorithms_test.iml b/.idea/modules/algorithms_test.iml index 8e03485..c6b9f37 100644 --- a/.idea/modules/algorithms_test.iml +++ b/.idea/modules/algorithms_test.iml @@ -1,12 +1,9 @@ - + - - - - + diff --git a/.idea/modules/sorting/sorting_main.iml b/.idea/modules/sorting/sorting_main.iml index 0a7b4a2..97501cb 100644 --- a/.idea/modules/sorting/sorting_main.iml +++ b/.idea/modules/sorting/sorting_main.iml @@ -1,11 +1,10 @@ - + - diff --git a/.idea/modules/sorting/sorting_test.iml b/.idea/modules/sorting/sorting_test.iml index c8dbe30..f86f3b8 100644 --- a/.idea/modules/sorting/sorting_test.iml +++ b/.idea/modules/sorting/sorting_test.iml @@ -1,11 +1,10 @@ - + - diff --git a/README.md b/README.md index 8c8bb63..3b65b56 100644 --- a/README.md +++ b/README.md @@ -4,12 +4,18 @@ This repository is meant for me to compile a list of examples of different algorithms that I have learned of, and develop a working implementation. These implementations may not be the most efficient, so please do point it out to me if you see something that could be improved upon. The complexity -notations were taken from the [Big O Cheatsheet](http://bigocheatsheet.com/) +notations were taken from the [Big O Cheat Sheet](http://bigocheatsheet.com/) + +### Searching + +Algorithm|Implementation|Tests|Worst Time Complexity|Worst Space Complexity +-----|-----|-----|:-----:|:-----: +[Linear Search](https://en.wikipedia.org/wiki/Linear_search)|[LinearSearch.java](searching/src/main/java/com/wbrawner/algorithms/searching/LinearSearch.java)|[ParameterizedLinearSearchTest.java](searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedLinearSearchTest.java)|O(n)|O(1) ### Sorting Algorithm|Implementation|Tests|Worst Time Complexity|Worst Space Complexity ------|-----|-----|-----|----- +-----|-----|-----|:-----:|:-----: [Bubble Sort](https://en.wikipedia.org/wiki/Bubble_sort)|[BubbleSort.java](sorting/src/main/java/com/wbrawner/algorithms/sorting/BubbleSort.java)|[ParameterizedBubbleSortTest.java](sorting/src/test/java/com/wbrawner/algorithms/sorting/ParameterizedBubbleSortTest.java)|O(n²)|O(1) [Insertion Sort](https://en.wikipedia.org/wiki/Insertion_sort)|[InsertionSort.java](sorting/src/main/java/com/wbrawner/algorithms/sorting/InsertionSort.java)|[ParameterizedInsertionSortTest.java](sorting/src/test/java/com/wbrawner/algorithms/sorting/ParameterizedInsertionSortTest.java)|O(n²)|O(1) [Merge Sort](https://en.wikipedia.org/wiki/Merge_sort)|[MergeSort.java](sorting/src/main/java/com/wbrawner/algorithms/sorting/MergeSort.java)|[ParameterizedMergeSortTest.java](sorting/src/test/java/com/wbrawner/algorithms/sorting/ParameterizedMergeSortTest.java)|O(n log(n))|O(n) @@ -21,4 +27,5 @@ Algorithm|Implementation|Tests|Worst Time Complexity|Worst Space Complexity To keep things simple, the tests share the same data where possible. +- [Searching algorithms test data](searching/src/test/java/com/wbrawner/algorithms/searching/SearchData.java) - [Sorting algorithms test data](sorting/src/test/java/com/wbrawner/algorithms/sorting/SortData.java) \ No newline at end of file diff --git a/searching/build.gradle b/searching/build.gradle new file mode 100644 index 0000000..2238d34 --- /dev/null +++ b/searching/build.gradle @@ -0,0 +1,14 @@ +group 'com.wbrawner' +version '1.0-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/searching/searching.iml b/searching/searching.iml new file mode 100644 index 0000000..07764d8 --- /dev/null +++ b/searching/searching.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/searching/src/main/java/com/wbrawner/algorithms/searching/LinearSearch.java b/searching/src/main/java/com/wbrawner/algorithms/searching/LinearSearch.java new file mode 100644 index 0000000..8da938e --- /dev/null +++ b/searching/src/main/java/com/wbrawner/algorithms/searching/LinearSearch.java @@ -0,0 +1,22 @@ +package com.wbrawner.algorithms.searching; + +/** + * Linear search begins at the first index, looping through each element of the array in order and comparing the + * value at each index with the value being searched for. + */ +class LinearSearch { + /** + * Search the given array for the given element using the Linear search algorithm + * + * @return The index of the array where the element can be found or -1 if it can't be found + */ + static int search(int[] array, int element) { + for (int i = 0; i < array.length; i++) { + if (array[i] == element) { + return i; + } + } + + return -1; + } +} diff --git a/searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedLinearSearchTest.java b/searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedLinearSearchTest.java new file mode 100644 index 0000000..c3d446c --- /dev/null +++ b/searching/src/test/java/com/wbrawner/algorithms/searching/ParameterizedLinearSearchTest.java @@ -0,0 +1,33 @@ +package com.wbrawner.algorithms.searching; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import static junit.framework.TestCase.assertEquals; + +@RunWith(Parameterized.class) +public class ParameterizedLinearSearchTest { + + @Parameterized.Parameter() + public int[] array; + + @Parameterized.Parameter(1) + public int searchFor; + + @Parameterized.Parameter(2) + public int expectedIndex; + + @Test + public void searchTest() { + assertEquals( + expectedIndex, + LinearSearch.search(array, searchFor) + ); + } + + @Parameterized.Parameters + public static Object[][] getData() { + return SearchData.get(); + } +} \ No newline at end of file diff --git a/searching/src/test/java/com/wbrawner/algorithms/searching/SearchData.java b/searching/src/test/java/com/wbrawner/algorithms/searching/SearchData.java new file mode 100644 index 0000000..a680a08 --- /dev/null +++ b/searching/src/test/java/com/wbrawner/algorithms/searching/SearchData.java @@ -0,0 +1,68 @@ +package com.wbrawner.algorithms.searching; + +class SearchData { + /** + * Test helper method to get test data for searching tests + * + * @return a multidimensional array of unsorted and sorted arrays wrapped + * in an array + */ + static Object[][] get() { + return new Object[][]{ + // First data set - unsorted array with even number of items + new Object[]{ + // Haystack + new int[]{5, 4, 1, 8, 7, 2, 6, 3}, + // Needle + 2, + // Expected index + 5, + }, + // Second data set - sorted array with even number of items + new Object[]{ + // Haystack + new int[]{1, 2, 3, 4, 5, 6, 7, 8}, + // Needle + 2, + // Expected index + 1, + }, + // Third data set - unsorted array with odd number of items + new Object[]{ + // Haystack + new int[]{9, 5, 4, 1, 8, 7, 2, 6, 3}, + // Needle + 3, + // Expected index + 8, + }, + // Fourth data set - sorted array with odd number of items + new Object[]{ + // Haystack + new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, + // Needle + 7, + // Expected index + 6, + }, + // Fifth data set - empty array + new Object[]{ + // Haystack + new int[]{}, + // Needle + 4, + // Expected index + -1, + }, + // Sixth data set - a single integer array + new Object[]{ + // Haystack + new int[]{1}, + // Needle + 1, + // Expected index + 0, + }, + }; + } +} diff --git a/settings.gradle b/settings.gradle index 24c4f1c..c8ddb14 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,3 +1,3 @@ rootProject.name = 'algorithms' -include 'sorting' +include 'searching', 'sorting'