(1)线性查找和二分搜索
来源:互联网 发布:mac没有任何来源 编辑:程序博客网 时间:2024/05/29 07:50
线性查找算法遍历数组,直到找到查询内容,其时间复杂度为O(n)。
public static int lineatSearch(int[] list,int key) { for(int i=0;i<list.length;i++) { if(key==list[i]) return i; } return -1; }
二分搜索算法的时间复杂度为O(logn).
public static int binarySearch(int[] list,int key) { int low=0; int high=list.length-1; while(high>=low){ int mid=(low+high)/2; if(key<list[mid]) high=mid-1; else if(key==list[mid]) return mid; else low=mid+1; } return -low-1; }/** Use binary search to find the key in the list */ public static<E extends Comparable<E>> int binarySearch(E[] list, E key) { int low = 0; int high = list.length - 1; while (high >= low) { int mid = (low + high) / 2; if (key.compareTo(list[mid]) < 0) { high = mid - 1; } else if (key.equals(list[mid])) { return mid; } else { low = mid + 1; } } return -low - 1; // Now high < low }
0 0
- (1)线性查找和二分搜索
- 搜索之线性搜索和二分搜索
- 数组的线性查找和二分查找
- Java-线性查找和二分查找
- 搜索算法-顺序查找和二分查找
- 线性查找,二分查找(C语言版)
- java查找值二分查找和线性查找
- 查找算法之线性查找和二分查找
- 二分查找/二分搜索(binary_search)详解
- 二分查找算法(java)二分搜索
- 搜索算法----二分查找(折半查找)
- 搜索算法-二分搜索(折半查找)
- C语言实现的线性查找和二分查找
- List<T>线性查找和二分查找BinarySearch效率分析
- Java — 线性查找和二分查找法
- 线性表的查找操作(线性表的顺序查找、二分查找、分块查找)
- 不光是查找值!“二分搜索”(1)
- Python 实现查找的几种类型 (线性查找,线性有序查找,线性查找最小值,二分查找)
- 【LatinIME】默认开启键盘按键声
- 八、Window
- 5-35计算N个分式有理数的平均值
- uva620 - Cellular Structure
- 炮弹攻击
- (1)线性查找和二分搜索
- spring异常:FileNotFoundException: Could not open ServletContext resource [/WEB-INF/XX-servlet.xml
- 原码,补码,反码详解
- 由主线程更新UI所想到的Window和RootViewImpl的创建过程
- hdu4311(排序)
- 203. Remove Linked List Elements
- xcoj 1203 F.铺设电缆
- (转)C# 温故而知新:Stream篇(三)
- ZOJ 3913 球缺