Skip to content

Instantly share code, notes, and snippets.

@JorgeOlvera
Last active August 29, 2015 13:57
Show Gist options
  • Save JorgeOlvera/9362548 to your computer and use it in GitHub Desktop.
Save JorgeOlvera/9362548 to your computer and use it in GitHub Desktop.
import java.io.*;
import java.util.*;
public class searchAndDestroy {
public static void main(String[] args) throws IOException{
Scanner input = new Scanner(System.in);
//Establish array length
System.out.println("Hey there! Please establish the length of the array");
int positions = input.nextInt();
int[] inputArray = new int[positions];
Random num = new Random();
for (int i = 0; i < positions; i++) {
inputArray[i] = num.nextInt(20);
}
mergeSort(inputArray);
System.out.println("What number are you looking for?");
int toFind = input.nextInt();
int lapos;
lapos = linear(inputArray, toFind);
int bazooka;
bazooka = howMany(inputArray, toFind);
System.out.println("In Linear Search");
System.out.println("Your number first appeared on index ");
System.out.println(lapos);
System.out.println("Your number was repeated ");
System.out.println(bazooka + " time(s)");
int bin;
bin = firstOne(inputArray, toFind);
int ban;
ban = whereIsIt(inputArray, toFind);
System.out.println("In Binary Search...");
System.out.println("Your number first appears on index ");
System.out.println(bin);
System.out.println("Your number was repeated ");
System.out.println(ban + " time(s)");
}
static void mergeSort(int[] A) {
if (A.length > 1) {
int q = A.length/2;
int[] leftArray = Arrays.copyOfRange(A, 0, q);
int[] rightArray = Arrays.copyOfRange(A,q,A.length);
mergeSort(leftArray);
mergeSort(rightArray);
merge(A,leftArray,rightArray);
}
}
public static void merge(int[] a, int[] l, int[] r) {
int totElem = l.length + r.length;
//int[] a = new int[totElem];
int i,li,ri;
i = li = ri = 0;
while ( i < totElem) {
if ((li < l.length) && (ri<r.length)) {
if (l[li] < r[ri]) {
a[i] = l[li];
i++;
li++;
}
else {
a[i] = r[ri];
i++;
ri++;
}
}
else {
if (li >= l.length) {
while (ri < r.length) {
a[i] = r[ri];
i++;
ri++;
}
}
if (ri >= r.length) {
while (li < l.length) {
a[i] = l[li];
li++;
i++;
}
}
}
}
//return a;
}
public static int linear(int [] inputArray, int toFind) {
for (int a=0; a<inputArray.length; a++) {
if (inputArray[a] == toFind) {
return a;
}
}
return -1;
}
public static int howMany(int [] inputArray, int toFind) {
int counT = 0;
for (int b=0; b<inputArray.length; b++) {
if (inputArray[b] == toFind) {
counT = counT + 1;
}
}
return counT;
}
public static int firstOne(int [] inputArray, int toFind) {
int mid;
int low = 0;
int high = inputArray.length-1;
while (low<=high) {
mid = (low + high) /2;
if (inputArray[mid] > toFind) {
high = mid - 1;
} else if (inputArray[mid] < toFind) {
low = mid + 1;
} else {
return mid;
}
}
return -1;
}
public static int whereIsIt(int [] inputArray, int toFind) {
int counT = 0;
int lol = toFind;
for (int k =0; k<inputArray.length; k++) {
if (inputArray[lol] == toFind) {
counT = counT + 1;
lol = lol + 1;
} else {
break;
}
}
return counT;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment