Question

When keys are stored in an array in an ascending order, what will be the best...

  1. When keys are stored in an array in an ascending order, what will be the best running time to search a certain key from the array? Explain your answer.

Homework Answers

Answer #1
When an array is sorted in ascending order, we can use binary search to search for a certain key in the array.
in binary search, we compare key with the middle element
and if key is greater than the middle element, then we search for key in right part of the array(because array is sorted in ascending order)
if key is lesser than the middle element, then we search for key in left part of the array
so, after each comparision we are essentially reducing the search space by half.
This algorithm takes O(log n) (logarithmic) time complexity.
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
#include<stdio.h> int main() {       /* For binary search, the array should be arranged in ascending...
#include<stdio.h> int main() {       /* For binary search, the array should be arranged in ascending or descending order */       int data[] = {1,2,3,4,5,6,7,8,9,10}; ****You might change the numbers for your program also       /* 'min' use for starting location of the array, 'max' use for end location of the array and 'mid' use for middle location of the array */       int min = 0, max = 10, search, mid = (max + min) / 2;       printf("Enter...
If a B-tree is of order b, then what is the maximum number of keys that...
If a B-tree is of order b, then what is the maximum number of keys that can be stored if the B-tree has height h?
Complete the following function (detailed instructions are given below the code): private static int predecessor(int array[],...
Complete the following function (detailed instructions are given below the code): private static int predecessor(int array[], int arrayLen, int key) { // Complete this function. } Given a set of numbers, the predecessor of a number x is the highest number in the set that is less than or equal to x. Thus, if I have the set {6; 9; 10; 13; 22; 31; 34; 88}, then the predecessor of 31 is 31 itself, whereas the predecessor of 30 is...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number...
1. Given an n-element array A, Algorithm X executes an O(n)-time computation for each even number in A and an O(log n)-time computation for each odd number in A. What is the best-case running time of Algorithm X? What is the worst-case running time of Algorithm X? 2. Given an array, A, of n integers, give an O(n)-time algorithm that finds the longest subarray of A such that all the numbers in that subarray are in sorted order. Your algorithm...
Consider the keys given below: 200   15   8   22   28   30   31 If we were to...
Consider the keys given below: 200   15   8   22   28   30   31 If we were to store the above keys into the hash table, which of the following three Hash Function is the best: •   Function 1:    Key % 22 •   Function 2:    Key % 10 •   Function 3:    (Key – R) % R    where R is the greatest prime number less than the smallest key Give solid reason to support your answer. A correct answer...
‏What is the output of the Euler tour in the normal binary search tree if the...
‏What is the output of the Euler tour in the normal binary search tree if the key insert order is 5 , 2 , 8 , 5 , 9 , 5 , 1 , 3 , 4 , 2 , 8 ? All keys equal to the node should be the right subtree of that node. ____________________________________________________________ ‏Construct the binary max - heap for the keys given below. Once all the keys are inserted, perform the remove maximum operation, and...
You are given a list, L, and another list, P, containing integers sorted in ascending order....
You are given a list, L, and another list, P, containing integers sorted in ascending order. The operation printLots(L, P) will print the elements in L that are in positions specified by P. For instance, if P = 1, 3, 4, 6, the elements in positions 1, 3, 4, and 6 in L are printed. Write the procedure printLots(L, P). You may use only the public STL container operations. What is the running time of your procedure?
Write code in java Implement a method to build an AVL tree out of a sorted...
Write code in java Implement a method to build an AVL tree out of a sorted (ascending order) array of unique integers, with the fastest possible big O running time. You may implement private helper methods as necessary. If your code builds a tree that is other than an AVL tree, you will not get any credit. If your code builds an AVL tree, but is not the fastest big O implementation, you will get at most 12 points. You...
Which of the following is critical to remember when setting up your array? a. columns used...
Which of the following is critical to remember when setting up your array? a. columns used for running totals need to be initialized at the value of zero b. use integer datatypes to store numbers c. columns that are dimensions should be initialized to the value of NULL. d. its best to limit the number of columns per array to 1/3 of the number of rows
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT