logo

אלגוריתם חיפוש בינארי ב-C

שיטה מהירה לאיתור אלמנט מסוים במערך ממוין היא חיפוש בינארי. המשימה הראשונית של אלגוריתם זה היא להשוות את ערך היעד לאלמנט האמצעי של המערך. החיפוש נחשב מוצלח אם ערך היעד כלול באלמנט האמצעי. האלגוריתם יראה בחצי השמאלי של המערך אם ערך המטרה קטן מהאלמנט המרכזי. התוכנית תסרוק את החצי הימני של המערך אם ערך המטרה גדול מהאלמנט המרכזי. שיטה זו חוזרת על עצמה עד למיצוי ערך היעד או טווח החיפוש.

נוֹהָג:

מסדי נתונים, מנועי חיפוש ועיבוד נתונים הם רק חלק קטן מהיישומים המשתמשים באסטרטגיית החיפוש הבינארית.

מאפיינים:

  • יש למיין את מערך ערכי הקלט.
  • עם כל איטרציה, השיטה מצמצמת את טווח החיפוש בחצי, מה שהופך אותה ליעילה במיוחד עבור מערכי נתונים ענקיים.
  • לאלגוריתם יש O (log n) מורכבות זמן במקרה הגרוע ביותר.
  • מציאת הערך הרצוי מתבצעת על ידי התוכנית באמצעות אסטרטגיית חלוקה-וכבש.

הנה דוגמה פשוטה של ​​אלגוריתם החיפוש הבינארי שנכתב ב-C:

 #include int binary_search(int arr[], int left, int right, int target) { while (left <= right) { int mid="left" + (right - left) 2; if (arr[mid]="=" target) return mid; } else < left="mid" 1; right="mid" -1; target not found main() arr[]="{1," 3, 5, 7, 9}; n="sizeof(arr)" sizeof(arr[0]); index="binary_search(arr," 0, 1, target); (index="=" -1) printf('target found
