Terms of Service | Privacy Policy | Cookie Policy

Commit c522e9dc authored by Uwe Plonus's avatar Uwe Plonus
Browse files

Initial commit.

parents
# Binary Search
The documentation is found on the [project page](https://java-dojo.github.io/binary-search).
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.java-dojo</groupId>
<artifactId>binary-search</artifactId>
<version>0.1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Java Dojo - Binary Search</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<testng.version>6.8.21</testng.version>
<javadoc.plugin.version>2.10.2</javadoc.plugin.version>
<project-info-reports.plugin.version>2.8</project-info-reports.plugin.version>
<scm-publish.plugin.version>1.1</scm-publish.plugin.version>
<site.plugin.version>3.4</site.plugin.version>
</properties>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>${testng.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>${site.plugin.version}</version>
<configuration>
<skipDeploy>true</skipDeploy>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<executions>
<execution>
<id>stage-for-scm-publish</id>
<phase>post-site</phase>
<goals>
<goal>stage</goal>
</goals>
<configuration>
<skipDeploy>false</skipDeploy>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-scm-publish-plugin</artifactId>
<version>${scm-publish.plugin.version}</version>
<configuration>
<scmBranch>gh-pages</scmBranch>
</configuration>
<executions>
<execution>
<id>scm-publish</id>
<phase>site-deploy</phase>
<goals>
<goal>publish-scm</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>${project-info-reports.plugin.version}</version>
<reportSets>
<reportSet>
<reports>
<report>scm</report>
</reports>
</reportSet>
</reportSets>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>${javadoc.plugin.version}</version>
<reportSets>
<reportSet>
<reports>
<report>javadoc</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
<scm>
<url>https://github.com/java-dojo/binary-search</url>
<developerConnection>scm:git:ssh://git@github.com/java-dojo/binary-search.git</developerConnection>
</scm>
<distributionManagement>
<site>
<id>github</id>
<url>scm:git:ssh://git@github.com/java-dojo/binary-search.git</url>
</site>
</distributionManagement>
</project>
package com.github.javadojo.binarysearch;
/**
*
*/
public class BinarySearch implements Search {
/**
* Searches the value {@code toSearch} in the given (sorted) array
* {@code values}.
*
* @param values the sorted array to search through.
* @param toSearch the value to search.
* @return {@code true} if the value {@code toSearch} is found in the array
* {@code values}.
*/
@Override
public boolean search(final int[] values, final int toSearch) {
throw new UnsupportedOperationException("Not supported yet.");
}
}
package com.github.javadojo.binarysearch;
/**
* An interface to do a search on an array.
*/
public interface Search {
/**
* Searches the value {@code toSearch} in the given array {@code values}.
*
* @param values the array to search through.
* @param toSearch the value to search.
* @return {@code true} if the value {@code toSearch} is found in the array
* {@code values}.
*/
boolean search(int[] values, int toSearch);
}
Task
Your task is to implement the method <<<search>>> in the class
<<<BinarySearch>>>.
Specification
The method <<<search>>> searches over a given array and returns <<<true>>>, if
the value to search for is found.
Implement a binary search. If needed you can add further (private) methods to
the class <<<BinarySearch>>>.
Support
To support you with implementing the method there are unit tests in
<<<BinarySearchTest>>>. The unit tests are disabled at the start. To use them
you have to enable them.
Further Challenges
If you solved this kata already and want a new challenge you can solve this
kata with one of the following constraints:
* If you implemented the binary search with a recursive algorithm then create
an iterative implementation.
* If you implemented the binary search with an iterative algorithm then create
a recursive implementation.
* Implement the method without using an additional method.
* To implement a binary search in a generic way (using generics) use the kata
{{{{https://java-dojo.github.io/advanced-binary-search}Advanced Binary Search}}.
Development Requirements
To perform this kata you need a JDK 8 and Maven 3.3.x.
References
* {{{http://rosettacode.org/wiki/Binary_search}Rosetta Code}}
# Binary Search
The documentation is found on the [project page](https://java-dojo.github.io/binary-search).
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/DECORATION/1.4.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/DECORATION/1.4.0 http://maven.apache.org/xsd/decoration-1.4.0.xsd"
name="Java Dojo - Binary Search">
<skin>
<groupId>org.apache.maven.skins</groupId>
<artifactId>maven-fluido-skin</artifactId>
<version>1.3.1</version>
</skin>
<body>
<links>
<item name="Dojo" href="../index.html"/>
</links>
<menu name="Binary Search">
<item name="Task" href="index.html"/>
</menu>
<menu ref="reports"/>
</body>
</project>
\ No newline at end of file
package com.github.javadojo.binarysearch;
import org.testng.Assert;
import org.testng.annotations.Test;
public class BinarySearchTest {
private final BinarySearch objectToTest = new BinarySearch();
private final int[] testArray = new int[] {
1, 3, 5, 7, 8, 9, 10, 12, 14, 17,
};
@Test(enabled = false)
public void testFound() {
Assert.assertTrue(objectToTest.search(testArray, 1),
"Expected the value 1 to be found.");
}
@Test(enabled = false)
public void testNotFound() {
Assert.assertFalse(objectToTest.search(testArray, 2),
"Expected the value 2 not to be found.");
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment