我的“二分尋找演算法”實現

來源:互聯網
上載者:User

這是我的“二分尋找演算法”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 data21          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 data30         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 not48         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 substance66     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 substances79     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 substances81         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 }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.