Java 程式,遞迴線性搜尋陣列中的元素


在本文中,我們將瞭解如何遞迴線性搜尋陣列中的元素。線性搜尋是一種非常簡單的搜尋演算法,它對所有專案逐個執行順序搜尋。

以下是對相同內容的演示示例 −

假設我們的輸入是

Input array:
14 20 35 47 50 65 72 81 90 99

Key element: 72

期望的輸出是

The element 72 is present at position: 6

演算法

Step 1 - START
Step 2 - Declare a string array namely input_array, two integer namely key_element and index
Step 3 - Define the values.
Step 4 - Iterate through the array.
Step 5 - Define the element to be searched. Invoke the recursive method by passing these parameters.
Step 6 - Define an ‘if’ condition with the condition that a failure would return -1, otherwise the position of the element itself.
Step 7 - Display this on the console.
Step 8 - Stop

示例 1

在這裡,我們展示對整數的線性搜尋。

public class LinearSearch {
   static int recSearch(int input_array[], int l, int r, int key_element) {
      if (r < l)
         return -1;
      if (input_array[l] == key_element)
         return l;
      if (input_array[r] == key_element)
         return r;
      return recSearch(input_array, l+1, r-1, key_element);
   }
   public static void main(String[] args) {
      int input_array[] = {14, 20, 35, 47, 50, 65, 72, 81, 90, 99};
      System.out.println("The elements of the array is defined as ");
      for (int i : input_array) {
         System.out.print(i +" ");
      }
      int key_element = 72;
      System.out.println("\n\nThe elements to be searched in the array is: " + key_element);
      int index = recSearch(input_array, 0, input_array.length-1, key_element);
      if (index != -1)
         System.out.println("\nThe element " + key_element + " is present at position: " + index);
      else
         System.out.println("Element " + key_element + " is not present: ");
   }
}

輸出

The elements of the array is defined as
14 20 35 47 50 65 72 81 90 99

The elements to be searched in the array is: 72

The element 72 is present at position: 6

示例 2

在這裡,我們展示對字串的線性搜尋。

public class Demo {
   static int recSearch(String input_array[], int l, int r, String key_element) {
      if (r < l)
         return -1;
      if (input_array[l] == key_element)
         return l;
      if (input_array[r] == key_element)
         return r;
      return recSearch(input_array, l+1, r-1, key_element);
   }
   public static void main(String[] args) {
      String input_array[] = { "Scala", "Java", "Python", "Mysql"};
      System.out.println("The elements of the array is defined as ");
      for (String i : input_array) {
         System.out.print(i +" ");
      }
      String key_element = "Java";
      System.out.println("\n\nThe elements to be searched in the array is: " + key_element);
      int index = recSearch(input_array, 0, input_array.length-1, key_element);
      if (index != -1)
         System.out.println("\nThe element " + key_element + " is present at position: " + index);
      else
         System.out.println("Element " + key_element + " is not present: ");
   }
}

輸出

The elements of the array is defined as
Scala Java Python Mysql

The elements to be searched in the array is: Java

The element Java is present at position: 1

更新於: 2022-03-29

3K+ 瀏覽量

開啟你的 職業生涯

完成該教程來獲得認證

開始學習
廣告
© . All rights reserved.