Hashing
Diperkenalkan
15:07
Jenis Data Buatan Pengguna
Medan string biasa akan dengan senang hati menyimpan hal yang tidak masuk akal. Minta jenis kendaraan, lalu seseorang mengetik Pisang — program menerimanya tanpa protes. Namun jika Anda…
Narasi bahasa Inggris · takarir bahasa Inggris dan 中文 yang dapat dipilih
Dalam pelajaran ini
| English | Makna |
|---|---|
| key/kiː/ | kunci |
| collision-resolution strategy | A method for handling keys that produce the same hash address. |
| hash function/hæʃ ˈfʌŋkʃn/ | fungsi pencacah |
| hashing algorithms | Methods that calculate a storage address from a record's key. |
| slot/slɒt/ | A position in a hash table where a record can be stored. |
| search/sɜːtʃ/ | To look through data for an item that matches a given value. |
| insert/ˈɪnsɜːt/ | To add a new item to a data structure, file or table. |
| address/əˈdres/ | alamat |
| deterministic/dɪˌtɜːmɪˈnɪstɪk/ | deterministik |
| modulo/məˈdjʊləʊ/ | A method that uses the remainder after division to find an address. |
| folding | A method that splits a key into parts and adds them before using modulo. |
| string hash | A method that adds character codes and uses modulo to find an address. |
| character codes | Numbers used to represent characters in a computer. |
| collision/kəˈlɪʒn/ | tabrakan |
| hash tables | Data structures that use keys to calculate where records are stored. |
| Linear probing/ˈlɪnɪə ˈprəʊbɪŋ/ | A method that checks slots one after another until it finds a free slot. |
| cluster/ˈklʌstə/ | To form a group of records in slots next to each other. |
| Chaining/ˈtʃeɪnɪŋ/ | A method that keeps records with the same hash address in a linked list. |
| linked list/lɪŋkt lɪst/ | A sequence of nodes connected by references or pointers; the nodes need not occupy adjacent memory locations. |
| clustering/ˈklʌstərɪŋ/ | The forming of groups of records in slots next to each other. |
| pointers/ˈpɔɪntəz/ | A value that identifies a memory location or refers to another stored object. |
| Rehashing | A method that uses a second hash function to find another slot. |
| load factor/ləʊd ˈfæktə/ | faktor beban |
| probe sequences | The slots checked, in order, when searching for or inserting a record. |
| linear search/ˈlɪnɪə sɜːtʃ/ | Checking items in sequence until the required item is found or the list ends. |
Menemukan satu baris dalam lima puluh juta tanpa mencari
- Kasir supermarket memindai barcode dan harga muncul sebelum tangan Anda meninggalkan barang. File produk memegang lima puluh juta baris.
- Tidak ada yang mencarinya. Nomor barcode melewati perhitungan singkat yang menghasilkan posisi, dan komputer membaca posisi itu: satu baca, tanpa perbandingan, waktu yang sama apakah file berisi lima puluh baris atau lima puluh juta.
- Perhitungan itu adalah fungsi hash, dan seluruh ide bergantung padanya: jangan simpan data di tempat yang muat, simpan di tempat kunci miliknya sendiri mengatakan ia berada.
- Pelajaran ini tentang algoritma hashing, apa yang terjadi ketika dua kunci menginginkan slot yang sama, dan bagaimana cara mencari dan menyisipkan.
Fungsi hash
- Fungsi hash, atau algoritma hashing, mengambil kunci rekaman dan menghasilkan alamat di mana rekaman disimpan.
- Yang bagus itu cepat, deterministik (kunci yang sama selalu menghasilkan alamat yang sama), dan menyebarkan kunci merata di seluruh slot yang tersedia.
- Untuk $N$ slot, tiga hal yang diharapkan oleh silabus adalah: modulo,
address ← key MOD N; folding, pisahkan kunci menjadi bagian-bagian, jumlahkan, lalu MOD $N$; string hash, jumlahkan kode karakter, lalu MOD $N$.
A hash function:
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
A hash function maps a key to an address, enabling near-instant direct lookup.
What makes a hash function a good one? Select semua that apply.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Fast, deterministic and evenly spread. No realistic hash avoids collisions entirely, which is why every design includes a resolution strategy.
Contoh terpecah: terapkan setiap algoritma
- Sebuah file memiliki 10 slot, bernomor 0 hingga 9. Di mana kunci 4517 akan masuk?
- Modulo: $4517 \bmod 10 = 7$, jadi slot 7.
- Folding berpasangan: $45 + 17 = 62$, kemudian $62 \bmod 10 = 2$, jadi slot 2.
- Dan kunci "CAB" melalui string hash? $67 + 65 + 66 = 198$, kemudian $198 \bmod 10 = 8$, jadi slot 8.
- Tunjukkan perhitungannya. Nilai diberikan untuk perhitungan tersebut, bukan hanya nomor slotnya.

Using the modulo hash address ← key MOD N with key = 27 and N = 10, what address is produced?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
27 MOD 10 = 7 (the remainder when 27 is divided by 10).
A table has 10 slots. Using folding in pairs on key 4517 (add 45 and 17, then MOD 10), which slot does it go to?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
45 + 17 = 62, and 62 MOD 10 = 2. The same key under a modulo hash would go to slot 7 instead.
Tabrakan
- Tabrakan terjadi ketika dua kunci berbeda dihash ke alamat yang sama. Dengan fungsi hash apa pun dan file yang realistis, tabrakan pasti terjadi, sehingga strategi menanganinya merupakan bagian dari desain, bukan pertimbangan terakhir.
- Linear probing menempatkan rekaman di slot kosong berikutnya, melingkar kembali ke awal di akhir tabel. Sederhana, tetapi rekaman menumpuk: area penuh membesar dan setiap kunci yang mendarat di dalamnya membutuhkan waktu lebih lama.
- Chaining menjadikan setiap slot sebagai kepala dari daftar taut (linked list) dari semua rekaman yang dihash ke sana. Tidak ada penumpukan, tetapi memerlukan memori tambahan untuk pointer dan perjalanan singkat sepanjang daftar.
- Rehashing menerapkan fungsi hash kedua untuk menemukan slot lain, menyebarkan kunci dengan lebih baik dengan mengorbankan lebih banyak komputasi.

A collision occurs when:
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
Two keys mapping to the same slot is a collision; it must be resolved by probing, chaining or rehashing.
Match each collision-handling idea to what it does.
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
Collisions are resolved by chaining or probing; keeping the load factor low keeps lookups near O(1).
Pencarian dan penyisipan
- Untuk menyisipkan: hash kunci. Jika slot kosong, tulis rekaman di sana. Jika tidak, ikuti strategi penyelesaian, slot kosong berikutnya untuk linear probing, atau depan daftar slot tersebut untuk chaining.
- Untuk mencari: hash kunci dan baca slot itu. Jika kunci yang tersimpan cocok, rekaman ditemukan. Jika tidak, ikuti strategi yang sama, hingga kunci cocok atau slot kosong membuktikan rekaman tidak ada dalam file.
- Kedua operasi menggunakan strategi yang sama. Pencarian yang berhenti pada ketidakcocokan pertama akan melewatkan setiap rekaman yang pernah digeser oleh tabrakan.
Contoh terpecah: telusuri tabrakan
- Sebuah tabel 10 slot menggunakan
key MOD 10dengan linear probing. Sisipkan 23, 33, 43 secara berurutan, lalu cari 43. - 23 dihash ke 3; slot 3 kosong, jadi masuk ke sana. 33 dihash ke 3; slot 3 sudah diisi oleh 23, jadi linear probing memasukkannya ke slot 4. 43 dihash ke 3; slot 3 dan 4 sudah terisi, jadi masuk ke slot 5.
- Mencari 43: hash ke 3, baca slot 3, kuncinya 23, tidak cocok, jadi lanjutkan probe; slot 4 berisi 33, tidak cocok; slot 5 berisi 43, ditemukan, setelah tiga kali baca.
- Rangkaian tiga yang membesar itu adalah penumpukan yang disebabkan oleh linear probing.
Hash each key straight to a bucket
A hash function turns a key into a bucket number, so you jump straight to the record instead of searching. When two keys land in the same bucket that is a collision — they chain together in that bucket.
When searching a hash table, the record is not in the file as soon as the first slot read holds a different key.
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
The record may have been displaced by a collision. The search follows the same resolution strategy until a match is found or the strategy proves the key absent.
Faktor beban
- Faktor beban adalah jumlah rekaman dibagi dengan jumlah slot. Ini adalah satu angka yang memprediksi seberapa baik tabel berkinerja.
- Di bawah sekitar 70% rata-rata pencarian mendekati satu kali baca. Di atasnya, urutan probe memanjang tajam dan kinerja menurun menuju pencarian linier.
- Solusinya adalah membuat tabel lebih besar dan melakukan rehashing pada setiap rekaman ke dalamnya, itulah sebabnya tabel hash dibuat berdasarkan data yang akan ditampungnya, bukan data yang dimilikinya hari ini.

A 10-slot table uses key MOD 10 with linear probing. After inserting 23, 33 and 43 in that order, which slot holds 43?
Kesulitan 2/5 · 140 koin + bonus kecil pada penyelesaian pertama hari ini
All three hash to 3. 23 takes slot 3, 33 is probed to 4, and 43 to 5. Three keys in a row is exactly the clustering linear probing causes.
Kartu ini telah diperbaiki, jadi ditampilkan dalam bahasa Inggris sampai terjemahannya diperbarui.
Marks that slip away
- A collision is two keys, one address. It is not an error and not a lost record; it is the normal case a strategy handles.
- A search must follow the same resolution strategy as the insert, stopping on a match or when that strategy proves the key absent.
- Linear probing clusters 聚集; chaining costs memory. Give the trade-off, not just the mechanism.
- The load factor is records divided by slots, and the threshold is about 70%, not 100%.
To keep hash lookups fast, the load factor (records ÷ slots) should be kept:
Kesulitan 1/5 · 100 koin + bonus kecil pada penyelesaian pertama hari ini
A lower load factor means fewer collisions, so lookups stay close to O(1).
Kartu ini telah diperbaiki, jadi ditampilkan dalam bahasa Inggris sampai terjemahannya diperbarui.
You've got it
- a hash function turns a key into an address: fast, deterministic, evenly spread; modulo, folding and string hashes are the three to know
- a collision is two keys hashing to one address, resolved by linear probing 线性探测 (next free slot, clusters), chaining (a linked list per slot, more memory) or rehashing
- insert and search both follow the same strategy; a search ends on a match or when that strategy proves the key absent
- keep the load factor, records divided by slots, below about 70% for near one-read lookups
Apa yang salah? Hanya tim situs yang dapat melihatnya.