Searching: linear and binary · البحث: الخطي والثنائي
Two ways to search
- Searching means finding where a value is in an array (or saying it is not there).
- Linear search checks every item one by one. It works on any array.
- Binary search is much faster but needs the array to be sorted first.
طريقتان للبحث
- البحث يعني إيجاد أين تكون قيمة في مصفوفة (أو القول بأنها غير موجودة).
- البحث الخطي يفحص كل عنصر واحدًا تلو الآخر. يعمل على أي مصفوفة.
- البحث الثنائي أسرع بكثير لكنه يحتاج إلى أن تكون المصفوفة مرتبة أولاً.
Linear search
- Walk from index
0ton - 1, comparing each item to the target. - Return the index the moment you find it. If you reach the end, return
-1. - For an array of
nitems, this looks at up tonof them.
البحث الخطي
- تجول من الفهرس
0إلىn - 1، مقارنًا كل عنصر بالهدف. - رُجع الفهرس لحظة العثور عليه. إذا وصلت للنهاية، رُجع
-1. - لمصفوفة من
nعناصر، هذا يفحص حتىnمنها.
Why sorting enables binary search
- If the array is sorted, you can jump to the middle and compare.
- If the middle is too small, the target must be in the right half; if too big, the left half.
- Each step throws away half the array, so it is very fast (about
log2(n)steps).
لماذا الترتيب يمكّن البحث الثنائي
- إذا كانت المصفوفة مرتبة، يمكنك القفز إلى الوسط والمقارنة.
- إذا كان الوسط صغيرًا جدًا، يجب أن يكون الهدف في النصف الأيمن؛ إذا كبيرًا جدًا، في النصف الأيسر.
- كل خطوة تتخلص من نصف المصفوفة، لذا فهي سريعة جدًا (حوالي
log2(n)خطوات).
low, high, mid
- Keep two bounds:
low(start) andhigh(end). The middle ismid = low + (high - low) / 2. - If
a[mid]is the target, returnmid. Ifa[mid] < target, movelow = mid + 1; elsehigh = mid - 1. - Stop when
low > high— the target is not there, so return-1.
low, high, mid
- احتفظ بحدَين:
low(البداية) وhigh(النهاية). الوسط هوmid = low + (high - low) / 2. - إذا كان
a[mid]هو الهدف، رُجعmid. إذا كانa[mid] < target، حرّكlow = mid + 1؛ وإلاhigh = mid - 1. - توقف عندما
low > high— الهدف غير موجود، لذا رُجع-1.
#include <stdio.h>
int main(void) {
int a[] = {1, 3, 5, 7, 9}; // sorted!
int target = 7, lo = 0, hi = 4, found = -1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] == target) { found = mid; break; }
if (a[mid] < target) lo = mid + 1;
else hi = mid - 1;
}
printf("%d\n", found); // 3
return 0;
}
Common mistakes
- Binary search needs a sorted array.
- Linear search checks each element in turn.
أخطاء شائعة
- البحث الثنائي يتطلب مصفوفة مرتبة.
- البحث الخطي يفحص كل عنصر بالتتابع.
Now you try
- For binary search, assume the array is already sorted. Use
low,high, andmid. - Return
-1when the value is not found. Do not write amain— the checker provides one.
الآن جرب بنفسك
- للبحث الثنائي، افترض أن المصفوفة مرتبة مسبقًا. استخدم
low،high، وmid. - أعد
-1عندما لا يتم العثور على القيمة. لا تكتبmain— المتحقق يوفر واحدًا.
Linear vs binary search · البحث الخطي مقابل الثنائي
Binary search halves the range each step. · البحث الثنائي يُقلص النطاق إلى النصف في كل خطوة.
Complete int linear_search(const int a[], int n, int target) so it returns the index of the first target, or -1 if it is not in the array. Do not write a main. · أكمل int linear_search(const int a[], int n, int target) لتعيد فهرس أول عنصر يساوي target، أو -1 إذا لم يكن موجوداً في المصفوفة. لا تكتب دالة main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete int binary_search(const int a[], int n, int target) for a sorted array. Return the index of target, or -1. Use low, high, and mid. Do not write a main. · أكمل int binary_search(const int a[], int n, int target) لمصفوفة مرتبة. أعد فهرس العنصر target، أو -1. استخدم low وhigh وmid. لا تكتب دالة main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.
Complete int first_negative(const int a[], int n) so it returns the index of the first item less than 0, or -1 if there is none. Do not write a main. · أكمل int first_negative(const int a[], int n) لتعيد فهرس أول عنصر أصغر من 0، أو -1 إذا لم يكن هناك أي عنصر يحقق ذلك. لا تكتب دالة main.
Click Run to see the output here. · اضغط تشغيل لرؤية المخرجات هنا.