943,931 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 2983
  • Java RSS
Nov 3rd, 2004
0

find out the error in my code

Expand Post »
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 :

Java Syntax (Toggle Plain Text)
  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 :

Java Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
shantuli is offline Offline
16 posts
since Jul 2004
Nov 3rd, 2004
0

Re: find out the error in my code

>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:
Java Syntax (Toggle Plain Text)
  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:
Java Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: java code problem
Next Thread in Java Forum Timeline: Iterative and Recursive Demo





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC