java資料結構和演算法------雜湊尋找

來源:互聯網
上載者:User

標籤:

 1 package iYou.neugle.search; 2  3 public class Hash_search { 4     private static int m = 13; 5     private static int[] hash = new int[m]; 6     private static int[] array = new int[] { 13, 25, 2, 60, 39, 52 }; 7  8     public static void main(String[] args) { 9         InsertHash();10         System.out.println("雜湊表建立如下:");11         System.out.print("[");12         for (int i = 0; i < hash.length; i++) {13             System.out.print(hash[i]);14             if (i != hash.length - 1) {15                 System.out.print(",");16             }17         }18         System.out.println("]");19         System.out.println("39在雜湊表中的位置為:" + SearchHash(39));20     }21 22     public static void InsertHash() {23         // 除法取餘法24         for (int i = 0; i < array.length; i++) {25             int value = array[i];26             int address = value % m;27             while (hash[address] != 0) {28                 address = (++address) % m;29             }30             hash[address] = value;31         }32     }33 34     public static int SearchHash(int key) {35         // 開放地址法36         int address = key % m;37         while (hash[address] != 0 && hash[address] != key) {38             address = (++address) % m;39         }40 41         if (hash[address] == 0) {42             return -1;43         }44         return address;45     }46 }

 

java資料結構和演算法------雜湊尋找

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.