Binary search vs sequential search - java

At what Array Size is it better to use the Sequential Search over the Binary Search (needs to be sorted First) for these specific situations. The first case is when all the values of the array are just random numbers and not sorted. The second case is when the values or random numbers of the array are sorted numerically from least to greatest or greatest to least. For the searches assume you are only trying to find one number in the array.
Case 1: Random numbers
Case 2: Already sorted

The Sequential Search algorithm has a worst case running time of O(n) and does not depend on whether the data is sorted or not.
The Binary Search algorithm has a worst case running time of O(logn), however, in order to use algorithm the data must be sorted. If the data is not sorted, sorting the data will take O(nlogn) time.
Therefore:
Case 1: When the data is not sorted, a Sequential Search will be more time efficient as it will take O(n) time. A Binary Search would require the data to be sorted in O(nlogn) and then searched O(logn). Therefore, the time complexity would be O(nlogn) + O(logn) = O(nlogn).
Case 2: When the data is sorted, a Binary Search will be more time efficient as it will only take O(logn) time, while the Sequential Search will still take O(n) time.

Binary search is always better. But binary search always requires to array should be sorted.

Related

Search the different number in array, when all the other numbers are same , can this be done in O(logn) using divide and conquer

Lets say we have a very large array and we need to find the only different number in the array, all the other numbers are the same in the array, can we find it in O(log n) using divide and conquer, just like mergeSort, please provide an implementation.
This cannot be done in better time complexity than O(n) unless that array is special. With the constraints you have given, even if you apply an algorithm like divide and conquer you have to visit every array element at least once.
As dividing the array will be O(log n) and comparing 2 elements when array is reduced to size 2 will be O(1)
This is wrongly put. Dividing the array is not O(log n). The reason why something like a binary search works in O(log n) is because the array is sorted and that way you can discard the other half of the array at every step even without looking at what elements they have, thereby halving the size of original problem.
Intuitively, you can think this as follows : Even if you keep on dividing the array into halves, the leaf nodes of the tree formed are n/2 (Considering you compare 2 elements at leaf). You will have to make n/2 comparisons, which leads to asymptotic complexity of O(n).

Does this mean binary search algorithm?

I have to write a small Java program to find out how long does it take for performing a search algorithm.
The algorithm reads as:
Assume you have a search algorithm which, at each level of recursion, excludes half of the data from consideration when searching for a specific data item. Search stops only when one data item is left.
I would like to know your opinion about which search algorithm it is.
sounds like a binary search or half-interval search provided that your collection or data is sorted, worst and everage case in O(log n)
if you have to sort first, then the best algorithm will give you O(n log n), then plus O(log n) of binary search , overall it becomes O(n log n)
If your data is sorted, then yes this is binary search algorithm. This algorithm comes under "Divide and Conquer" strategy. In this, each time algorithm goes on dividing the data and half of the data is eliminated. The basic assumption is data is sorted before applying the algorithm.

Searching for a particular String in an array

I would like to know what is the fastest way / algorithm of checking for the existence of a word in a String array. For an example, if I have a String array with 10,000 elements, I would like to know whether it has the word "Human". I can sort the array, no problem.
However, binary search (Arrays.binarySearch()) is not allowed. Other collection types like HashSet, HashMap and ArrayList are not allowed too.
Is there any proven algorithm for this? Or any other method? The way of searching should be really really fast.
the fastest way you can sort will result in O(nLogn) complexity
so if you are looking for particular word in unordered data just scan the array with single for cycle, that will cost you O(n)
For fastest performance you have to use hashing .
You can use rolling hash .
It ensures lesser number of collisions .
hash = [0]*base^(n-1) + [1]*base^(n-2) + ... + [n-1]
where base is a prime number , say 31 .
You need to take modulo also , so integer range is not exceeded , by a prime number .
Time complexity : O(number of characters) considering multiplication and modulo O(1) operation .
A very good explaination is given here : Fast implementation of Rolling hash
Build a trie out of the array. It can be built in linear time (assuming a constant size alphabet). Then you can query in linear time as well (time proportional to the query word length). Both preprocessing and query time are asymptotically optimal.

about the usage of modulus operator

