| | |
Sequential search in java
![]() |
•
•
Join Date: Nov 2004
Posts: 2
Reputation:
Solved Threads: 0
hi.i've written a program that loads #'s into an array and sorts them.The end user then inputs a number and the program is supposed to perform a sequential search for the input number and tell wether it was found or not found.I'm hoping someone canhelp me with my sequential search.binary gave me enough trouble.any idea?
>binary gave me enough trouble.
Binary search is leaps and bounds more complex than sequential search. Start at the beginning, and go to the end. If you find a match, stop. What's so hard about that? You probably solved the first half of the problem when you printed the sorted array (ie. start at the beginning and go to the end).
Binary search is leaps and bounds more complex than sequential search. Start at the beginning, and go to the end. If you find a match, stop. What's so hard about that? You probably solved the first half of the problem when you printed the sorted array (ie. start at the beginning and go to the end).
I'm here to prove you wrong.
Java Syntax (Toggle Plain Text)
/** * Requires j2se 5.0 (version 1.5). This uses class Scanner, static import, * and some methods of class Arrays. */ import java.util.Arrays; import java.util.Scanner; import static java.lang.Math.*; public class Prog3c_1 { public static void main(String... args) { Scanner sc = new Scanner(System.in); int j, k, number, size; boolean found = false; System.out.print("Enter the array size: "); size = sc.nextInt(); int[] intArray = new int[size]; for (j=0; j<size; j++) intArray[j] = (int) (random()*100); System.out.println("A " + size + " element int array of random " + "ints between 0 and 99 has been loaded."); System.out.print("Guess one of the numbers: "); number = sc.nextInt(); k=0; while (!found && k<size) if (intArray[k]==number) found=true; else k +=1; System.out.println(); if (found) System.out.println("Found in position " + k); else System.out.println("The number is not found"); System.out.println("\nThe random array for verification is:\n"); System.out.println(Arrays.toString(intArray)); } }
![]() |
Similar Threads
- three algorithms=sequential search, the binary and interjection..??Problem! (Computer Science)
- please help me solve this! How to search in java using MYSQL database? (Java)
- sequential search (cannot import string functions) (Python)
- Create an array of 100 random integers in a sequential search (C++)
- java sequential file? (Java)
Other Threads in the Java Forum
- Previous Thread: Your Homework
- Next Thread: loop to create arrays when reading a file
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card chat class classes client code collision columns component constructor crashcourse database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress integer intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program programming project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






