Mathematics Homework Solutions
Problem
#106875

Show, how the given binary search algorithm executes in case of given key value.

Given algorithm looks for a value in a nondecreasing sequence and returns the index of the value if it is found or 0 if it is not found.

Input: A sequence si, ...... ,sj  (j >= i >= 1) sorted in nondecreasing order, a value key, i, and j
Output: The output is an index k for which sk = key, or if key is not in the sequence, the output is the value 0.

binary_search(s, i, j, key) {
    if (i > j)            // not found
        return 0
    k = (i + j)/2
    if (key == sk)   // found
        return k
    if (key < sk)     // search left half
        j = k - 1
    else                 // search right half
        i = k + 1
    return binary_search(s, i, j, key)
}

Consider the sequence s1 = 'C',   s2 = 'G',   s3 = 'J',   s4 = 'M',   s5 = 'X'. Show how the algorithm executes in case key = 'C'.

Attached file(s):
Attachments
doc1.doc  View File

Attachment Content Summary (Note: view attachment at the above link before purchasing. Actual attachment content may vary slightly from that shown below.)

doc1.doc
Refer to the sequence s1 = ‘C’, s2 = ‘G’, s3 = ‘J’, s4
= ‘M’, s5 = ‘X’

This algorithm looks for a value in a nondecreasing sequence and returns
the index of the value if it is found or 0 if it is not found.

1, sorted in nondecreasing order, a value key, i, and j

Output: The output is an index k for which sk = key, or if key is not in
the sequence, the output is the value 0.

binary_search(s, i, j, key){

if (i > j) // not found

return 0

k = (i + j)/2

if (key = = sk) // found

return k

if (key < sk) // search left half

j = k – 1

else // search right half

i = k + 1

return binary_search(s, i, j, key)

}

Show how the Algorithm above executes in case key = ‘C’

Solution Summary

Attached document gives statement by statement execution of each binary_search call, explaining the execution/non-execution of individual statements during the call. Additionally it counts the number of operations at algorithm level during each call, excluding the contribution from recursive call.

Solution
What is this?
By OTA - Overall OTA Rating
Purchase Cost Now
$2.19 CAD (was ~$3.99)
Included in Download
  • Plain text response
  • Attached file(s):
    • 106875_doc1.doc
$2.19 Instant Download
Add to Cart
Why you can trust BrainMass.com
  • Your Information is Secure
  • Best Online Academic Help Service
  • Students find real academic Success
Related Solutions
Browse