核心:從0索引開始往后依次查找
在尚志等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供網(wǎng)站建設(shè)、網(wǎng)站設(shè)計 網(wǎng)站設(shè)計制作定制網(wǎng)站建設(shè),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站建設(shè),成都全網(wǎng)營銷推廣,成都外貿(mào)網(wǎng)站制作,尚志網(wǎng)站建設(shè)費用合理。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ū)域結(jié)束索引
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
以上學習內(nèi)容均來自于B站黑馬程序員
你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級服務(wù)器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
本文標題:JAVA學習第十四章——常見算法(一)-創(chuàng)新互聯(lián)
網(wǎng)頁路徑:http://jinyejixie.com/article42/dhdpec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機、域名注冊、Google、營銷型網(wǎng)站建設(shè)、動態(tài)網(wǎng)站、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容