首页  >  文章  >  Java  >  在Java中对元素进行排序和搜索

在Java中对元素进行排序和搜索

WBOY
WBOY转载
2023-08-30 20:09:101211浏览

在Java中对元素进行排序和搜索

排序和搜索是我们可以对数组执行的基本操作。排序意味着按升序或降序重新排列给定列表或数组的元素,而搜索意味着在列表中查找元素或其索引。

尽管有多种算法可用于执行这些操作,但在本文中,我们将使用其中的一些算法来对 java 中的元素进行排序和搜索。我们将一一研究它们。

方法一:使用数组的内置方法

在本节中,我们将讨论以下有助于对数组中的元素进行排序和搜索的方法。

sort() - 它是 Arrays 类的静态方法,按升序对作为参数传递的数组进行排序。

语法

Arrays.sort(nameOfarray);

binarySearch() - 它也是 Arrays 类的静态方法。它接受两个参数,第一个是需要搜索其元素的数组,第二个是我们需要在该数组中查找的元素。

它返回作为参数传递的元素的索引号。

语法

Arrays.binarySearch(nameOfarray, element);

示例

import java.util.*;
public class Srch {
   public static void main(String args[]) {
      int araylist[] = {9, 3, 56, 0, -2, -6, 2, 1, 80};
      System.out.print("The given unsorted list: ");
      // for each loop that prints the original array
      for (int print : araylist) {
         System.out.print(print + " ");
      } 
      Arrays.sort(araylist); 
      // method to sort given array
      System.out.println();
      System.out.print("The newly sorted list: ");
      // for each loop that prints the newly sorted array
      for (int print : araylist) {
         System.out.print(print + " ");
      } 
      System.out.println();
      // method to search given element
      int position = Arrays.binarySearch(araylist, 1);
      if(position > -1) {
         System.out.print("Element is available at index: " + position);
      } else {
         System.out.print("Element is not available");
      }
   }
} 

输出

The given unsorted list: 9 3 56 0 -2 -6 2 1 80 
The newly sorted list: -6 -2 0 1 2 3 9 56 80 
Element is available at index: 3

方法 2:使用我们的自定义逻辑

使用冒泡排序进行排序

算法

  • 步骤 1 - 首先,声明并初始化一个名为“araylist”的数组和一个名为“temp”的整数变量来临时存储移位的元素。

  • 步骤 2 - 使用两个 for 循环将第 i 个位置元素与第 i + 1 个元素进行比较。在第二个 for 循环内创建一个 if 块来检查哪个元素更大,然后我们执行移位操作以升序重新排列这些元素。

  • 第 3 步 - 现在使用每个循环,我们将打印排序后的数组。

示例

public class Bubble {
   public static void main(String[] args) {
      int araylist[] = {9, 3, 56, 0, 2, 1, 80};
      int temp = 0; 
      System.out.print("The given unsorted list: ");
      for (int print : araylist) {
         System.out.print(print + " ");
      }   
      for (int i = 0; i < araylist.length; i++) {     
         for (int j = i+1; j < araylist.length; j++) {     
            if(araylist[i] > araylist[j]) {    
               temp = araylist[i];    
               araylist[i] = araylist[j];    
               araylist[j] = temp;    
            }     
         }     
      }
      System.out.println();
      System.out.print("The newly sorted list: ");
      for (int print : araylist) {
         System.out.print(print + " ");
      }     
   }     
}

输出

The given unsorted list: 9 3 56 0 2 1 80 
The newly sorted list: 0 1 2 3 9 56 80 

使用线性搜索进行搜索

算法

  • 第 1 步 - 首先,声明并初始化一个名为“araylist”的数组和一个名为“searchElem”的整型变量,我们将在该数组中搜索该变量。我们还需要两个整数变量“isFound”和“locate”。

  • 第 2 步 - 现在,创建一个 for 循环,该循环将运行到数组的长度。在此循环中,使用 if 块检查数组中是否存在“searchElem”。如果可用,则将其索引存储在变量“locate”中,并将变量“isFound”增加到 1。

  • 第 3 步 - 接下来,我们创建一个 if else 块来检查变量“isFound”是否增加到 1。如果它等于 1,则表示找到了元素,我们将返回索引。如果不是,则将执行 else 块中的语句。

示例

public class Linear {
   public static void main(String[] args) {
      int araylist[] = {9, 3, 56, 0, 2, 1, 80};
      int searchElem = 0;
      int isFound = 0;
      int locate = 0;
      for(int i = 0; i < araylist.length; i++) {
         if(searchElem == araylist[i]) {
            isFound = 1;
            locate = i;
         } 
      }
      if(isFound == 1) {
         System.out.print("Element is available at index: " + locate);
      } else {
         System.out.print("Element is not available");
      }
   }
}

输出

Element is available at index: 3

结论

在本文中,我们讨论了如何对数组元素进行排序并执行搜索操作来查找该数组的特定元素。我们可以使用名为“sort()”的内置方法或任何排序和搜索算法。

以上是在Java中对元素进行排序和搜索的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:tutorialspoint.com。如有侵权,请联系admin@php.cn删除