Top 10 Advance Java Interview questions? What are the differences between abstract classes and interfaces in Java? What is the difference between ArrayList and LinkedList in Java? What is the purpose of the finalize() method in Java? What is polymorphism in Java and how is it achieved? What are the different types of inner classes in Java? What is the difference between static and non-static methods in Java? What are the different types of exceptions in Java and how do they differ? What is the difference between checked and unchecked exceptions in Java? How does Java handle multithreading and synchronization? What are the different types of JDBC drivers in Java and how do they differ?
Different between Linear search vs Binary search
Linear search | Binary search |
It follows a sequential approach. | This follows a divide and conquer approach. |
It works well on unsorted data. | To apply binary search, data has to be sorted. |
Its worst-case time complexity is O(n). | Its worst-case time complexity is O(log n) |
It is slower in comparison to binary search. | It is more efficient than linear search. |
In linear search, we compare an element with every other element. | In binary search, we don’t compare an element with all other elements. We leave a few comparisons. |
We prefer linear search only for small-sized data. | It is preferred for large-sized data. |
We can implement linear search on all linear data structures like arrays and linked lists. | We can implement binary search only on those data structures that permit 2-way traversal. |
Comments
Post a Comment