java实现二分查找

思路:

  1.先使用经典算法之快速排序.

   2.使用二分查找查找目标数据

代码1:(Quick_Sort.java)

 1 package com.cn.algorithm_arithmetic算法;
 2 /**
 3  * 本程序记录了经典排序算法之快排
 4  * 时间复杂度:一般O(nlogn),最差O(n^2)
 5  * @author Administrator
 6  *
 7  */
 8 
 9 public class Quick_Sort {
10     public static int partition(int a[],int low,int hight){
11         int temp = a[low];
12         while (low<hight){
13             while (low<hight && a[hight]>=temp){
14                 hight--;
15             }
16             a[low] = a[hight];
17             while (low<hight && a[low]<=temp){
18                 low++;
19             }
20             a[hight] = a[low];
21             a[low] = temp;
22         }
23         return low;
24     }
25     public static void quick_sort(int a[],int low,int hight){
26         if (low >= hight){
27             return;
28         }
29         int mid = partition(a, low, hight);
30         quick_sort(a,low,mid-1);
31         quick_sort(a, mid+1, hight);
32     }
33     public static int[] get_arr(int size){
34         int a[] = new int[size];
35         for (int i = 0; i < a.length; i++) {
36             a[i] = (int)(Math.random()*1000);
37         }
38         return a;
39     }
40     public static void main(String[] args) {
41         System.out.println("---------快速排序----------");
42         int b[] = new int[10];
43         System.out.print("排序前:");
44         for (int i = 0; i < b.length; i++) {
45             b[i] = (int)(Math.random()*1000);
46             System.out.print(b[i]+"  ");
47         }
48 //        long start_time = System.currentTimeMillis();
49         System.out.print("
排序后:");
50         quick_sort(b, 0, 9);
51 //        long end_time = System.currentTimeMillis();
52         for (int i = 0; i < b.length; i++) {
53             System.out.print(b[i]+"  ");
54         }
55 //        System.out.println("
排序总耗时:"+(end_time - start_time));
56     }
57 }
View Code

代码2:(Two_Devide_Find.java)

 1 package com.cn.algorithm_arithmetic算法;
 2 /**
 3  * 二分查找来查找指定的数值
 4  * @author Administrator
 5  *
 6  */
 7 public class Two_Devide_Find {
 8 public static String find_target(int a[],int key){
 9     int start = 0;
10     int end = a.length - 1;
11     while (start <= end){
12         int mid = (start + end)/2;
13         if (a[mid] == key){
14             return mid+"";
15         }else if (a[mid] > key){
16             end = mid - 1;
17         }else if (a[mid] < key){
18             start = mid + 1;
19         }
20     }
21     return null;
22 }
23 public static void main(String[] args) {
24     int key = 188;
25     int a[] = Quick_Sort.get_arr(1000);
26     Quick_Sort.quick_sort(a, 0, 999);
27     System.out.print("原始数据:");
28     for (int i = 0; i < a.length; i++) {
29         if (i%10 == 0){
30             System.out.println("
");
31         }
32         System.out.print(a[i]+"  ");
33     }
34     String index = find_target(a, key);
35     System.out.println("
结果位:"+ (index==null?"未找到目标数字":index));
36 }
37 }
View Code
原文地址:https://www.cnblogs.com/g177w/p/8342926.html