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
ADVERTISEMENT
May 10, 2014
This was my initial question: I am confused as to why this won't return the desired value. I keep running into an infinite loop or data is returned that I searched.
I still need to work on whether a user enters a search that doesn't exist......that is where I am stuck....
code has been updated......
int low = 0;
int high = subArray.length;
while (low < high) {
System.out.println("You have entered the while loop");
int mid = (low + high) / 2;
[Code] ....
View Replies
View Related
Apr 24, 2014
Is there a way that I can save a binary search algorithm to a text file or something, so that it will always contain more information? The reason why I ask this is because I have BST like project which is supposed to act the game Twenty Questions, were it asks a person 20 Q's until it guess the answer. However, although my project does not require you to save stuff to a file, I find it rather very useful and cool if I could do that.So for example it would start out something like this:
Think of an animal and I will guess it.
Does it have legs? YES
Is it a cat? YES
I win! Continue? YES
Think of an animal and I will guess it.
Does it have legs? NO
Is it a snake? YES
I win! Continue? YES
Think of an animal and I will guess it.
Does it have legs? NO
Is it a snake? NO
I give up. What is it? EARTHWORM
Please type a question whose answer is yes for an earthworm and no for a snake.
DOES IT LIVE UNDERGROUND?
Continue? YES
Think of an animal and I will guess it.
Does it have legs? NO
Does it live underground? NO
Is it a snake? NO
I give up. What is it? FISH
Please type a question whose answer is yes for an earthworm and no for a snake.
DOES IT LIVE IN WATER?
Continue? NO
Good-bye.
However, if the user exits the program then everything will be lost and go to waste. So once again, is there a way that I can save all of these inputs to a file or something and then read it in again?
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
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 29, 2015
I am using what is known as a Depth First Search to solve a maze using my own Stack created class for an assignment. The reason I believe I am getting this error is because my program is never meeting the `finishSpot` condition. I have used the debugger and it looks like it is infinitely stuck at the Point before the `finishSpot`. My logic seems to be mostly correct except until the point where my maze solver tries to finish the maze.
This is a sample maze:
*****
*s* *
* * *
* f*
*****
Here is my Main which uses my created Stack class.
//Creates a Stack and determines the start and endpoints.
public static void solveDFS( char [][] maze ){
LinkedStack stack = new LinkedStack();
Point currentSpot = findPoint( maze,'s' );
Point finishSpot = findPoint( maze, 'f' );
findPath( maze,currentSpot, finishSpot,stack );
[code]....
View Replies
View Related
Apr 29, 2015
I am using what is known as a Depth First Search to solve a maze using my own Stack created class for an assignment. The reason I believe I am getting this error is because my program is never meeting the `finishSpot` condition. I have used the debugger and it looks like it is infinitely stuck at the Point before the `finishSpot`. My logic seems to be mostly correct except until the point where my maze solver tries to finish the maze, so i just need meeting that last condition that is causing my program to crash.
This is a sample maze:
*****
*s* *
* * *
* f*
*****
Here is my Main which uses my created Stack class.
//Creates a Stack and determines the start and endpoints.
public static void solveDFS( char [][] maze ){
LinkedStack stack = new LinkedStack();
Point currentSpot = findPoint( maze,'s' );
Point finishSpot = findPoint( maze, 'f' );
findPath( maze,currentSpot, finishSpot,stack );
[Code] ....
I made a mistake it says my program is crashing which it is not, but simply not meeting the condition.
View Replies
View Related
Oct 4, 2014
I have been working on a program that takes in a 2d array (there is a method to convert to 1d as necessary) and uses it as an 8 puzzle, which the program then solves (if it is solvable) using the A* Search algorithm, outputting each solution move. It needs error checkers/traps/exception handlers, and be able to create an 8 puzzle from user input, create an 8 puzzle from file input, and create an 8 puzzle from file input and then output to another file the solution move sequence. I have gotten nearly everything done except for implement the algorithm itself and the methods for handling input from files or output to files (the input/output file methods are a minor problem that I can probably solve fairly easily, I just want to be able to test the a* with the puzzle created from user input first.
I have taken several cracks at it, but I am not really good with Java in this type of situation and this is hurting me a lot. I have put in a method to determine if a puzzle is solvable, but the A* algorithm still eludes me. I understand the concept of the algorithm, you have a heuristic function, a cost function (each move equals 1 in this case), and you add the two together to get f(n), choosing the move that will get you the closest to your goal (best first search basically). here is what I have so far that works:
import java.io.File;
import java.io.IOException;
import java.util.Queue;
import java.util.Scanner;
public class Puzzle {
public static void main(String[] args) throws IOException {
[Code] ....
I have it outputting things to make sure all that works already, I have all the error handlers, but I think that before I can even implement the A*search algorithm, there may be something else I need. Conceptually, I understand I need Nodes, States and Stacks, and Queues (I think, I am not sure about this), but with the way my code has been made so far, I am not sure how to implement those and then implement them with the A* algorithm. What I should do next? I have looked at the code of other uses of A* algorithm(that is how I found out I might need Java data structures like queue and stack and list), but I am not sure exactly where to go to from where I am.
View Replies
View Related
Feb 7, 2015
Coding (must follow pseudocode algorithm provided) for comparing characters of a word to a puzzle board char[][].
I've gotten the horizontal and vertical searches to work, but I'm having a difficult time figuring out how to search diagonally.
Here's the search algorithm format I have to use:
for each guess word
for each letter of the puzzle
for each letter of the guess word
check for diagonal match
if found, add word to list, break to next guess
I'm assuming there should be a +1 horizontal offset after I find the first letter, but every index I've bumped has caused me to screw up my array bounds. I'm missing something.
Here's the algorithm I'm using for diagonal search. This same algorithm worked for vertical search (minus the offset of course)
//diagonal search
for(String word : guessWords) {//for each guess word
int letterCount = 0;
int index = 0;
for(int j=0; j<grid[index].length; j++) {//for each puzzle letter
[Code] ....
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
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
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
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 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
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
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
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
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
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