排序数组中的线性搜索 - Java

Linear search in a sorted array - Java

我想制作一个程序,在排序数组中进行线性搜索,并可以输出找到搜索项的不同位置。目前我的程序只输出找到搜索项的第一个位置,所以这是我的程序现在所做的一个例子:

Enter number of elements
5
Enter 5 integers
1
3
3
9
15
Enter value to find
3
3 is present at location 2.

现在问题是 3 在位置 2 和 3 上,这就是我想在程序中编辑的内容,但我不知道该怎么做。

这是我的程序代码:

import java.util.Scanner;
 class LinearSearchArray1 {
    public static void main(String args[]){
        int c, n, search, array[];

        Scanner in = new Scanner(System.in);
        System.out.println("Enter number of elements");
        n = in.nextInt(); 
        array = new int[n];

        System.out.println("Enter " + n + " integers");

        for (c = 0; c < n; c++)
        array[c] = in.nextInt();

        System.out.println("Enter value to find");
        search = in.nextInt();

        for (c = 0; c < n; c++)
        {
            if (array[c] == search)     /* Searching element is present */
            {
             System.out.println(search + " is present at location " + (c + 1) + ".");
            break;
        }
    }
    if (c == n)  /* Searching element is absent */
        System.out.println(search + " is not present in array.");
    }
}
...
System.out.println("Enter value to find");
search = in.nextInt();

boolean exists = false;

for (c = 0; c < n; c++)
{
  if (array[c] == search)     /* Searching element is present */
  {
     System.out.println(search + " is present at location " + (c + 1) + ".");
     exists = true;
  }
}

if (!exists)  /* Searching element is absent */
  System.out.println(search + " is not present in array.");

您需要删除 break; 语句。否则,一旦找到第一个值,循环就会中断,并且永远不会到达下一个匹配项。

此代码可能适合您。

    int c = 0;

    while (c < array.length && array[c] < search)
    {
        cc++;
    }
    int first = c;
    while (c < array.length && array[c] == search) {
        c++;
    }
    int last = c;

现在索引 "first" 和 "last" 之间的元素(包括它们)包含搜索到的数字。所以你基本上搜索与你正在寻找的相同的第一个和最后一个元素。
将其写入控制台的示例:

    for (int i = first; i <= last; i++)
    {
        System.out.println(search + " is present at location " + (i + 1) + ".");
    }
import java.util.Scanner;

public class BinarySearch {
    /*
     * A binary search or half-interval search algorithm finds the position of a
     * specified value (the input "key") within a sorted array. Binary search
     * requires a sorted collection. Also, binary searching can only be applied
     * to a collection that allows random access (indexing).
     * 
     * Worst case performance: O(log n)
     * 
     * Best case performance: O(1)
     */

    public static int binSearch(int a[], int key) {
        int start = 0;
        int end = a.length - 1;

        while (start <= end) {
            int mid = (start + end) / 2;

            if (key == a[mid]) {
                return mid;
            }
            if (key < a[mid]) {
                end = mid - 1;
            } else {
                start = mid + 1;

            }

        }
        return -1;

    }

    public static void main(String arg[]) {

        BinarySearch bi = new BinarySearch();
        int[] arr = { 2, 4, 6, 8, 10, 12, 14, 16 };
        System.out.println("Please Key to be search");
        Scanner sc = new Scanner(System.in);
        int input = Integer.parseInt(sc.nextLine());

        if (bi.binSearch(arr, input) != -1) {
            System.out.println(input + ": " + " Search found at "
                    + bi.binSearch(arr, input) + " " + "Position");
        } else {
            System.out.println(input + ": " + " Search Result not found ");
        }

    }
}

使用ArrayList存储位置:-

进口java.util.ArrayList; 导入 java.util.Scanner;

class LinearSearchArray1 {

public static void main(String args[]){

    int c, n, search, array[];
    boolean searchStatus=false;

    ArrayList<Integer> al=new ArrayList<Integer>();

    Scanner in = new Scanner(System.in);
    System.out.println("Enter number of elements");
    n = in.nextInt(); 
    array = new int[n];
    System.out.println("Enter " + n + " integers");
    for (c = 0; c < n; c++)
        array[c] = in.nextInt();
    System.out.println("Enter value to find");
    search = in.nextInt();
    for (c = 0; c < n; c++)
    {
        if (array[c] == search)     /* Searching element is present */
        {
         al.add(c+1);
         searchStatus=true;  
    }
}
if (searchStatus==false)  /* Searching element is absent */
    System.out.println(search + " is not present in array.");
else {
    System.out.print(search+ " is present in array at location ");
    for(Integer i:al) 
        System.out.print(i+",");
}
}

}