Question

In: Computer Science

Assume that the incoming array is sorted and adjust the algorithm below to allow for faster...

Assume that the incoming array is sorted and adjust the algorithm below to allow for faster exit if the searched for value is not in the list.

void linearsearch (int n, int Arr[], int a, int& index){

index = 1;

while (index <= n && Arr[index] != a)

index++;

if (index > n)

index = 0;

}

Solutions

Expert Solution

In the algorithm mentioned, the assumptions made are

n = number of elements in the Array

Arr[] = Sorted array provided

a = element to be searched

index = index element to be used in the algorithm

There could be many ways out of which I am suggesting two ways to exit out faster if the searched value is not in the list

1) Include a separate If condition after while loop

void linearsearch (int n, int Arr[], int a, int& index){

index = 1;

while (index <= n && Arr[index] != a)

/* Since the array is sorted.We can search the list further only if the element is larger than current Index value.If it is smaller and not matching value,then we will exit from loop .In this way search time will be reduced */

if (a > Arr[index])

index++;

else

print (“Element”,a, “not present in the searched array”)

exit; // Break from the loop displaying the above message

if (index > n)

index = 0;

}

2) Modify the while loop to add equality condition ( Arr[index] < a ) as well

void linearsearch (int n, int Arr[], int a, int& index){

index = 1;

/* Since the array is sorted.We can search the list further only if the element is larger than current Index value.We will add this extra condition in the While condition so as to exit the search loop If indexed value is greater than the searched value .In this way search time will be reduced */

while (index <= n && Arr[index] != a && Arr[index] < a)

if (index > n)

index = 0;

}


Related Solutions

The insertion sort algorithm updates its sorted array as each new data value is entered. It...
The insertion sort algorithm updates its sorted array as each new data value is entered. It is an in-place algorithm, in that the sorted array at each step writes over the array from the previous step. • Let us start with sorting in descending order (largest to smallest). The ascending order case follows as a simple modification to the descending case. • Assume that we have thus far read N values and these are sorted in correct order (largest to...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array of size n into k subsets each of size n/k and recursively searches only one of these k subsets. Which one of these k subsets should be searched is decided by making (k − 1) comparisons, each of which can be done in constant time. Clearly, the recurrence relation for this k-ary search algorithm can be written as, T(n) = T(n/k) + (k −...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the help of : (a) insertion sort; (b) selection sort; (c) bubble sort with swaps counting; (d) bubble sort without swaps counting subject design and analysis of algorithms answer should be like eg p u r p l e p r u p l e
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the...
Create an array of 10,000 elements, use sorted, near sorted, and unsorted arrays. Implement find the kth smallest item in an array. Use the first item as the pivot. Compare sets of results using a static call counter. Reset counter before running another search. Create a Test drive to exhaustively test the program. // Assume all values in S are unique. kSmall(int [] S, int k): int (value of k-smallest element) pivot = arbitrary element from S:  let’s use the first...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array...
Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. Return that integer. Input: arr = [1,2,2,6,6,6,6,7,10] Output:
Developing a Faster Intersection Algorithm Scenario We have already seen an algorithm that produces an intersection...
Developing a Faster Intersection Algorithm Scenario We have already seen an algorithm that produces an intersection between two input arrays in Snippet 1.6, shown below: public List intersection(int[] a, int[] b) { List result = new ArrayList<>(a.length); for(int x : a) { for(int y : b) { if (x == y) result.add(x); } } return result; } We have already shown how the runtime complexity of this algorithm is O(n2). Can we write an algorithm with a faster runtime complexity?...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 52 sample problems. The new algorithm completes the sample problems with a mean time of 16.34 hours. The current algorithm completes the sample problems with a mean time of 16.93 hours. The standard deviation is found to be 3.913 hours for the new algorithm, and 3.6243.624 hours for the current algorithm. Conduct a hypothesis test at the...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 41sample problems. The new algorithm completes the sample problems with a mean time of 23.42 hours. The current algorithm completes the sample problems with a mean time of 26.39 hours. Assume the population standard deviation for the new algorithm is 3.442 hours, while the current algorithm has a population standard deviation of 5.364 hours. Conduct a hypothesis...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 5454 sample problems. The new algorithm completes the sample problems with a mean time of 11.9011.90 hours. The current algorithm completes the sample problems with a mean time of 14.0714.07 hours. Assume the population standard deviation for the new algorithm is 5.1155.115 hours, while the current algorithm has a population standard deviation of 5.7365.736 hours. Conduct a...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each...
A systems analyst tests a new algorithm designed to work faster than the currently-used algorithm. Each algorithm is applied to a group of 51 51 sample problems. The new algorithm completes the sample problems with a mean time of 24.43 24.43 hours. The current algorithm completes the sample problems with a mean time of 24.68 24.68 hours. The standard deviation is found to be 3.481 3.481 hours for the new algorithm, and 3.420 3.420 hours for the current algorithm. Conduct...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT