核心:從0索引開始往后依次查找
在尚志等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供網站建設、網站設計 網站設計制作定制網站建設,公司網站建設,企業(yè)網站建設,品牌網站建設,成都全網營銷推廣,成都外貿網站制作,尚志網站建設費用合理。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;
}
需求:定義一個方法利用基本查找,查詢某個元素在數(shù)組中的索引
不考慮數(shù)組中元素是否重復
//需求:定義一個方法利用基本查找,查詢某個元素在數(shù)組中的索引
//不考慮數(shù)組中元素是否重復
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;
}
需求:定義一個方法利用基本查找,查詢某個元素在數(shù)組中的索引
需要考慮數(shù)組中元素是否重復
public static void main(String[] args) {//需求:定義一個方法利用基本查找,查詢某個元素在數(shù)組中的索引
//需要考慮數(shù)組中元素是否重復
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){ArrayListlist = new ArrayList<>();
for (int i = 0; i< arr.length; i++) {if(arr[i] == number){list.add(i);
}
}
return list;
}
前提條件:數(shù)組中的數(shù)據(jù)必須是有序的
核心:每次排除一半的查找范圍
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) {//確定查找數(shù)據(jù)所在區(qū)域
int indexBlock = findIndexBlock(blockArr, number);
if(indexBlock == -1){return -1;
}
int startIndex = blockArr[indexBlock].getStartIndex();
int endIndex = blockArr[indexBlock].getEndIndex();
//區(qū)域開始索引 區(qū)域結束索引
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};
//執(zhí)行次數(shù)
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};
//循環(huán)次數(shù)
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站黑馬程序員
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網查看詳情吧