标签:复杂度 有序 查找 sys seq while return logs ret
一、折半查找的前提是线性表中的记录必须好似关键码有序,线性表必须是顺序存储的。
二、折半查找
1 public class Binary_SearchDemo01 { 2 static int[] num = {0,1,3,5}; 3 public static void main(String[] args) { 4 int key = 3; 5 int x = Sequential_Search(key); 6 System.out.println(num[x]); 7 } 8 private static int Sequential_Search(int key) { 9 int low,mid,high; 10 low = 1; 11 high = num.length-1; 12 while(low <= high){ 13 mid = (low + high)/2; 14 if(key<num[mid]){ 15 high = mid-1; 16 } 17 else if(key>num[mid]){ 18 low = mid+1; 19 }else{ 20 return mid; 21 } 22 } 23 return 0; 24 } 25 }
折半查找的时间复杂度为O(logn);
标签:复杂度 有序 查找 sys seq while return logs ret
原文地址:http://www.cnblogs.com/XuGuobao/p/7287696.html