| | |
find out the error in my code
![]() |
•
•
Join Date: Jul 2004
Posts: 16
Reputation:
Solved Threads: 0
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 :
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 :
Problem:
this code has few problem.its not running properly.
thank you very much for helping me
shantuli
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)
public class linearsearch { public static void main (String[] args) { int [] Array = {20,55,10,02,100,02}; int target = 02; int location = -1; int i = 0; while ((Array[i] != target) && ( i<Array.length )) i++; if (Array[i] == target) location = i; System.out.println (i); } }
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)
class lec3 { public static void main (String[] args) { int [] array = {20,55,10,02,100,02}; for (int i=0;i<array.length; i++) { System.out.println(array[i]+"\n"); System.out.println("the location is "); } int target = 02; int location = -1; int first = 0; int last = 5; int h = 0; int index = 5; while((h>=1) && (index != 02)) { int middle = 2; if(2 = 02) { location = middle; System.out.println(middle); } else { if(target<middle) last = (middle - 1); else first = (middle + 1); } } if (middle == target) location = middle; System.out.println(middle); } }
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
>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:
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:
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.
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)
public class Main { public static void main ( String[] args ) { int[] array = {20,55,10,2,100,2}; int target = 2; for ( int i = 0; i < array.length; i++ ) { if ( array[i] == target ) System.out.println ( target + " found at index " + i ); } } }
Java Syntax (Toggle Plain Text)
public class Main { public static void main ( String[] args ) { int[] array = {2,2,10,20,55,100}; int target = 2; int left = 0; int right = array.length - 1; int mid = -1; while ( left <= right ) { mid = ( left + right ) / 2; if ( target == array[mid] ) break; else if ( target < array[mid] ) right = mid - 1; else left = mid + 1; } if ( mid != -1 ) System.out.println ( target + " found at index " + mid ); else System.out.println ( target + " not found" ); } }
I'm here to prove you wrong.
![]() |
Similar Threads
- Unknown Javascript error - cannot find what is wrong with my code. (JavaScript / DHTML / AJAX)
- Array C++ Help (C++)
- find the error (C++)
- Windows Update Error Code 80010108 (Windows Vista and Windows 7)
- Help fixing error with code (C++)
- Find out the problem of my code. (Java)
- Can't find the error (C)
- help me find the error (C++)
Other Threads in the Java Forum
- Previous Thread: java code problem
- Next Thread: Iterative and Recursive Demo
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) card class classes client code collision columns component constructor database designadrawingapplicationusingjavajslider draw eclipse error errors eventlistener exception expand fractal free game givemetehcodez graphics gui guidancer html ide image inetaddress integer integration intellij j2me java javadoc javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop machine map method methods migrate mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle plazmic print problem program project radio recursion scanner server set sharepoint smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads tree unlimited utility webservices windows






