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?

Posted 14 May 2014 - 06:47 PM

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:

Does this look somewhat correct?

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?

Posted 14 May 2014 - 09:08 PM

No, that's a linear search. A binary search reduces the search space by fifty percent on each iteration, so the time it takes is proportional to log(n) in the worst case. A linear search reduces the search space by one on each iteration, and so it takes time proportional to n.

I'm starting to think you should spend more time with your books and less time posting every half-assed stab at an answer that you come up with.

I think that would help you a lot more.

I'm starting to think you should spend more time with your books and less time posting every half-assed stab at an answer that you come up with.

I think that would help you a lot more.

Posted 15 May 2014 - 03:22 PM

A binary search splits the array in half. The first decision it makes is, does it need to search the lower haf of the array or the upper half.

In order to do that it either is passed a sorted array or sorts it within the method.

In order to do that it either is passed a sorted array or sorts it within the method.

Posted 15 May 2014 - 04:18 PM

This is what I came up with (after going through selectionSort in ascending order):

public int binarySearch(int[] ints, int n) { selectionSort(ints); int low = 0; int high = ints.length - 1; int mid = (low + high) / 2; while(low <= high && (ints[mid] != n)){ if(ints[mid] < n) { low = mid + 1; } else { high = mid - 1; } mid = (low + high) /2; } if(low > high) { mid = -1; //returns -1 if n is not in the array of ints } return mid; }

Page 1 of 1

- Caffeine Lounge
- Corner Cubicle
- Student Campus
- Software Development
- Industry News
- Introduce Yourself
- Nightmare.In.Code

- C and C++
- VB.NET
- Java
- C#
- Python
- PHP
- Mobile Development
- ASP.NET
- .NET Framework
- Ruby
- Game Development
- Assembly
- Databases
- ColdFusion
- VB6
- Other Languages
- 52 Weeks Of Code

- Web Development
- HTML & CSS
- JavaScript
- Graphic Design
- Flash & ActionScript
- Blogging
- SEO & Advertising
- Web Servers & Hosting
- Site Check

- Dijkstra's Algorithm
- Book Review: Murach's Beginning Java with NetBeans
- Graph Data Structure Tutorial
- Swing, Passive Model-View-Presenter in 5 minutes.
- Book Review: Murach's Java Servlets and JSP
- Phobos - A JavaFX Games Engine: Part 2 - JavaFX Scene API and the FSM
- Maven Tutorial 2 - Adding Dependencies
- Maven Tutorial 1 - Installation and Getting Started
- Phobos - A JavaFX Games Engine: Part 1 - Intro to Threading and DP
- Swing to JavaFX
**220 More Java Tutorials...**

- C Snippets
- C++ Snippets
- Java Snippets
- Visual Basic Snippets
- C# Snippets
- VB.NET Snippets
- ASP.NET Snippets
- PHP Snippets
- Python Snippets
- Ruby Snippets
- ColdFusion Snippets
- SQL Snippets
- Assembly Snippets
- Functional Programming Snippets
- Perl Snippets
- HTML/CSS Snippets
- Javascript Snippets
- Flash/ActionScript Snippets
- ASP Snippets
- Linux, Unix, and Bash Snippets
- Other Languages Snippets
- Regex

A

Server: secure3