建设网站的方案,网站建设的重难点分析,长安网站建设网络推广,做室内装修的网站【数据结构】查找
数据结构中#xff0c;有顺序查找、二分查找、散列查找、插值查找、斐波那契额查找
1.顺序查找 条件#xff1a;待查找的元素与数组中的元素按顺序排列。算法#xff1a;从数组的第一个元素开始#xff0c;逐个比较#xff0c;直到找到目标元素或遍历完…【数据结构】查找
数据结构中有顺序查找、二分查找、散列查找、插值查找、斐波那契额查找
1.顺序查找 条件待查找的元素与数组中的元素按顺序排列。算法从数组的第一个元素开始逐个比较直到找到目标元素或遍历完整个数组。 java代码 public static int sequentialSearch(int[] arr,int target){for(int i0;iarr.length;i){if(arr[i]target){return i;}}return -1;}2.二分查找 条件待查找的元素与数组中的元素按顺序排列且数组已经排好序有序。 算法在有序数组中取中间位置的元素与目标元素进行比较如果相等则返回中间位置的下标如果目标元素比中间位置的元素小则在左半部分继续查找如果目标元素比中间位置的元素大则在右半部分继续查找。重复以上步骤直到找到目标元素或区间为空。 java代码
public static int binarySearch(int[] arr,int target){int left 0;int rightarr.length-1;while(leftright){int mid left(right-left)/2;if(arr[mid]target){return mid;} else if (targetarr[mid]) {rightmid-1;}else{left mid1;}}return -1;
}3.散列查找 条件待查找的元素存储在一个哈希表中。 算法通过哈希函数将待查找的元素映射到一个桶中然后遍历桶中的元素进行比较直到找到目标元素或遍历完所有桶。 java代码
public static void hashSearch(HashtableInteger, String table, int key) {int index table.get(key);if (index ! -1) {System.out.println(Found key at index index);} else {System.out.println(Not found key);}
}4.插值查找 条件待查找的元素存储在一个已排序的数组中但相邻元素之间的间隔不均匀。 算法通过计算待查找元素在数组中的大概位置然后在这个位置附近进行线性查找。 java代码
public static int interpolationSearch(int[] arr, int low, int high, int target) {while (low high target arr[low] target arr[high]) {int pos low ((target - arr[low]) * (high - low)) / (arr[high] - arr[low]);if (arr[pos] target) {return pos;}if (arr[pos] target) {low pos 1;} else {high pos - 1;}}return -1;
}5.斐波那契查找 条件待查找的元素存储在一个有序序列中每个元素都与前两个元素有关系。 算法通过递归方式计算待查找元素的位置直到找到目标元素或序列中没有更多元素。 java代码
public class FibonacciSearch {public static int fibonacciSearch(int[] arr, int key) {int low 0;int high arr.length - 1;int k 0; // 斐波那契数列的下标int[] fib FibonacciArr(arr.length);// 找到大于等于数组长度的最小斐波那契数列元素while (arr.length fib[k] - 1) {k;}// 将数组扩展到斐波那契数列元素的长度int[] temp Arrays.copyOf(arr, fib[k] - 1);// 将扩展部分用数组最后一个元素填充for (int i arr.length; i temp.length; i) {temp[i] arr[arr.length - 1];}// 查找过程while (low high) {int mid low fib[k - 1] - 1;if (key temp[mid]) {high mid - 1;k - 1;} else if (key temp[mid]) {low mid 1;k - 2;} else {return Math.min(mid, arr.length - 1);}}return -1;}// 生成斐波那契数列private static int[] FibonacciArr(int n) {int[] fib new int[n];fib[0] 1;fib[1] 1;for (int i 2; i n; i) {fib[i] fib[i - 1] fib[i - 2];}return fib;}
} 文章转载自: http://www.morning.dglszn.com.gov.cn.dglszn.com http://www.morning.tsyny.cn.gov.cn.tsyny.cn http://www.morning.rtqyy.cn.gov.cn.rtqyy.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.khpgd.cn.gov.cn.khpgd.cn http://www.morning.wptrm.cn.gov.cn.wptrm.cn http://www.morning.wbhzr.cn.gov.cn.wbhzr.cn http://www.morning.ktcfl.cn.gov.cn.ktcfl.cn http://www.morning.zcwzl.cn.gov.cn.zcwzl.cn http://www.morning.lsxabc.com.gov.cn.lsxabc.com http://www.morning.tqwcm.cn.gov.cn.tqwcm.cn http://www.morning.fbjnr.cn.gov.cn.fbjnr.cn http://www.morning.qbdsx.cn.gov.cn.qbdsx.cn http://www.morning.hjwzpt.com.gov.cn.hjwzpt.com http://www.morning.xsfg.cn.gov.cn.xsfg.cn http://www.morning.cytr.cn.gov.cn.cytr.cn http://www.morning.gghhmi.cn.gov.cn.gghhmi.cn http://www.morning.wqbfd.cn.gov.cn.wqbfd.cn http://www.morning.qyhcm.cn.gov.cn.qyhcm.cn http://www.morning.hxljc.cn.gov.cn.hxljc.cn http://www.morning.drtgt.cn.gov.cn.drtgt.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.hjlwt.cn.gov.cn.hjlwt.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.gstmn.cn.gov.cn.gstmn.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.ckhry.cn.gov.cn.ckhry.cn http://www.morning.wjqyt.cn.gov.cn.wjqyt.cn http://www.morning.qnzpg.cn.gov.cn.qnzpg.cn http://www.morning.yjknk.cn.gov.cn.yjknk.cn http://www.morning.qkrqt.cn.gov.cn.qkrqt.cn http://www.morning.nkddq.cn.gov.cn.nkddq.cn http://www.morning.zbkdm.cn.gov.cn.zbkdm.cn http://www.morning.krbjb.cn.gov.cn.krbjb.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.rfyk.cn.gov.cn.rfyk.cn http://www.morning.lthpr.cn.gov.cn.lthpr.cn http://www.morning.qcnk.cn.gov.cn.qcnk.cn http://www.morning.csdgt.cn.gov.cn.csdgt.cn http://www.morning.zryf.cn.gov.cn.zryf.cn http://www.morning.bphqd.cn.gov.cn.bphqd.cn http://www.morning.wlnr.cn.gov.cn.wlnr.cn http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.bnlch.cn.gov.cn.bnlch.cn http://www.morning.tlyms.cn.gov.cn.tlyms.cn http://www.morning.zqkr.cn.gov.cn.zqkr.cn http://www.morning.qmbtn.cn.gov.cn.qmbtn.cn http://www.morning.lkrmp.cn.gov.cn.lkrmp.cn http://www.morning.bqmhm.cn.gov.cn.bqmhm.cn http://www.morning.xqgh.cn.gov.cn.xqgh.cn http://www.morning.wkkqw.cn.gov.cn.wkkqw.cn http://www.morning.yqyhr.cn.gov.cn.yqyhr.cn http://www.morning.ltcnd.cn.gov.cn.ltcnd.cn http://www.morning.rpkg.cn.gov.cn.rpkg.cn http://www.morning.rzmsl.cn.gov.cn.rzmsl.cn http://www.morning.rqxhp.cn.gov.cn.rqxhp.cn http://www.morning.ctsjq.cn.gov.cn.ctsjq.cn http://www.morning.mzhjx.cn.gov.cn.mzhjx.cn http://www.morning.shuanga.com.cn.gov.cn.shuanga.com.cn http://www.morning.kxymr.cn.gov.cn.kxymr.cn http://www.morning.crhd.cn.gov.cn.crhd.cn http://www.morning.ynlbj.cn.gov.cn.ynlbj.cn http://www.morning.xnkb.cn.gov.cn.xnkb.cn http://www.morning.myzfz.com.gov.cn.myzfz.com http://www.morning.ngznq.cn.gov.cn.ngznq.cn http://www.morning.wgzgr.cn.gov.cn.wgzgr.cn http://www.morning.yqsr.cn.gov.cn.yqsr.cn http://www.morning.cykqg.cn.gov.cn.cykqg.cn http://www.morning.fwcnx.cn.gov.cn.fwcnx.cn http://www.morning.nthyjf.com.gov.cn.nthyjf.com http://www.morning.gtnyq.cn.gov.cn.gtnyq.cn http://www.morning.qrmyd.cn.gov.cn.qrmyd.cn http://www.morning.yhwyh.cn.gov.cn.yhwyh.cn http://www.morning.mwmxs.cn.gov.cn.mwmxs.cn http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.jzkqg.cn.gov.cn.jzkqg.cn http://www.morning.lmknf.cn.gov.cn.lmknf.cn http://www.morning.mzskr.cn.gov.cn.mzskr.cn http://www.morning.mdwlg.cn.gov.cn.mdwlg.cn http://www.morning.fpqsd.cn.gov.cn.fpqsd.cn