this a part of code for Quick Sort algorithm but realy I do not know that why it uses rand() %n please help me thanks
Swap(V,0,rand() %n) // move pivot elem to V[0]
It is used for randomizing the Quick Sort to achieve an average of nlgn time complexity.
To Quote from Wikipedia:
What makes random pivots a good choice?
Suppose we sort the list and then
divide it into four parts. The two
parts in the middle will contain the
best pivots; each of them is larger
than at least 25% of the elements and
smaller than at least 25% of the
elements. If we could consistently
choose an element from these two
middle parts, we would only have to
split the list at most 2log2n times
before reaching lists of size 1,
yielding an algorithm.
Quick sort has its average time complexity O(nlog(n)) but worst case complexity is n^2 (when array is already sorted). So to make it O(nlog(n)) pivot is chosen randomly so rand()%n is generating a random index between 0 to n-1.

What is the difference between quicksort and tuned quicksort?

What is the fundamental difference between quicksort and tuned quicksort? What is the improvement given to quicksort? How does Java decide to use this instead of merge sort?
As Bill the Lizard said, a tuned quicksort still has the same complexity as the basic quicksort - O(N log N) average complexity - but a tuned quicksort uses some various means to try to avoid the O(N^2) worst case complexity as well as uses some optimizations to reduce the constant that goes in front of the N log N for average running time.
Worst Case Time Complexity
Worst case time complexity occurs for quicksort when one side of the partition at each step always has zero elements. Near worst case time complexity occurs when the ratio of the elements in one partition to the other partition is very far from 1:1 (10000:1 for instance). Common causes of this worst case complexity include, but are not limited to:
A quicksort algorithm that always chooses the element with the same relative index of a subarray as the pivot. For instance, with an array that is already sorted, a quicksort algorithm that always chooses the leftmost or rightmost element of the subarray as the pivot will be O(N^2). A quicksort algorithm that always chooses the middle element gives O(N^2) for the organ pipe array ([1,2,3,4,5,4,3,2,1] is an example of this).
A quicksort algorithm that doesn't handle repeated/duplicate elements in the array can be O(N^2). The obvious example is sorting an array that contains all the same elements. Explicitly, if the quicksort sorts the array into partitions like [ < p | >= p ], then the left partition will always have zero elements.
How are these remedied? The first is generally remedied by choosing the pivot randomly. Using a median of a few elements as the pivot can also help, but the probability of the sort being O(N^2) is higher than using a random pivot. Of course, the median of a few randomly chosen elements might be a wise choice too. The median of three randomly chosen elements as the pivot is a common choice here.
The second case, repeated elements, is usually solved with something like Bentley-McIlroy paritioning(links to a pdf) or the solution to the Dutch National Flag problem. The Bentley-McIlroy partitioning is more commonly used, however, because it is usually faster. I've come up with a method that is faster than it, but that's not the point of this post.
Optimizations
Here are some common optimizations outside of the methods listed above to help with worst case scenarios:
Using the converging pointers quicksort as opposed to the basic quicksort. Let me know if you want more elaboration on this.
Insertion sort subarrays when they get below a certain size. Insertion sort is asymptotically O(N^2), but for small enough N, it beats quicksort.
Using an iterative quicksort with an explicit stack as opposed to a recursive quicksort.
Unrolling parts of loops to reduce the number of comparisons.
Copying the pivot to a register and using that space in the array to reduce the time cost of swapping elements.
Other Notes
Java uses mergesort when sorting objects because it is a stable sort (the order of elements that have the same key is preserved). Quicksort can be stable or unstable, but the stable version is slower than the unstable version.
"Tuned" quicksort just means that some improvements are applied to the basic algorithm. Usually the improvements are to try and avoid worst case time complexity. Some examples of improvements might be to choose the pivot (or multiple pivots) so that there's never only 1 key in a partition, or only make the recursive call when a partition is above a certain minimum size.
It looks like Java only uses merge sort when sorting Objects (the Arrays doc tells you which sorting algorithm is used for which sort method signature), so I don't think it ever really "decides" on its own, but the decision was made in advance. (Also, implementers are free to use another sort, as long as it's stable.)
In java, Arrays.sort(Object[]) uses merge sort but all other overloaded sort functions use
insertion sort if length is less than 7 and if length of array is greater than 7 it uses
tuned quicksort.

Categories

Resources