基本查找(顺序查找)
核心:从0索引开始往后依次查找
public static void main(String[] args) {
int[] arr = {131,127,181,191,158,147};
int number = 81;
System.out.println(basicSearch(arr,number));
}
public static boolean basicSearch(int[] arr,int number){
for (int i = 0; i < arr.length; i++) {
if(arr[i] == number){
return true;
}
}
return false;
}
顺序查找练习
需求:定义一个方法利用基本查找,查询某个元素在数组中的索引
不考虑数组中元素是否重复
//需求:定义一个方法利用基本查找,查询某个元素在数组中的索引
//不考虑数组中元素是否重复
int[] arr = {131,127,181,191,158,147};
int number = 127;
System.out.println(basicSearch(arr,number));
}
public static int basicSearch(int[] arr,int number){
for (int i = 0; i < arr.length; i++) {
if(arr[i] == number){
return i;
}
}
return -1;
}
需求:定义一个方法利用基本查找,查询某个元素在数组中的索引
需要考虑数组中元素是否重复
public static void main(String[] args) {
//需求:定义一个方法利用基本查找,查询某个元素在数组中的索引
//需要考虑数组中元素是否重复
int[] arr = {131,127,181,191,158,127};
int number = 127;
System.out.println(basicSearch(arr,number));
}
public static ArrayList basicSearch(int[] arr,int number){
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < arr.length; i++) {
if(arr[i] == number){
list.add(i);
}
}
return list;
}
二分查找/折半查找
前提条件:数组中的数据必须是有序的
核心:每次排除一半的查找范围
public static void main(String[] args) {
int[] arr = {1,2,3,4,5,6,7,8,9,10,11,12};
int number = 7;
System.out.println(binarySearch(arr,number));
}
public static int binarySearch(int[] arr, int number) {
int min = 0;
int max = arr.length - 1;
while (true) {
if (min > max) {
return -1;
}
int mid = (min + max) / 2;
if (arr[mid] > number) {
max = mid - 1;
} else if (arr[mid] < number) {
min = mid + 1;
}else {
return arr[mid];
}
}
}
二分查找改进
mid = min + {key - arr[min] / arr[max] - arr[min]}*(max - min)
分块查找
public static void main(String[] args) {
int[] arr = {16,5,9,12,21,18,
32,23,37,26,45,34,
50,48,61,52,73,66};
Block b1 = new Block(21,0,5);
Block b2 = new Block(45,6,11);
Block b3 = new Block(73,12,17);
Block[] blockArr = {b1,b2,b3};
int number = 37;
int index = getIndex(blockArr,arr,number);
//打印查找的索引
System.out.println(index);
}
private static int getIndex(Block[] blockArr, int[] arr, int number) {
//确定查找数据所在区域
int indexBlock = findIndexBlock(blockArr, number);
if(indexBlock == -1){
return -1;
}
int startIndex = blockArr[indexBlock].getStartIndex();
int endIndex = blockArr[indexBlock].getEndIndex();
//区域开始索引 区域结束索引
for (int i = startIndex; i < endIndex; i++) {
if(arr[i] == number){
return i;
}
}
return -1;
}
public static int findIndexBlock(Block[] blockArr,int number){
for (int i = 0; i < blockArr.length; i++) {
if(number <= blockArr[i].getMax()){
return i;
}
}
return -1;
}
分块练习
(27 22 30 40 36)
(13 19 16 20)
(7 10)
(43 50 48)
查找:48
public static void main(String[] args) {
int[] arr = {27, 22, 30, 40, 36, 13, 19, 16, 20, 7, 10, 43, 50, 48};
Date d1 = new Date(40, 22, 0, 4);
Date d2 = new Date(20, 13, 5, 8);
Date d3 = new Date(10, 7, 9, 10);
Date d4 = new Date(50, 43, 11, 13);
Date[] dateArr = {d1, d2, d3, d4};
int number = 48;
int index = getNumber(dateArr, arr, number);
System.out.println(index);
}
private static int getNumber(Date[] dateArr, int[] arr, int number) {
int index = findIndexDate(dateArr, number);
if (index == -1) {
return -1;
}
int startIndex = dateArr[index].getStrat();
int endIndex = dateArr[index].getEnd();
for (int i = startIndex; i <= endIndex; i++) {
if (arr[i] == number) {
return i;
}
}
return -1;
}
private static int findIndexDate(Date[] dateArr, int number) {
for (int i = 0; i < dateArr.length; i++) {
if (number >= dateArr[i].getMin() && number <= dateArr[i].getMax()) {
return i;
}
}
return -1;
}
冒泡排序
核心:两两比较
public static void main(String[] args) {
int[] arr = {2,4,5,3,1};
//执行次数
for (int j = 0; j < arr.length - 1; j++) {
//两两比较并交换
for (int i = 0; i < arr.length - 1; i++) {
if(arr[i] > arr[i + 1]){
int temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
选择排序
核心:从0索引开始,拿着每一个索引上的元素跟后面的元素以此比较,小的放前面,大的放后面,以此类推
public static void main(String[] args) {
int[] arr = {2, 4, 5, 3, 1};
//循环次数
for (int j = 0; j < arr.length - 1; j++) {
//比较交换
for (int i = j + 1; i < arr.length; i++) {
if (arr[j] > arr[i]) {
int temp = arr[j];
arr[j] = arr[i];
arr[i] = temp;
}
}
}
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
Tips
以上学习内容均来自于B站黑马程序员