Java 排序和搜索元素
排序和搜索是我们可以对数组执行的基本操作。排序意味着按升序或降序重新排列给定列表或数组的元素,而搜索意味着在列表中查找元素或其索引。
尽管有各种算法可以执行这些操作,但在本文中,我们将使用其中几种来对Java中的元素进行排序和搜索。我们将逐个查看它们。
方法1:使用数组的内置方法
在本节中,我们将讨论以下方法,这些方法有助于对数组中的元素进行排序和搜索。
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 - 首先,声明和初始化一个名为“arraylist”的数组和一个名为“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 - 现在,创建一个循环,循环次数为数组的长度。在此循环中,使用一个if块来检查数组中是否存在“searchElem”。如果存在,将其索引存储在变量“locate”中,并将变量“isFound”递增为1。
-
步骤3 - 接下来,我们创建一个if-else块来检查变量“isFound”是否递增到1。如果等于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()’或者任何排序和搜索的算法。