Binary Search Method - Find Int Within Array
May 14, 2014
We are required to write a binary‐search method to find an int within an array of ints. Without using any functions besides the basics (for loops, if statements, etc.)
I have this so far:
public int binarySearch(int[] ints, int n) {
for(int i = 0; i < ints.length; i++) {
if(ints[i] == n) {
return ints[i];
}
}
}
Does this look somewhat correct?
View Replies
ADVERTISEMENT
Apr 22, 2014
I want to create a search method that returns the frequency of a word in the search method.
public class IndexTree {
private class TreeNode {
TreeNode left;
String word;
int frequency;
TreeNode right;
[Code] .....
View Replies
View Related
Apr 19, 2015
I am currently taking a class thats requires me to use flowcharts in a way t figure out algithrims This is the flowchart that i need to use: [URL] .....
This is my current code.
public int binSearch(int target) {
int first = 0;
int last = count -1;
int found =0;
int middle=0;
while (first <=last && found == 0)
[Code] ......
View Replies
View Related
Mar 25, 2014
Doing an early exercise out of the Java Examples in a Nutshell book and they are asking for 'an efficient search algorithm to find the desired position' of two floats in a sorted array that bound an int. My try is below:
public static int search(int searchnum, float[] nums){
int low = 0;
int high = nums.length - 1;
int mid = (low + high) / 2;
while(low < high){
if(nums[mid] < searchnum){
[Code] ....
This is working for the example but I would like to know if it is considered 'efficient' or even good?
View Replies
View Related
Mar 23, 2014
I'm just getting two errors concerning line 38 where it has Arrays.sort(int roomList); and the errors state that ".class is expected" and so is a semicolon. What did I do wrong? Also, how might I tweak the code to display "Occupied" or "Unoccupied" depending on the room that was entered?
Also we're not allowed to make use of API method for binary search so that's out of the question.
import java.util.Scanner;
import java.util.Arrays;
public class HotelRoom
{
public static void main(String[] args)
{
Scanner stdIn = new Scanner(System.in);
int[] roomList = new int[25]; // occupied rooms
[Code] ....
View Replies
View Related
Nov 3, 2014
How should I call my findSmallest method in the main class.. Here is the code:
public class Tester {
public static void main(String[] args){
try {
BinaryTree<Integer> myTree2 = new BinaryTree<Integer>();
myTree2.insert(5);
myTree2.insert(2);
myTree2.insert(7);
[Code] ....
So the question is what kind of parameter I should pass in myTree2.findSmallest()??? Here is my findSmallest method..
public E findSmallest(Node<E> parent){
if(parent.left.left == null){
E returnValue = parent.left.data;
parent.left = parent.left.right;
return returnValue;
} else {
return findSmallest(parent.left);
}
}
View Replies
View Related
May 11, 2012
I'm doubted regarding the implementation of Collections.binarySearch() method on an ArrayList of objects of a custom class Movie.
Here is the movie class :
public class Movie implements Comparable<Movie> {
String movieName;
String rating;
String director;
String theme;
[Code] .....
The sort/binarySearch in searchByMovieName function is done with natural sorting (and Comparable Interface in Movie class). I mean no comparators involved here. And the comparator that I used for sorting/binarySearching on Movies Director attribute in searchByMovieDirector function is :
public class MovieDirectorComparator implements Comparator<Movie> {
public int compare(Movie movie1, Movie movie2) {
return movie1.getDirector().compareToIgnoreCase(movie2.getDirector());
}
}
But I was not able to implement binarySearch ?? How to implement the binarySearch here. I have google to see only binarySearch working on Arrays or probably ArrayList of String only, but not on ArrayList of custom objects.
View Replies
View Related
Feb 24, 2014
Operator is undefined for argument type. Error is located at the end of the binary search method array[position] < key
import java.util.Arrays;
public class binarySearch {
public static <T extends Comparable<T>> int binarysearch(T key, T[] array) {
int start = 0;
int end = array.length - 1;
int position =-1;
while (start <= end && position == -1) {
[Code]....
View Replies
View Related
Jul 10, 2014
I'm (failing at) writing a program that searches an array using binary search, but I can't get it to work.
My code so far is this:
Java Code:
package sorting;
import java.lang.*;
import java.util.*;
public class sorteh {
public static void main(String [] args){
int[] array=new int [20]; //creates new array
for (int x=0;x<array.length;x++){ //populates array
array[x]=x*3+1;
[code]...
I copied what a website did for the sorting part, but if I have low=0 and high=19, wouldn't mid not be an int?
View Replies
View Related
Mar 29, 2015
I have some work where I have to create a binary tree. Anyway I am stuck on one of the classes which has several methods I must fill in! The code is below
The headers for each method are: public Node find (member p1)The method find(Member p1) is used to return the node which contains a particular member. It is a private method because the interface to CabinetTree should not provide direct access to the internal nodes. It makes use of the method Node findNode (Member p1, Node current)
and the next method is : Node findNode(Member p1, Node current) which searches the tree from current node to find a node whose value is equal to the Member p1, i.e. a node in the tree such that p1.equals(current.getMember()) is true. If there is no such node then the method should return null.
public class CabinetTree {
private Node head;
CabinetTree(Member p1) {
head = new Node(p1, null);
[Code] ....
View Replies
View Related
Mar 30, 2014
This is the code fragment I have for searching my ArrayList. Now, each contact is stored in the ArrayList with five elements (first name, last name, etc.) and they're Strings. The error I get when I try to compile my program lies within this code fragment. It says it cannot find the symbol for the search method. I'm not quite sure what to do with this error.
int foundIndex = SAAddressBook.search(aBook);
System.out.println();
if (foundIndex > -1)
aBook.get(foundIndex).displayContact();
else {
System.out.println("No Entry Found");
}
View Replies
View Related
Sep 1, 2014
I'm trying to build a method that can search a binary search tree for a specific target and then return the number of probes it took to get there. It seems to me that the best way to do this would be a recursive search method and a counter that tracks the number of calls. But I'm struggling with how to implement this. Here's the code I have so far. what works/doesn't work with the method.
// Method to search the tree for a specific name and
// return the number of probes
public T search(BTNode<T> btNode) {
[Code]....
View Replies
View Related
Jun 8, 2014
im trying to make a linear search method to check if a number 'n' is included in an array.
PHP Code:
package test;
public class Recursion {
public static void main(String[] args) {
}
static int linearSearch(int n, int[] array)
[code]....
Im getting the following error: this method must have a result type of type int ?? i already have a return type of type int...
View Replies
View Related
May 26, 2015
I have problem in searching for the words from my text file.. im using binary search technique
private boolean doSearchQuery(String searchQuery) throws IOException {
Log.i(TAG, "in doSearchQuery, query string: " + searchQuery);
boolean result = false;
toSort = new ArrayList<String>();
myTv = (TextView) findViewById(R.id.myFile);
[Code] .....
View Replies
View Related
Mar 24, 2014
I am trying to make a program that compares the 3 different search algorithms(sequential/binary/hashing). I have already made the sequential search algorithm, binary and hashing part of the code.My program OPENS a data file, and then OPENS a key data file. and then searches through them using both. How to go about the binary and hashing search algorithm (in the driver program).
*I have included a zip file with:
-base program
-driver program
-data file
-key data file
View Replies
View Related
Apr 26, 2015
package bisecsearchsmccr;
import java.util.Random;
public class BisecSearchSMcCr
{
public static void main(String[] args) {
String[] names =
[Code] ....
I get this error:
run:
Johann
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 41
at bisecsearchsmccr.BisecSearchSMcCr.bisect(BisecSearchSMcCr.java:72)
at bisecsearchsmccr.BisecSearchSMcCr.main(BisecSearchSMcCr.java:42)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
View Replies
View Related
Apr 20, 2015
I have created a binary search with 3 subsets, some aslo call it a ternary search and have come up with a minor problem. If you run the code as posted below it just runs until you quit it. If anyother value in the array is searched it is found.
/*
public class BinarySearchDemo {
public static void main(String[] args) {
String[] songs = {"Ace Of Spades", "Beyond the Realms Of Death", "Breaking The Law",
[Code].....
View Replies
View Related
Dec 7, 2014
How do I make it so I am able to enter any number rather then just the numbers in the arrays? I want it to be able to find the position of any number between 0-100.
import javax.swing.*;
public class BinarySearch {
public static void main(String[] args) {
int array[] = new int[11];
array[0] = 10;
array[1] = 20;
array[2] = 30;
array[3] = 40;
array[4] = 50;
array[5] = 60;
[code]...
View Replies
View Related
Sep 15, 2014
The point is to use a binary search to determine how many steps it would take to get to int X, int Y in a grid that is N by N. Picture a parking lot that is square and your car is at (x,y). You can honk your horn from your key to determine the direction of the car. I need to return the amount of steps to get to the car. You can't step diagonally. I am currently getting an error that causes an infinite loop and I can't fix it.
public class ParkingLot {
public int search(int N, int X, int Y) {
int minX = 0, maxY = N, minY = 0, maxX = N;
int num = 0;
int curX, curY;
int newCurX, newCurY;
curX = (minX + maxX)/2;
curY = (minY + maxY)/2;
while (curX != X || curY != Y)
[code]....
View Replies
View Related
Dec 10, 2014
Righto, so I've crafted a binary search and a sequential search. The sequential search works perfectly fine.
However; my binary search doesn't. If I enter in incorrect data, it tells me the data I entered was incorrect. But if I enter in correct data, my sequential search tells me my datas correct, but binary search tells me I'm still incorrect. Here's my binary search + the test program.
public class ValidatorWilson
{
int[] accountNumbers = {5658845, 4520125, 7895122, 8777541, 8451277, 1302850,
8080152, 4562555, 5552012, 5050552, 7825877, 1250255,
1005231, 6545231, 3852085, 7576651, 7881200, 4581002};
public boolean partTwo(int numberCheck)
{
if (accountNumbers.length == 0)
[Code] ....
View Replies
View Related
Aug 2, 2014
If an array has been sorted using a comparator then why is it necessary to pass on that comparator to the binaryserach method. What I want to know is that how come the presence of a comparator reference affect the way the algorithm works?
View Replies
View Related
Apr 15, 2014
The problem is it is returning -2, and also returning false when it should be true. There is no error it just is not working correctly.
import java.util.Arrays;
import java.util.Scanner;
/**
* This Script will allow you to add e-Mails and than beable to search for them.
*/
public class eMailSeacher
{
public static void main(String[] args)
[Code] ....
1. Enter an Email
2. Find an existing email
3. Exit
1
Enter the users E-Mail:
josh
-1
Insertion successful.
ans so on .....
View Replies
View Related
Apr 11, 2015
I am writing binary search method. I don't want to use recursive way, I want to write this method iteratively
Java Code:
public boolean binarySearch(int[] T, int min, int max, int target)
{
int mid=(min+max)/2;
boolean found = false;
int index=0;
while (!found && T.length <= 0 )
{
if (target == mid)
{
found = true;
[Code] ....
View Replies
View Related
Dec 5, 2014
I made a method which takes values from a data set and finds out which one is the highest value. When it finds the highest value, it returns the country which is associated with the value. Here's the data set.
"Country""Total CO2 2005 (million tonnes)""Road CO2 (million tonnes)""Road CO2 per person (tonnes)""Cars per 1000 people"
10
USA5951.131530.35.16777
UK2573.4119.681.99470
Italy476.08116.862592
Germany841.78150.211.82550
Canada553.02123.423.82562
France414.03128.132.04477
Russia1575.44114.690.8178
Japan1254.47224.241.76447
China5100.6228.020.317
India1147.4691.060.18
So if the number was 5951.13 then the program would return USA. How do I do that? I've already started on trying to get this code to work but it doesn't seem to compiler so what's wrong with it?
public static CO2Data highest (CO2Data [] arr2){
Scanner sc = new Scanner(System.in);
CO2Data highestindex = arr2[0];
CO2Data currentcountry = arr2[0].getCountry(sc.nextLine());
[Code] ....
Also the array is a CO2Data array which is part of the following class:
public class CO2Data { //The CO2Data class will be called in other methods
private String country; //A private variable will prevent other users from accessing and changing these variables.
private double totalCO2;
private double roadCO2;
private double CO2PerPerson;
private int carsPerPerson;
public CO2Data() {
country = "";//this sets the initial values for the different variables
[code].....
View Replies
View Related
May 17, 2015
I am trying to implement a simple binary search tree . How can I make a node in the tree which should store two pieces of information: a String variable called name, and a int variable called mark. ..
public class BinarySearchTree<E> implements Comparable<E> {
public BinaryTree<E> root;
int size;
int mark;
String name;
// Constructor
public BinarySearchTree()
[Code]...
View Replies
View Related
Aug 17, 2014
I don't see any nodes that I add. Not sure why getting this error.
duplicate found
Exception in thread "main" java.lang.NullPointerException
at binarysearchtree.delete(binarysearchtree.java:111)
at binarysearchtree.main(binarysearchtree.java:196)
Java Result: 1
public class node<T>
[Code] .....
View Replies
View Related