Writing A Program That Searches Array Using Binary Search

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


ADVERTISEMENT

Writing Binary Search Program And Keep Getting Errors

Feb 24, 2014

Java Code:

public class Search
{
public static final int NOT_FOUND = -1;
public static int binarySearch( int [ ] a, int x )
{
int low = 0;
int high = a.length - 1;
int mid;
while ( low <= high )

[Code] ....

I keep getting illegal start of expression at ={-3,10,5,24,45.3,10.5}

It says I have 12 errors and I have tried so many things to fix it and I just don't know what to change to make it work.

View Replies View Related

Writing A Program To Search Bluetooth Devices

May 2, 2014

Im trying to write a java program to search nearby bluetooth devices.. Im using Bluecove-glp-2.1.0 and bluecove 2.1.0..Im working on Ubuntu 13.04 LTS.I typend a code to get the local Device name..

public class BluetoothSearch {
public static void main(String[] args) {
LocalDevice ld= null ;
String name = ld.getBluetoothAddress();
System.out.print(name);
}
}

this is my code.. It returns me null. doesn't return me the local Device.

View Replies View Related

Binary Search Of Float Array?

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

Binary Search Code For Array?

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

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 View Related

Insertion Points In Array Using Binary Search Using Comparable

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

Ability To Search A Binary Search Tree And Return The Number Of Probes

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

Creating Search Method For Binary Search Tree

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

Write A Program That Searches Through Files?

Dec 10, 2014

I'm trying to use graphics for my programs and here I'm trying to write a program that searches through files for text received from a JTextField. However, it does not seem to be working as my message is not displayed when text is found...

I used applet. Here are the functions:
 
JTextField jtf;
JFrame jf;
public void init()

[Code].....

View Replies View Related

Binary Search For String

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

Binary Search Algorithm

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

Binary Search With Compare To

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

Binary Search With 3 Subsets

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

Binary Search Error

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

Binary Search - Get To Int X Int Y In A Grid That Is N By N

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

Binary And Sequential Search?

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

Binary Search And Comparator

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

Flowchart - Using Binary Search Method

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

Binary Search Returning False

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

How To Write Binary Search Iteratively

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

How To Implement Simple Binary Search Tree

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

Not Getting Correct Results In Binary Search Tree

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

Selection Sort / Binary Search And Recursion

Nov 26, 2014

Here is the code I have at the moment:

/**
* Use selection sort to sort the tracks by name. Return a new, sorted ArrayList of tracks.
*
* @return an ArrayList containing the tracks sorted by name, or null if no tracks exist
*/
public ArrayList<Track> getTracksSortedByName() {
// YOUR CODE HERE
if(tracks == null)
return tracks;

[Code] ....

Here are my 2 issues (there are 2 lines with compiler errors): I feel like I understand selection sort and binary search, but am not sure how to apply it to the more abstract idea of a Track ArrayList (hence the 2 compiler errors). What should I use to make it work. Lastly, I'm very uncomfortable with recursion, so my guess is there is also probably some logical issue with it in the getTracksSortedByName method.

View Replies View Related

Finding Height Of Binary Search Tree?

Nov 13, 2014

So for an assignment I have to write a method that finds that height of this BST. I wrote this:

public int height(Node node)
{
if (node == null)
{
return 0;
}
else
{
return 1 +
Math.max(height(node.left),
height(node.right));
}
}

Which makes sense in my head. However, I added a couple of nodes in and realized I'm not sure how to call this method.

public static void main(String[] args) {
Tree<String, String> tree = new Tree<String, String>();
tree.add("A", "1");//some added stuff
tree.add("B", "2");
tree.add("C", "3");
tree.add("D", "4");
tree.add("E", "5");
tree.add("F", "6");
tree.add("G", "7");
tree.add("H", "8");
System.out.println(tree);
System.out.println(tree.height());

What exactly do I put in the parenthesis after tree.height? If you need to see more of my code I can do that but it is quite lengthy.

View Replies View Related

How To Compare String In Binary Search Tree

Aug 31, 2014

I'm trying to implement a Binary Search Tree that accepts strings. I cannot figure out how to compare the string value in my add method against the node object. While I could make the node class data be the string type I am trying to make the code be as reusable as possible. So my question is this, is there a simple way I can compare the two that I am missing?

public class BTNode<E> {
private E data;
private BTNode<E> left, right;
//constructor
public BTNode(E initialData, BTNode<E> initialLeft, BTNode<E> initialRight) {
data = initialData;
left = initialLeft;
right = initialRight;

[Code] ....

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved