十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
核心:从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){ArrayListlist = 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站黑马程序员
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