This page needs a recent browser (with SharedArrayBuffer support). Please update Chrome, Edge, Firefox or Safari to the latest version. · تحتاج هذه الصفحة إلى متصفح حديث (يدعم SharedArrayBuffer). يرجى تحديث Chrome أو Edge أو Firefox أو Safari إلى أحدث إصدار.
English
Searching a list
- Searching means finding whether a value is in a list, and where.
- The usual answer is the index of the value, or
-1if it is missing. - Two classic methods are linear search and binary search.
العربية
البحث في قائمة
- البحث يعني إيجاد ما إذا كانت قيمة موجودة في قائمة، وأين تقع.
- الإجابة المعتادة هي الفهرس الخاص بالقيمة، أو
-1إذا كانت مفقودة. - طريقتان كلاسيكيتان هما البحث الخطي والبحث الثنائي.
English
Linear search
- Check each item in turn, from the start.
- Stop as soon as you find a match.
- It works on any list, sorted or not.
العربية
البحث الخطي
- تحقق من كل عنصر على التوالي، من البداية.
- توقف بمجرد العثور على تطابق.
- يعمل على أي قائمة، مرتبة أم غير مرتبة.
names = ["Sam", "Mia", "Leo"]
target = "Mia"
found = -1
for i in range(len(names)):
if names[i] == target:
found = i
break
print(found)
English
Binary search
- Binary search needs a sorted list.
- Look at the middle item. If it is the target, stop.
- If the target is smaller, search the left half; if bigger, the right half.
العربية
البحث الثنائي
- يحتاج البحث الثنائي إلى قائمة مرتبـة.
- انظر إلى العنصر الأوسط. إذا كان هو المستهدف، توقف.
- إذا كان المستهدف أصغر، ابحث في النصف الأيسر؛ إذا كان أكبر، في النصف الأيمن.
data = [2, 4, 6, 8, 10]
target = 8
low = 0
high = len(data) - 1
found = -1
while low <= high:
mid = (low + high) // 2
if data[mid] == target:
found = mid
break
elif data[mid] < target:
low = mid + 1
else:
high = mid - 1
print(found)
English
Compare them
- Linear search may check every item — slow for a long list.
- Binary search throws away half the list each step, so it is much faster.
- But binary search only works if the data is already sorted.
العربية
قارن بينهما
- قد يفحص البحث الخطي كل عنصر — بطيء للقوائم الطويلة.
- يتخلص البحث الثنائي من نصف القائمة في كل خطوة، لذا فهو أسرع بكثير.
- لكن البحث الثنائي يعمل فقط إذا كانت البيانات مرتبـة مسبقاً.
English
In Cambridge pseudocode
- Note
DIVis whole-number division (Python's//).
العربية
في الرمز الوهمي لكامبريدج
- لاحظ أن
DIVهو القسمة الصحيحة (في بايثون//).
// Linear search — stop at the first match
found ← -1
i ← 0
WHILE i < LENGTH(list) AND found = -1
IF list[i] = target THEN
found ← i
ENDIF
i ← i + 1
ENDWHILE
// Binary search (list must be sorted)
found ← -1
low ← 0
high ← LENGTH(list) - 1
WHILE low <= high AND found = -1
mid ← (low + high) DIV 2
IF list[mid] = target THEN
found ← mid
ELSE
IF list[mid] < target THEN
low ← mid + 1
ELSE
high ← mid - 1
ENDIF
ENDIF
ENDWHILE
English
Common mistakes
- Binary search needs a sorted list; it halves the range each step.
- Linear search works on any list but is slower.
العربية
أخطاء شائعة
- يحتاج البحث الثنائي إلى قائمة مرتبـة؛ ينصف النطاق في كل خطوة.
- يعمل البحث الخطي على أي قائمة لكنه أبطأ.
English
Now you try
- Return the index of the value, or
-1when it is not found. - Press Check answer to test your code.
العربية
الآن جرب بنفسك
- أعد الفهرس الخاص بالقيمة، أو
-1عند عدم العثور عليها. - اضغط على تحقق من الإجابة لاختبار الكود الخاص بك.
Explore · استكشف
Linear vs binary search
Binary search halves the list each step — far fewer comparisons.
Write linear_search(items, target) that returns the index of target in items, or -1 if it is not there. Check the items one by one.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write binary_search(items, target) for a sorted list. Return the index of target, or -1 if it is missing. Halve the range each step.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Write first_negative(items) that scans the list and returns the index of the first number less than 0, or -1 if there are none.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.