Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Java: Общие вопросы > проблема с последовательным поиском


Автор: ArniLand 16.11.2010, 21:21
При поиске значений в 3 массиве, для примера задаю размерность массива 100, поиск значения задаю 4, при результате выдает 2. Не могу понять в чем ошибка. Значения генерируются генератором случайных чисел. Вот код программы и результат выполнения. package laba5 ;
Код

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.io.*;
import java.util.*;
/**
 *
 * @author Anton
 */
public class Main {


    public static void main(String[] args) {

        System.out.println("Enter array size: ");
        Scanner in = new Scanner(System.in);
        int M = in.nextInt();
         System.out.println("Enter value to search");
         int p = in.nextInt();

           int[] a = new int[M];

        Random r = new Random(100);

        for (int i = 0; i < M; i++)
            a[i] =  r.nextInt(100);

        Arrays.sort(a); 

linearSearch(a,p); 
int z= a.length;
 long begin = System.nanoTime();
binarySearch(a,p, 0, z);
if(binarySearch(a,p, 0, z) != -1)
{
    System.out.println("\n matches found by binary searching");
}
else System.out.println("\n no matches found");
long v = (System.nanoTime()- begin);
System.out.println("\n Elapsed time is "+v+" nanoseconds");
System.out.println("Enter second value");
p=in.nextInt();
int[] b = new int[p];
 for (int i = 0; i < p; i++)
            b[i] =  r.nextInt(100);
System.out.println("Enter value to search");
         int t = in.nextInt();

        Arrays.sort(b); 
linearSearch(b,t); 
int x= b.length;
 long egin = System.nanoTime();
binarySearch(b,t, 0, x);
if(binarySearch(b,t, 0, x) != -1)
{
    System.out.println("\n matches found by binary searching");
}
else System.out.println("\n no matches found");
long m = (System.nanoTime()- egin);
System.out.println("\n Elapsed time is "+m+" nanoseconds");
System.out.println("Enter the value of array 3");

p=in.nextInt();
int[] c = new int[p];
 for (int i = 0; i < p; i++)
            c[i] =  r.nextInt(100);
System.out.println("Enter value to search");
         int k = in.nextInt();

        Arrays.sort(b);
linearSearch(b,t); 
int y= c.length;
 long begin3 = System.nanoTime();
binarySearch(b,k, 0, y);
if(binarySearch(b,k, 0, y) != -1)
{
    System.out.println("\n matches found by binary searching");
}
else System.out.println("\n no matches found");
long l = (System.nanoTime()- begin3);
System.out.println("\n Elapsed time is "+l+" nanoseconds");


    }


   public static int binarySearch(int[] array, int value, int left, int right) {

      if (left > right)

            return -1;

      int middle = (left + right) / 2;

      if (array[middle] == value) {

            return middle;


      }


      else if (array[middle] > value)

            return binarySearch(array, value, left, middle - 1);

      else

            return binarySearch(array, value, middle + 1, right);

}
    public static void linearSearch(int array1[],  int key) {

        long begin = System.nanoTime();
        int q=0;
        for (int i = 0; i < array1.length; i++) {
        if (key == array1[i]) {
           System.out.println("\n value "+key+" founded at"+ i + "position by by linear searching");
           q++;

        }

    }
             if (q==0)
            System.out.println("\n No matches founded");
          long p = (System.nanoTime()- begin);
            System.out.println("\n Elapsed time is "+p+" nanoseconds");

    }

}


Результат программы:
run:
Enter array size: 
1000
Enter value to search
3

 value 3 founded at34position by by linear searching

 value 3 founded at35position by by linear searching

 value 3 founded at36position by by linear searching

 value 3 founded at37position by by linear searching

 value 3 founded at38position by by linear searching

 value 3 founded at39position by by linear searching

 value 3 founded at40position by by linear searching

 value 3 founded at41position by by linear searching

 Elapsed time is 1149901 nanoseconds

 matches found by binary searching

 Elapsed time is 76990 nanoseconds
Enter second value
75
Enter value to search
2

 value 2 founded at1position by by linear searching

 value 2 founded at2position by by linear searching

 value 2 founded at3position by by linear searching

 Elapsed time is 337106 nanoseconds

 matches found by binary searching

 Elapsed time is 63792 nanoseconds
Enter the value of array 3
100
Enter value to search
4

 value 2 founded at1position by by linear searching

 value 2 founded at2position by by linear searching

 value 2 founded at3position by by linear searching

 Elapsed time is 424545 nanoseconds

 matches found by binary searching

 Elapsed time is 63792 nanoseconds
BUILD SUCCESSFUL (total time: 29 seconds)

Автор: _Y_ 16.11.2010, 23:32
Встроке 70 ввод производится в переменную k, а строка 73 передает методу поиска величину t

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)