मराठी
महाराष्ट्र राज्य शिक्षण मंडळएचएससी विज्ञान (संगणक विज्ञान) इयत्ता १२ वी

Write an algorithm for Binary Search Method. Explain algorithm with suitable example. - Computer Science 1

Advertisements
Advertisements

प्रश्न

Write an algorithm for Binary Search Method. Explain algorithm with suitable example.

Write the Binary search Algrorithm with suitable example.

कोड लेखन
सविस्तर उत्तर
Advertisements

उत्तर

Algorithm: Binary Search
Binary(DATA, LB, UB, ITEM, LOC)
Step 1: Initialize
        BEG = LB
        END = UB
        MID = INT((BEG + END) / 2)
Step 2: While BEG ≤ END AND DATA[MID] ≠ ITEM
            If ITEM < DATA[MID] then
                END = MID - 1
            Else
                BEG = MID + 1
            End If
            MID = INT((BEG + END) / 2)
        End While
Step 3: If DATA[MID] = ITEM then
            LOC = MID
        Else
            LOC = NULL
        End If
Step 4: Exit

Example:

Given a sorted array:

11 22 30 33 40 44 55 60 66 77 80 88 99

ITEM = 40

LB = 1, UB = 13

BEG = 1, END = 13
MID = INT((1+13)/2) = 7 → DATA[7] = 55

40 < 55 → END = 6
MID = INT((1+6)/2) = 3 → DATA[3] = 30

40 > 30 → BEG = 4
MID = INT((4+6)/2) = 5 → DATA[5] = 40

ITEM found at location 5.

shaalaa.com
Basic Data Structures (Stack, Queue, Dequeue)
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
2014-2015 (March)
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×