Initial Commit
This commit is contained in:
6
Java/RecursiveLargestNumber/.classpath
Normal file
6
Java/RecursiveLargestNumber/.classpath
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
17
Java/RecursiveLargestNumber/.project
Normal file
17
Java/RecursiveLargestNumber/.project
Normal file
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>RecursiveLargestNumber</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,12 @@
|
||||
#Sun Dec 05 14:18:28 EST 2010
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.6
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.6
|
||||
BIN
Java/RecursiveLargestNumber/bin/LargestNumber.class
Normal file
BIN
Java/RecursiveLargestNumber/bin/LargestNumber.class
Normal file
Binary file not shown.
BIN
Java/RecursiveLargestNumber/bin/Main.class
Normal file
BIN
Java/RecursiveLargestNumber/bin/Main.class
Normal file
Binary file not shown.
126
Java/RecursiveLargestNumber/src/LargestNumber.java
Normal file
126
Java/RecursiveLargestNumber/src/LargestNumber.java
Normal file
@@ -0,0 +1,126 @@
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Twenty Codes, LLC
|
||||
* @author ricky barrette
|
||||
* @date Dec 5, 2010
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class LargestNumber {
|
||||
|
||||
private int[] mNumbers;
|
||||
|
||||
/**
|
||||
* Creates a new LargestNumber
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public LargestNumber(int[] numbers) {
|
||||
mNumbers = numbers;
|
||||
sortArray();
|
||||
}
|
||||
|
||||
/**
|
||||
* sorts the array
|
||||
*
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private void sortArray() {
|
||||
Arrays.sort(mNumbers);
|
||||
// for(int item : mNumbers)
|
||||
// System.out.print(item+" ");
|
||||
// System.out.println();
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the largest number
|
||||
* @return
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public int getLargestNumber(){
|
||||
return mNumbers[mNumbers.length-1];
|
||||
// return getLargestNumber(0, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* returns the largest number in the array
|
||||
* @param i
|
||||
* @param index
|
||||
* @return the largest number in the array
|
||||
* @deprecated since we started sorting the array
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private int getLargestNumber(int i, int index) {
|
||||
try{
|
||||
if (mNumbers[index] > i)
|
||||
i = mNumbers[index];
|
||||
} catch (ArrayIndexOutOfBoundsException e){
|
||||
return i;
|
||||
} finally {
|
||||
index++;
|
||||
}
|
||||
return getLargestNumber(i, index);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* finds a number in a sorted array
|
||||
* @param number to find
|
||||
* @return true if the number exist
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public boolean find(int number){
|
||||
int min= 0, max = mNumbers.length - 1;
|
||||
return find(number, min, min + ((max - min) / 2), max) != -1 ? true : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively finds a number in the array
|
||||
* @param number
|
||||
* @param min
|
||||
* @param mid
|
||||
* @param max
|
||||
* @return the index of the value, or -1 if it doesn't exist
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private int find(int number, int min, int mid, int max) {
|
||||
// System.out.println(min+", "+mid+", "+max);
|
||||
|
||||
/*
|
||||
* if the min, mid, max is what were looking for, then
|
||||
* lets return the designated index
|
||||
*/
|
||||
if (mNumbers[min] == number) {
|
||||
return min;
|
||||
}
|
||||
if (mNumbers[mid] == number) {
|
||||
return mid;
|
||||
}
|
||||
if (mNumbers[max] == number) {
|
||||
return max;
|
||||
}
|
||||
|
||||
/*
|
||||
* if we get to this point, lets prep our values for the next round
|
||||
*/
|
||||
if (mNumbers[mid] < number) {
|
||||
min = mid;
|
||||
}
|
||||
if (mNumbers[mid] > number) {
|
||||
max = mid;
|
||||
}
|
||||
mid = min + ((max - min) / 2);
|
||||
|
||||
/*
|
||||
* if the mid is == max -1 then we will exit, or we will loop indefinitely
|
||||
*/
|
||||
if(mid == max-1)
|
||||
return -1;
|
||||
|
||||
return find( number, min, mid, max);
|
||||
}
|
||||
|
||||
}
|
||||
36
Java/RecursiveLargestNumber/src/Main.java
Normal file
36
Java/RecursiveLargestNumber/src/Main.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* @author Twenty Codes, LLC
|
||||
* @author ricky barrette
|
||||
* @date Dec 5, 2010
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is the main driver class
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public class Main {
|
||||
|
||||
int[] mNumber = new int[]{ 1, 2, 3, 5, 45, 7, 9, 10, 16, 2, 100, 42, 23, 9, 0 };
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new Main
|
||||
* @author ricky barrette
|
||||
*/
|
||||
private Main(){
|
||||
LargestNumber ln = new LargestNumber(mNumber);
|
||||
System.out.println(ln.getLargestNumber());
|
||||
System.out.println(ln.find(5));
|
||||
System.out.println(ln.find(34));
|
||||
}
|
||||
|
||||
/**
|
||||
* main method, this is called when the application is first started
|
||||
* @param args
|
||||
* @author ricky barrette
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
new Main();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user