赞
踩
遍历数组挨个查找
package org.westos.demo01; public class ArrayDemo{ public static void main(String[] args){ //定义一个数组 int[] arr = {10,20,70,10,90,100,1,2}; //根据元素查找该元素在数组中第一次出现的索引 int index = getIndexByElE(arr,ele:20); System.out.println("该元素第一卒在数组中出现的索引是:"+index); } private static int getIndexByElE(int i){ //遍历数组,去查找 for(int i = 0;i<arr.length;i++);{ if(ele==arr[i]){ return i; } } return-1;//如果我们没有找到这个元素那么就返回-1 } }
二分查找的前提:该数组的元素必须有序
二分查找的思想:每一次查找中间的元素,比较大小就能减少一半的元素
package org.westos.demo02; public class ArrayDemo{ public static void main(String[] args){ //二分查找:前提是数组元素必须优先,如果没有顺序,那么得使用基本查找 int[] arr = {10,20,30,40,50,60,70,80,90}; int index = getIndexByElE(arr,i:10); } private static int getIndexByElE(int i){ //定义最小索引,中间索引,最大索引 int minIndex = 0; int maxIndex = arr.length-1; int centerIndex = (minIndex+maxIndex)/2; //如果说你要找的这个元素,正好等于中间索引所对应的元素,那么就返回中间索引 if(ele==arr[centerIndex]){ return centerIndex; //如果你要找的元素大于中间索引对应的元素,你就移动最小索引 }else if(ele>arr[centerIndex]){ minIndex = centerIndex+1; }else if(ele<arr[centerIndex]){ //如果你要赵安迪这个元素小于中间的索引所对应的元素,你就移动最大索引 maxIndex = centerIndex-1; } retun -1;//如果没有找到,就返回-1 } }
package org.westos.demo03; public class ArrayDemo{ public static void main(String[] args){ //排序原理:数组元素两两比较,交换位置,大元素往后放,那么经过一轮比较后,最大元素,就会出现在最大索引处 int[] arr = {24,69,80,57,13};//待排序数组 //第一轮比较 for (int i = 0;i<arr.length-1;i++){ if(arr[i]>arr[i+1]){ //交互位置 int t = arr[i]; arr[i] = arr[i+1]; arr[i+1] = t; } } //第二轮比较 for (int i = 0;i<arr.length-1-1;i++){ if(arr[i]>arr[i+1]){ //交互位置 int t = arr[i]; arr[i] = arr[i+1]; arr[i+1] = t; } } //第三轮 for (int i = 0;i<arr.length-1-1-1;i++){ if(arr[i]>arr[i+1]){ //交互位置 int t = arr[i]; arr[i] = arr[i+1]; arr[i+1] = t; } } //第四轮 //第三轮 for (int i = 0;i<arr.length-1-1-1-1;i++){ if(arr[i]>arr[i+1]){ //交互位置 int t = arr[i]; arr[i] = arr[i+1]; arr[i+1] = t; } } /* for(int j = 0;j<arr.length-1;j++){ for(int i = 0;i<arr.length-1-j;i++){ if(arr[j]>arr[i+1]){ //交换位置 int t = arr[i]; arr[i] = arr[i+1]; arr[i+1] = t; } } } */ System.out.println(Arrays.toString(arr)); } }
package org.westos.demo03; public class ArrayDemo{ public static void main(String[] args){ // int[] arr = {24,69,80,57,13}; for(int i = 1;i<arr.length;i++){ if(arr[index]>arr[i]){ int t = arr[index]; arr[index] = arr[i]; arr[i] = t; } } //第二轮 index = 1; int[] arr = {24,69,80,57,13}; for(int i = 1+index;i<arr.length;i++){ if(arr[index]>arr[i]){ int t = arr[index]; arr[index] = arr[i]; arr[i] = t; } } //第三轮 index = 2; int[] arr = {24,69,80,57,13}; for(int i = 1+index;i<arr.length;i++){ if(arr[index]>arr[i]){ int t = arr[index]; arr[index] = arr[i]; arr[i] = t; } } //第四轮 index = 3; int[] arr = {24,69,80,57,13}; for(int i = 1+index;i<arr.length;i++){ if(arr[index]>arr[i]){ int t = arr[index]; arr[index] = arr[i]; arr[i] = t; } } /* for(int index = 0;index<arr.length-1;index++){ for(int i = 1+index;i<arr.length;i++){ if(arr[index]>arr[i]){ int t = arr[index]; arr[index] = arr[i]; arr[i] = t; } } */ System.out.println(Arrays.toString(arr)); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。