Add Linear search algorithm
This commit is contained in:
parent
f6cc3e2878
commit
6f1256575d
15 changed files with 174 additions and 41 deletions
|
@ -24,29 +24,5 @@
|
|||
<option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" />
|
||||
</formatting-settings>
|
||||
</DBN-SQL>
|
||||
<DBN-PSQL>
|
||||
<case-options enabled="false">
|
||||
<option name="KEYWORD_CASE" value="lower" />
|
||||
<option name="FUNCTION_CASE" value="lower" />
|
||||
<option name="PARAMETER_CASE" value="lower" />
|
||||
<option name="DATATYPE_CASE" value="lower" />
|
||||
<option name="OBJECT_CASE" value="preserve" />
|
||||
</case-options>
|
||||
<formatting-settings enabled="false" />
|
||||
</DBN-PSQL>
|
||||
<DBN-SQL>
|
||||
<case-options enabled="false">
|
||||
<option name="KEYWORD_CASE" value="lower" />
|
||||
<option name="FUNCTION_CASE" value="lower" />
|
||||
<option name="PARAMETER_CASE" value="lower" />
|
||||
<option name="DATATYPE_CASE" value="lower" />
|
||||
<option name="OBJECT_CASE" value="preserve" />
|
||||
</case-options>
|
||||
<formatting-settings enabled="false">
|
||||
<option name="STATEMENT_SPACING" value="one_line" />
|
||||
<option name="CLAUSE_CHOP_DOWN" value="chop_down_if_statement_long" />
|
||||
<option name="ITERATION_ELEMENTS_WRAPPING" value="chop_down_if_not_single" />
|
||||
</formatting-settings>
|
||||
</DBN-SQL>
|
||||
</code_scheme>
|
||||
</component>
|
|
@ -4,6 +4,8 @@
|
|||
<bytecodeTargetLevel>
|
||||
<module name="algorithms_main" target="1.8" />
|
||||
<module name="algorithms_test" target="1.8" />
|
||||
<module name="searching_main" target="1.8" />
|
||||
<module name="searching_test" target="1.8" />
|
||||
<module name="sorting_main" target="1.8" />
|
||||
<module name="sorting_test" target="1.8" />
|
||||
</bytecodeTargetLevel>
|
||||
|
|
|
@ -8,9 +8,11 @@
|
|||
<option name="modules">
|
||||
<set>
|
||||
<option value="$PROJECT_DIR$" />
|
||||
<option value="$PROJECT_DIR$/searching" />
|
||||
<option value="$PROJECT_DIR$/sorting" />
|
||||
</set>
|
||||
</option>
|
||||
<option name="useAutoImport" value="true" />
|
||||
</GradleProjectSettings>
|
||||
</option>
|
||||
</component>
|
||||
|
|
|
@ -5,6 +5,10 @@
|
|||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/algorithms.iml" filepath="$PROJECT_DIR$/.idea/modules/algorithms.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/algorithms_main.iml" filepath="$PROJECT_DIR$/.idea/modules/algorithms_main.iml" group="algorithms" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/algorithms_test.iml" filepath="$PROJECT_DIR$/.idea/modules/algorithms_test.iml" group="algorithms" />
|
||||
<module fileurl="file://$PROJECT_DIR$/searching/searching.iml" filepath="$PROJECT_DIR$/searching/searching.iml" group="searching" />
|
||||
<module fileurl="file://$PROJECT_DIR$/searching/searching.iml" filepath="$PROJECT_DIR$/searching/searching.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/searching/searching_main.iml" filepath="$PROJECT_DIR$/.idea/modules/searching/searching_main.iml" group="searching" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/searching/searching_test.iml" filepath="$PROJECT_DIR$/.idea/modules/searching/searching_test.iml" group="searching" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/sorting.iml" filepath="$PROJECT_DIR$/.idea/modules/sorting.iml" group="sorting" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/sorting/sorting_main.iml" filepath="$PROJECT_DIR$/.idea/modules/sorting/sorting_main.iml" group="sorting" />
|
||||
<module fileurl="file://$PROJECT_DIR$/.idea/modules/sorting/sorting_test.iml" filepath="$PROJECT_DIR$/.idea/modules/sorting/sorting_test.iml" group="sorting" />
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.id="algorithms:main" external.linked.project.path="$MODULE_DIR$/../.." external.root.project.path="$MODULE_DIR$/../.." external.system.id="GRADLE" external.system.module.group="com.wbrawner" external.system.module.type="sourceSet" external.system.module.version="1.0-SNAPSHOT" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output url="file://$MODULE_DIR$/../../out/production/classes" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$/../../src/main">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../src/main/resources" type="java-resource" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../src/main" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.id="algorithms:test" external.linked.project.path="$MODULE_DIR$/../.." external.root.project.path="$MODULE_DIR$/../.." external.system.id="GRADLE" external.system.module.group="com.wbrawner" external.system.module.type="sourceSet" external.system.module.version="1.0-SNAPSHOT" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output-test url="file://$MODULE_DIR$/../../out/test/classes" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$/../../src/test">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../src/test/java" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../src/test/resources" type="java-test-resource" />
|
||||
</content>
|
||||
<content url="file://$MODULE_DIR$/../../src/test" />
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module" module-name="algorithms_main" />
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.id=":sorting:main" external.linked.project.path="$MODULE_DIR$/../../../sorting" external.root.project.path="$MODULE_DIR$/../../.." external.system.id="GRADLE" external.system.module.group="com.wbrawner" external.system.module.type="sourceSet" external.system.module.version="1.0-SNAPSHOT" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output url="file://$MODULE_DIR$/../../../sorting/out/production/classes" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$/../../../sorting/src/main">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../../sorting/src/main/java" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../../sorting/src/main/resources" type="java-resource" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
|
|
|
@ -1,11 +1,10 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.id=":sorting:test" external.linked.project.path="$MODULE_DIR$/../../../sorting" external.root.project.path="$MODULE_DIR$/../../.." external.system.id="GRADLE" external.system.module.group="com.wbrawner" external.system.module.type="sourceSet" external.system.module.version="1.0-SNAPSHOT" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager">
|
||||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
|
||||
<output-test url="file://$MODULE_DIR$/../../../sorting/out/test/classes" />
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$/../../../sorting/src/test">
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../../sorting/src/test/java" isTestSource="true" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/../../../sorting/src/test/resources" type="java-test-resource" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
|
|
11
README.md
11
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)
|
14
searching/build.gradle
Normal file
14
searching/build.gradle
Normal file
|
@ -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'
|
||||
}
|
13
searching/searching.iml
Normal file
13
searching/searching.iml
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module external.linked.project.id=":searching" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$/.." external.system.id="GRADLE" external.system.module.group="com.wbrawner" external.system.module.version="1.0-SNAPSHOT" type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<excludeFolder url="file://$MODULE_DIR$/.gradle" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/build" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/out" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
</component>
|
||||
</module>
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
|
@ -1,3 +1,3 @@
|
|||
rootProject.name = 'algorithms'
|
||||
include 'sorting'
|
||||
include 'searching', 'sorting'
|
||||
|
||||
|
|
Loading…
Reference in a new issue