'); at %d
', index); 0; pre> <p> <strong>Output:</strong> </p> <pre> Target found at index 2 </pre> <ul> <li>The binary_search function accepts four arguments: the array to search, the left and right search range boundaries, and the target value to look for. The function returns its index if the desired value can be found; else, it returns -1.</li> <li>The main function creates an array arr and a value target. The binary_search function is then used to search the array for the desired value. The function returns the index where the target value was located if it was, the function returns the index at which it was found. Otherwise, the message &apos;Target not found&apos; is displayed.</li> <li>The binary search algorithm&apos;s implementation is basic. We begin by setting the left border to the array&apos;s initial index and the right boundary to the array&apos;s last index. Once the left boundary is less than or equal to the right border, the array is looped through one more time. We use the formula (left + right) / 2 within the loop to calculate the middle index of the search range. This formula computes the integer value of the middle index&apos;s floor.</li> <li>The centre member of the array is contrasted with the target value. We return the index of the middle element if they are equal. We change the right boundary to be one less than the middle index if the desired value is less than the middle element. If not, we adjust the left border so that it is one more than the centre index. We continue doing this until the goal value is obtained or the search space is filled.</li> <li>The temporal complexity of the binary search algorithm, where n is the array size, is O(log n). This is far more efficient than linear search, which has a temporal complexity of O(n), where n is the size of the array.</li> <li>Finally, the binary search technique offers a useful way to locate a particular member in a sorted array. It is easy to build and has an O(log n) time complexity, making it an efficient approach for large datasets.</li> </ul> <h3>Advantages:</h3> <ul> <li>For large datasets, the binary search algorithm is exceptionally efficient, and it is capable of handling a wide range of input sizes.</li> <li>The algorithm is simple to implement in almost all programming languages.</li> </ul> <h3>Disadvantages:</h3> <ul> <li>Before using the binary search technique, the input array must be sorted, which takes more time and memory.</li> <li>The algorithm cannot be applied to unsorted arrays.</li> <li>The algorithm may yield inaccurate results if the input array is not sorted.</li> <li>The binary search algorithm is not appropriate for tiny datasets since the technique&apos;s overhead may outweigh its benefits.</li> </ul> <h2>Conclusion:</h2> <p>A sorted array can be quickly searched for a specific element using the binary search technique. It employs a divide-and-conquer strategy to cut the search range in half with each iteration, allowing it to be highly efficient for large datasets. However, before using the binary search technique, the input array must be sorted, which takes extra time and memory. The binary search algorithm is a sophisticated data processing tool that is widely utilised in various sectors.</p> <hr></=>
  • הפונקציה binary_search מקבלת ארבעה ארגומנטים: המערך לחיפוש, גבולות טווח החיפוש השמאלי והימני וערך היעד שיש לחפש. הפונקציה מחזירה את האינדקס שלה אם ניתן למצוא את הערך הרצוי; אחרת, הוא מחזיר -1.
  • הפונקציה הראשית יוצרת מערך arr ומטרת ערך. לאחר מכן נעשה שימוש בפונקציה binary_search כדי לחפש במערך את הערך הרצוי. הפונקציה מחזירה את המדד שבו היה ממוקם ערך היעד אם היה, הפונקציה מחזירה את המדד שבו הוא נמצא. אחרת, ההודעה 'יעד לא נמצא' תוצג.
  • היישום של אלגוריתם החיפוש הבינארי הוא בסיסי. נתחיל בקביעת הגבול השמאלי לאינדקס ההתחלתי של המערך ואת הגבול הימני לאינדקס האחרון של המערך. ברגע שהגבול השמאלי קטן או שווה לגבול הימני, המערך עובר בלולאה פעם נוספת. אנו משתמשים בנוסחה (שמאל + ימין) / 2 בתוך הלולאה כדי לחשב את האינדקס האמצעי של טווח החיפוש. נוסחה זו מחשבת את הערך השלם של הרצפה של המדד האמצעי.
  • האיבר המרכזי של המערך מנוגד לערך היעד. נחזיר את האינדקס של האלמנט האמצעי אם הם שווים. אנו משנים את הגבול הימני להיות אחד פחות מהאינדקס האמצעי אם הערך הרצוי קטן מהאלמנט האמצעי. אם לא, נתאים את הגבול השמאלי כך שיהיה אחד יותר מהאינדקס המרכזי. אנו ממשיכים לעשות זאת עד לקבלת ערך המטרה או למילוי שטח החיפוש.
  • המורכבות הזמנית של אלגוריתם החיפוש הבינארי, כאשר n הוא גודל המערך, היא O(log n). זה הרבה יותר יעיל מחיפוש ליניארי, שיש לו מורכבות זמנית של O(n), כאשר n הוא גודל המערך.
  • לבסוף, טכניקת החיפוש הבינארי מציעה דרך שימושית לאתר איבר מסוים במערך ממוין. קל לבנות אותו ויש לו מורכבות זמן O(log n), מה שהופך אותו לגישה יעילה עבור מערכי נתונים גדולים.

יתרונות:

  • עבור מערכי נתונים גדולים, אלגוריתם החיפוש הבינארי יעיל במיוחד, והוא מסוגל לטפל במגוון רחב של גדלי קלט.
  • האלגוריתם פשוט ליישום כמעט בכל שפות התכנות.

חסרונות:

  • לפני השימוש בטכניקת החיפוש הבינארי, יש למיין את מערך הקלט, דבר שלוקח יותר זמן וזיכרון.
  • לא ניתן להחיל את האלגוריתם על מערכים לא ממוינים.
  • האלגוריתם עלול להניב תוצאות לא מדויקות אם מערך הקלט אינו ממוין.
  • אלגוריתם החיפוש הבינארי אינו מתאים למערכי נתונים זעירים מכיוון שהתקורה של הטכניקה עשויה לעלות על היתרונות שלה.

סיכום:

ניתן לחפש במהירות מערך ממוין אחר אלמנט ספציפי באמצעות טכניקת החיפוש הבינארי. הוא משתמש באסטרטגיית חלוקה-וכבש כדי לחתוך את טווח החיפוש בחצי עם כל איטרציה, מה שמאפשר לו להיות יעיל מאוד עבור מערכי נתונים גדולים. עם זאת, לפני השימוש בטכניקת החיפוש הבינארי, יש למיין את מערך הקלט, דבר שלוקח זמן וזיכרון נוספים. אלגוריתם החיפוש הבינארי הוא כלי מתוחכם לעיבוד נתונים שנמצא בשימוש נרחב בסקטורים שונים.