我的“二分查找算法”实现

这是我的“二分查找算法”Java 实现!

View Code
 1 /*
 2  * 练习:二分查找算法
 3  * 功能:查找一堆数据中是否存在某个要查找的数据
 4  * 作者:陈沛锐
 5  * 时间:2013.03.25
 6  * caution:this class import the package part01.charter02 for create a string sorted data
 7  */
 8 package part01.chapter02;
 9 
10 import java.util.ArrayList;
11 import java.util.List;
12 import java.util.Scanner;
13 
14 public class _2exercise {
15 
16     public static void main(String[] args) {
17 //        int[] A = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
18 //        MERGE_SORT merge_sort = new MERGE_SORT();
19 //        merge_sort.sort(A, 0, 9);
20         // create data
21          System.out.println("Please inter a int number for datum counts:");
22          Scanner scanner = new Scanner(System.in);
23          int leng = scanner.nextInt();
24          CreateDate createDate = new CreateDate(leng);
25          int[] A = createDate.create();
26          //sort data by MERGE_SORT()
27          MERGE_SORT merge_sort = new MERGE_SORT();
28          merge_sort.sort(A, 0, leng - 1);
29         // output sorted data
30         System.out.println("the sorted data are follow:");
31         for (int i = 0; i < leng; i++) {
32             System.out.print(A[i] + " ");
33         }
34         System.out.println();
35         BinaryFind myBinaryFind = new BinaryFind();
36         System.out.println("enter the number you want to find:");
37         if (myBinaryFind.binaryFind(A, 0, A.length - 1, scanner.nextInt())) {
38             System.out.println("existence!");
39         } else {
40             System.out.println("inexistence!");
41         }
42     }
43 }
44 
45 class BinaryFind {
46     public boolean binaryFind(int[] A, int p, int r, int s) {// 0,9,3;5,9,3;
47         // flag sign existence or not
48         boolean flag = false;
49         if (A.length > 0 && p <= r) {
50             int q = (p + r);// 2;7;
51             if (A[q] == s) {// A[4]==5;8;
52                 flag = true;
53                 return flag;
54             } else {
55                 if (A[q] < s) {
56                     flag = binaryFind(A, q + 1, r, s);
57                 } else {
58                     flag = binaryFind(A, p, q - 1, s);// 5,9;
59                 }
60             }
61         }
62         return flag;
63     }
64 
65     // find number and return it's first substance
66     public int find_first(int[] A, int p, int r, int s) {
67         int sub = 0;
68         if (A.length > 0) {
69             for (int i = p; i < r + 1; i++) {
70                 if (A[i] == s) {
71                     sub = i;
72                 }
73             }
74         }
75         return sub;
76     }
77 
78     // find all same number and return all their substances
79     public List<Integer> find_all(int[] A, int p, int r, int s) {
80         // create a integer List<Integer> to store all same number's substances
81         List<Integer> subList = new ArrayList<Integer>();
82         if (A.length > 0) {
83             for (int i = p; i < r; i++) {
84                 if (A[i] == s) {
85                     subList.add(i);
86                 }
87             }
88         }
89         return subList;
90     }
91 }

 

posted @ 2013-04-27 10:58  iGeneral  阅读(197)  评论(0编辑  收藏  举报