samedi 28 mai 2016

Java Program to Perform Binary Search in Array without Recursion

The binary search algorithm is used to search an element in the sorted array. It's faster than linear search and improves performance from O(n) to O(logN) for searching an element in the array. In order to perform the binary search, you need a sorted array, so you can either ask the user to enter array in sorted order or you should sort the array before performing the binary search. In this article, we will write a Java program which will take input from the user, both array and the number to be searched and then perform a binary search to find that number in given array. We'll not use the Collections.binarySearch() method instead we'll write our own because it's a programming exercise to test one's coding skill. In order to implement binary search, you must first know how binary search works? If you don't know the algorithm you cannot code it. So, let's first revise the binary search algorithm itself.
Read more »
Share:

0 commentaires:

Enregistrer un commentaire