What is the average number of comparisons needed in a
sequential search to determine the position of an element in
an array of 100 elements, if the elements are ordered from
largest to smallest?
Answer Posted / ranjit
Gianni, Your answer would be correct if the list was unordered. Since it is a sorted list (descending order), one does not have to go through the whole list in order to determine that the element is not present. Thus, the problem is to simply traverse the list until you find a number <= the number, x, you are looking for. This can be done with (n+1)/2 comparisons on average. Probability of x being present in the list is not a factor.
Shyam, as I explained in the above para, the fact that the list is sorted is relevant. If it was unsorted, Gianni would be absolutely right.
| Is This Answer Correct ? | 0 Yes | 1 No |
Post New Answer View All Answers
Can we use any class as map key?
How to create your own data structure in java?
What is the best case time complexity of bubble sort?
Which collection is fail safe?
What is the use of tree data structure?
Does hashtable allow null values?
How do you find the depth of a binary tree?
There is a program which inserts and deletes node in a sorted singly linked list. There is a bug in one of the modules, how would you debug it?
Can we add heterogeneous elements into treemap?
What does bubble sort do?
If you are given a choice to use either arraylist and linkedlist, which one would you use and why?
Briefly explain recursive algorithm?
What are the advantages of bubble sort?
Is it possible to make an array volatile in java?
Can you please explain the difference between string and an array?