find out the error in my code

Reply

Join Date: Jul 2004
Posts: 16
Reputation: shantuli is an unknown quantity at this point 
Solved Threads: 0
shantuli shantuli is offline Offline
Newbie Poster

find out the error in my code

 
0
  #1
Nov 3rd, 2004
hi,
i am new in java, pls anyone help me to find out the error of my code.

1)Q- Write a java program which implements a simple sequential search algorithm for finding the position of a number in an array.

My code is :

  1. public class linearsearch {
  2. public static void main (String[] args) {
  3. int [] Array = {20,55,10,02,100,02};
  4. int target = 02;
  5. int location = -1;
  6. int i = 0;
  7. while ((Array[i] != target) && ( i<Array.length ))
  8. i++;
  9. if (Array[i] == target)
  10. location = i;
  11.  
  12. System.out.println (i);
  13. }
  14. }

Problem:
it's only showing the 3rd position.but it also should show the 5th position.but it's not showing the 5th position.

2)Q- Extend the program to include an additional method to perform a binary search to achieve the same result as in Question 1.

My code is :

  1.  
  2. class lec3
  3. {
  4. public static void main (String[] args) {
  5. int [] array = {20,55,10,02,100,02};
  6.  
  7. for (int i=0;i<array.length; i++)
  8. {
  9. System.out.println(array[i]+"\n");
  10. System.out.println("the location is ");
  11. }
  12.  
  13. int target = 02;
  14. int location = -1;
  15. int first = 0;
  16. int last = 5;
  17. int h = 0;
  18. int index = 5;
  19. while((h>=1) && (index != 02))
  20. {
  21. int middle = 2;
  22. if(2 = 02)
  23. {
  24. location = middle;
  25. System.out.println(middle);
  26. }
  27. else
  28. {
  29. if(target<middle)
  30. last = (middle - 1);
  31. else
  32. first = (middle + 1);
  33.  
  34. }
  35. }
  36.  
  37. if (middle == target)
  38. location = middle;
  39. System.out.println(middle);
  40. }
  41. }
Problem:
this code has few problem.its not running properly.


thank you very much for helping me

shantuli
Last edited by alc6379; Nov 3rd, 2004 at 7:23 pm. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,566
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: 705
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: find out the error in my code

 
0
  #2
Nov 3rd, 2004
>but it's not showing the 5th position.
Your search only looks for the first occurance. To find subsequent matches, you need to continue the search until the end because you don't know how many matching items there are in the list:
  1. public class Main {
  2. public static void main ( String[] args ) {
  3. int[] array = {20,55,10,2,100,2};
  4. int target = 2;
  5.  
  6. for ( int i = 0; i < array.length; i++ ) {
  7. if ( array[i] == target )
  8. System.out.println ( target + " found at index " + i );
  9. }
  10. }
  11. }
Concerning your second question, the biggest problem is that the array is not sorted. Binary search requires that the input list be in sorted order. Once the array is sorted, the algorithm would look like this:
  1. public class Main {
  2. public static void main ( String[] args ) {
  3. int[] array = {2,2,10,20,55,100};
  4. int target = 2;
  5. int left = 0;
  6. int right = array.length - 1;
  7. int mid = -1;
  8.  
  9. while ( left <= right ) {
  10. mid = ( left + right ) / 2;
  11.  
  12. if ( target == array[mid] )
  13. break;
  14. else if ( target < array[mid] )
  15. right = mid - 1;
  16. else
  17. left = mid + 1;
  18. }
  19.  
  20. if ( mid != -1 )
  21. System.out.println ( target + " found at index " + mid );
  22. else
  23. System.out.println ( target + " not found" );
  24. }
  25. }
Notice in particular how the loop condition is such that left never crosses right and how mid is calculated for every iteration of the loop. The whole point of the binary search is to cut the search area in half by dividing it in the middle based on whether the middle item is larger than or smaller than the target.
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