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

>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:

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 );
    }
  }
}

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:

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" );
  }
}

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.