import java.util.Arrays;
public class ArraySearch {
public static int linearSearch(int[] arr, int x){
for (int i = 0; i < arr.length; i++) {
if(arr[i] == x){
return i;
}
}
return -1;
}
public static int binoSearch(int arr[], int x){
int start = 0;
int end = arr.length - 1;
while(start <= end){
int mid = (start + end) /2;
if(arr[mid] == x){
return mid;
}else if(arr[mid] < x){
start = mid + 1;
}else if(arr[mid] > x){
end = mid - 1;
}
}
return -1;
}
public static void guduSearch(int[] arr){
int temp;
for(int i = 0; i < arr.length; i ++){
for (int j = arr.length - 1; j > i; j--) {
if(arr[i] > arr[j]){
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = {1,2,5,6,7,9,11,16,25,28};
System.out.println(binoSearch(arr,9));
int[] arr1 = {12,32,55,16,75,9,11,16,25,28};
guduSearch(arr1);
System.out.println(Arrays.toString(arr1));
}
}
数据结构 简单的数组查找(二分法和线性查找法),另加了一个比较典型的冒泡排序法
最新推荐文章于 2024-09-11 02:02:22 发布