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 :
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 :
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);
}
}
Problem:
this code has few problem.its not running properly.
thank you very much for helping me
shantuli