Ask Question
1 February, 04:22

Suppose that you are given a sorted sequence of distinct integers {a1, a2, ..., an}. give an o (lgn) algorithm to determine whether there exists an i index such as ai

+5
Answers (1)
  1. 1 February, 05:07
    0
    Since the sequence is sorted, you can use the binary search, which is of O (lg2). You can google binary search, or inspire from the following.

    Basically, the algorithm looks like the following:

    checkMiddle (ai, a1, n)

    if (n==0) return (-1) / / key ai not found

    k = (1+n) / 2;

    if (a_k = ai) return (k)

    else

    if (a_k>ai

    checkMiddle (ai, a_1, k)

    else

    checkMiddle (ai, a_ (k+1), n-k)

    endif

    You may have to tune and check, but the basic idea is there.

    Also, concerning computing algorithms, you are better off with posting it under technology and computers.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Suppose that you are given a sorted sequence of distinct integers {a1, a2, ..., an}. give an o (lgn) algorithm to determine whether there ...” in 📘 Mathematics if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers