Question

In: Computer Science

If you run the binary search method on a set of unordered data, What happens? Why?...

If you run the binary search method on a set of unordered data, What happens? Why? How you would correct this issue?

Solutions

Expert Solution

If we run the binary search method on a set of unordered data then the results are unpredicable. If the data set has the target, it may or may not be found.

Just for kicks, I ran a little experiment. First, I picked an array size and generated an int array {0, 1, ..., size-1}. Then I shuffled the array, did a binary search for each value 0, 1, ..., size-1 and counted how many of these were found. For each size, I repeated the shuffle/search-for-each-value steps 100,000 times and recorded the percent of searches that succeeded. (This would be 100% for a sorted array.) The results are (rounded to the nearest percent):

Size    % Hit
 10      34%
 20      22%
 30      16%
 40      13%
 50      11%
 60      10%
 70       9%
 80       8%
 90       7%
100       6%

So the larger the array, the worse the effects of not sorting. Even for relatively small arrays, the results are pretty drastic.

Binary Search relies on data being sorted. It picks an element in an array and determines 1. If this is the element it is searching 2. If it is not the element it is looking for, where can it possibly find the element.

The second point relies on data being sorted to make a decision. Imagine an unsorted data. Just by comparing the search key with the element that we have picked, we will not be able to identify where the element could occur.

So, binary search cannot work consistently in unsorted data.

We would correect this issue by first sorting the input data and then applying the binary search on it.


Related Solutions

1. What is the Big-O run-time of linear search and binary search in the best, average...
1. What is the Big-O run-time of linear search and binary search in the best, average and worst cases?       Linear Search                  Binary Search Best: Worst: Average: 2. What is the Big-O runtime of the following linked list methods? addFirst() toString() What is the Big-O runtime of the below Stack method? isSorted(Node n) 3. What is the Big-O runtime of the below methods: Method A: Stack Copy Constructor Option 1 public Stack(Stack<T> original) { if(original == null) { return; }...
1.What happens if you attempt to call a method using a referencevariable that is set...
1.What happens if you attempt to call a method using a reference variable that is set to null?2.Consider the following class declaration:public class Square{   private double side;   public Square (double s){    side = s;}public double getArea(){   return side * side;}public double getSide(){   return side;}}a. Write a toString method for this class. The method should return a string containing the side and area of the square.b. Write a equals method for this class. The method should accept a Square object as...
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
In what situation could you not use a binary search
In what situation could you not use a binary search
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
Question 2: Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary...
PYTHON CODING Create a method (sortTraversal) for a Binary Search Tree that prints out the Binary Search Tree in ascending or deceasing order. The order type is an input to the method and can be "ascending" or "descending". The ascending input would return the node values of the tree beginning with the smallest and ending with the largest, descending returns the opposite. Discuss method's Big-O notation. Add proper and consistent documentation to identify code sections or lines to clearly identify...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
write a method in java for a binary search tree that receives a node as input...
write a method in java for a binary search tree that receives a node as input and returns the successor node.
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum...
Given a minimum unimodal array of integers, run the binary search algorithm to find the minimum element. You need to show the initial and the iteration-level values of the left index, right index and middle index as well as your decisions to reduce the search space in each iteration. 42 39 2 6 9 16 20 28 31 34
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT