在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
方法二:使用自定義邏輯
使用氣泡排序進行排序
演算法
步驟 1 − 首先,宣告並初始化一個名為“araylist”的陣列和一個名為“temp”的整數變數,用於臨時儲存移位的元素。
步驟 2 − 使用兩個 for 迴圈將第 i 個位置的元素與第 i+1 個元素進行比較。在第二個 for 迴圈內建立一個 if 塊,以檢查哪個元素更大,然後我們執行移位操作,以升序重新排列這些元素。
步驟 3 − 現在,使用 for each 迴圈列印排序後的陣列。
示例
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()”的內建方法或任何排序和搜尋演算法。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP