Sequential search in java

Reply

Join Date: Nov 2004
Posts: 2
Reputation: josirucu is an unknown quantity at this point 
Solved Threads: 0
josirucu josirucu is offline Offline
Newbie Poster

Sequential search in java

 
0
  #1
Nov 15th, 2004
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Sequential search in java

 
0
  #2
Nov 15th, 2004
>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).
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: Sequential search in java

 
0
  #3
Nov 17th, 2004
  1. /**
  2.  * Requires j2se 5.0 (version 1.5). This uses class Scanner, static import,
  3.  * and some methods of class Arrays.
  4.  */
  5.  
  6. import java.util.Arrays;
  7. import java.util.Scanner;
  8. import static java.lang.Math.*;
  9.  
  10. public class Prog3c_1
  11. {
  12. public static void main(String... args)
  13. {
  14. Scanner sc = new Scanner(System.in);
  15.  
  16. int j, k, number, size;
  17. boolean found = false;
  18.  
  19. System.out.print("Enter the array size: ");
  20. size = sc.nextInt();
  21.  
  22. int[] intArray = new int[size];
  23.  
  24. for (j=0; j<size; j++)
  25. intArray[j] = (int) (random()*100);
  26.  
  27. System.out.println("A " + size + " element int array of random "
  28. + "ints between 0 and 99 has been loaded.");
  29. System.out.print("Guess one of the numbers: ");
  30. number = sc.nextInt();
  31.  
  32. k=0;
  33. while (!found && k<size)
  34. if (intArray[k]==number)
  35. found=true;
  36. else
  37. k +=1;
  38.  
  39. System.out.println();
  40. if (found)
  41. System.out.println("Found in position " + k);
  42. else
  43. System.out.println("The number is not found");
  44.  
  45. System.out.println("\nThe random array for verification is:\n");
  46. System.out.println(Arrays.toString(intArray));
  47. }
  48. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,612
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Sequential search in java

 
0
  #4
Nov 18th, 2004
There's always some goofball who encourages lazy students by doing their homework for them.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC