التجزئة
مُقدَّم
15:07
أنواع البيانات المحددة من قبل المستخدم
سيخزن حقل النص العادي أي بيانات بلا رادع. اطلب نوع مركبة، وكتب شخص ما "موز" — سيقبل البرنامج ذلك دون اعتراض. ولكن إذا كنت…
سرد باللغة الإنجليزية · ترجمة نصية بالإنجليزية و中文 يمكنك التبديل بينهما
في هذا الدرس
| English | المعنى |
|---|---|
| key/kiː/ | مفتاح |
| collision-resolution strategy | A method for handling keys that produce the same hash address. |
| hash function/hæʃ ˈfʌŋkʃn/ | دالة التشفير |
| 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/ | عنوان |
| deterministic/dɪˌtɜːmɪˈnɪstɪk/ | الحتمي |
| 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/ | تصادف |
| 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ə/ | عامل التحميل |
| 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. |
إيجاد صف واحد من خمسين مليوناً دون النظر
- كاشير سوبر ماركت يمسح رمز شريطي ويظهر السعر قبل أن ترفع يدك عن المنتج. ملف المنتجات يحتوي على خمسين مليون سطر.
- لم يتم البحث عنهم جميعاً. ذهب رقم الرمز الشريطي عبر عملية حسابية قصيرة أنتجت موقعاً، وقارأ الكمبيوتر ذلك الموقع: قراءة واحدة، بدون مقارنات، نفس الوقت سواء كان الملف يحتوي على خمسين صفاً أو خمسين مليوناً.
- العملية الحسابية هي دالة تجعيد (Hash function)، والفكرة بأكملها تعتمد عليها: لا تخزن البيانات حيث ستسعدها، بل خزنها حيث يقول مفتاحها الخاص أنها تنتمي إليها.
- هذا الدرس يتعلق بخوارزميات التجعيد، وما يحدث عندما يريد مفتاحان نفس الشغرة، وكيف نبحث ونُدرج.
دالة التجعيد
- دالة التجعيد، أو خوارزمية التجعيد، تأخذ مفتاح السجل وتنتج العنوان الذي يُخزن فيه السجل.
- الجيدة منها سريعة، حتمية (نفس المفتاح يعطي دائماً نفس العنوان)، وتوزع المفاتيح بشكل متساوٍ عبر الشغرات المتاحة.
- بالنسبة لـ $N$ فترة، الثلاثة التي يتوقعها المنهج هي: القسمة المتبقية (modulo)،
address ← key MOD N؛ طي المفتاح (folding)، قسّم المفتاح إلى أجزاء، اجمعها، ثم خذ باقي القسمة على $N$؛ تشفير السلاسل النصية (string hash)، اجمع رموز الأحرف، ثم خذ باقي القسمة على $N$.
A hash function:
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
A hash function maps a key to an address, enabling near-instant direct lookup.
What makes a hash function a good one? Select جميعها that apply.
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Fast, deterministic and evenly spread. No realistic hash avoids collisions entirely, which is why every design includes a resolution strategy.
مثال محلول: تطبيق كل خوارزمية
- ملف يحتوي على 10 فترة مرقمة من 0 إلى 9. أين سيذهب المفتاح 4517؟
- القسمة المتبقية: $4517 \bmod 10 = 7$، إذن الفترة 7.
- الطي أزواجاً: $45 + 17 = 62$، ثم $62 \bmod 10 = 2$، إذن الفترة 2.
- وماذا عن المفتاح "CAB" باستخدام تشفير السلاسل النصية؟ $67 + 65 + 66 = 198$، ثم $198 \bmod 10 = 8$، إذن الفترة 8.
- اعرض العمليات الحسابية. العلامة تُمنح للحساب وليس رقم الفترة وحده.

Using the modulo hash address ← key MOD N with key = 27 and N = 10, what address is produced?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
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?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
45 + 17 = 62, and 62 MOD 10 = 2. The same key under a modulo hash would go to slot 7 instead.
التصادمات
- يحدث التصادم عندما يُشفّر مفتاحان مختلفان إلى نفس العنوان. مع أي دالة تشفير وأي ملف واقعي، التصادمات أمر حتمي، لذا فإن استراتيجية التعامل معها جزء من التصميم وليست تفكيراً لاحقاً.
- الفحص الخطي (Linear probing) يضع السجل في الفترة الحرة التالية، مع العودة للبداية عند نهاية الجدول. هو بسيط، لكن السجلات تتجمع: يزداد التكتل ويأخذ كل مفتاح يصل إليه وقتاً أطول.
- الربط (Chaining) يجعل كل فترة رأساً لـ قائمة مرتبطة بكل السجلات التي شُفرَت هناك. لا يوجد تجمع، لكنه يتطلب ذاكرة إضافية للروابط ومشي قصير عبر القائمة.
- إعادة التشفير (Rehashing) تطبق دالة تشفير ثانية لإيجاد فترة أخرى، مما يوزع المفاتيح بشكل أفضل على حساب زيادة الحسابات.

A collision occurs when:
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
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.
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Collisions are resolved by chaining or probing; keeping the load factor low keeps lookups near O(1).
البحث والإدراج
- للإدراج: شفر المفتاح. إذا كانت الفترة فارغة، اكتب السجل هناك. وإذا لم تكن كذلك، اتبع استراتيجية الحل: الفترة الحرة التالية للفحص الخطي، أو بداية قائمة تلك الفترة للربط.
- للبحث: شفر المفتاح واقرأ تلك الفترة. إذا تطابق المفتاح المخزن، تم العثور على السجل. وإذا لم يتطابق، اتبع نفس الاستراتيجية، حتى تتطابق المفاتيح أو تثبت فترة فارغة أن السجل غير موجود في الملف.
- كلا العمليتين تستخدمان نفس الاستراتيجية. لو توقف البحث عند أول عدم تطابق، لفوت كل سجل طُرد بسبب تصادم سابق.
مثال محلول: تتبع تصادم
- جدول مكون من 10 فترات يستخدم
key MOD 10مع الفحص الخطي. أدخل 23، 33، 43 بالترتيب، ثم ابحث عن 43. - 23 يشفر إلى 3؛ الفترة 3 فارغة، فتدخلها. 33 يشفر إلى 3؛ الفترة 3 مشغولة بـ 23، فالفحص الخطي يضعه في الفترة 4. 43 يشفر إلى 3؛ الفترتان 3 و4 مشغولتان، فتدخل الفترة 5.
- البحث عن 43: شفر إلى 3، اقرأ الفترة 3، المفتاح هو 23، ليس تطابقاً، فاستمر في الفحص؛ الفترة 4 تحتوي على 33، ليس تطابقاً؛ الفترة 5 تحتوي على 43، تم العثور عليه بعد ثلاث قراءات.
- هذا التتابع المتزايد المكون من ثلاثة هو التجمع الذي يسببه الفحص الخطي.
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.
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
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.
معامل التحميل
- معامل التحميل هو عدد السجلات مقسوماً على عدد الفترات. إنه الرقم الوحيد الذي يتنبأ بكفاءة أداء الجدول.
- تحت حوالي 70% يكون متوسط الاستعلام قريباً من قراءة واحدة. فوقها، تمتد تسلسلات الفحص بشدة وتتدهور الأداء نحو البحث الخطي.
- الحل هو تكبير الجدول وإعادة تشفير كل سجل فيه، ولهذا السبب يُصمم جدول التشفير بناءً على البيانات التي سيحملها مستقبلاً، وليس ما يحمله اليوم.

A 10-slot table uses key MOD 10 with linear probing. After inserting 23, 33 and 43 in that order, which slot holds 43?
الصعوبة 2/5 · 140 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
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.
تم تصحيح هذه البطاقة، لذلك تظهر باللغة الإنجليزية حتى يتم تحديث ترجمتها.
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:
الصعوبة 1/5 · 100 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
A lower load factor means fewer collisions, so lookups stay close to O(1).
تم تصحيح هذه البطاقة، لذلك تظهر باللغة الإنجليزية حتى يتم تحديث ترجمتها.
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
ما المشكلة؟ لا يرى ذلك سوى فريق الموقع.