Question

In the text box below, write down a divide and conquer algorithm for counting the number...

In the text box below, write down a divide and conquer algorithm for counting the number of entries in a sorted array of ints that are smaller than a given value.  In other words, the function takes as input an array A and an int value and returns the number of ints in A that are less than value.  To get any credit, your solution must use the divide and conquer technique.  To get full credit, your solution should run in  time in the worst case (where n is the size of the array) and must work on arrays that contain duplicate entries. You can still get half credit for a solution that runs in  time in the worst case or that works only on arrays without duplicate entries.
Hint1:  Remember that every divide and conquer algorithm we’ve done on arrays requires a recursive function that has extra parameters to indicate the sub range of the array that you are focusing on.  You may add extra input parameters below.
Hint2: If you are unsure how to start, you might begin by ignoring the fact that the array is sorted and coming up with a divide and conquer algorithm that isn’t fast, but that works on an unsorted array first, and then, improve your algorithm by modifying it to take advantage of the fact that the array is sorted.

Input parameters:  A : array of int,  value : int
Output:  the number of ints in A that are smaller than value


0 0
Add a comment Improve this question Transcribed image text
Answer #1

package search;

public class findNumber {

public static int mergeSearch(int ar[],int l,int r,int k){

if(l<r){

int mid=(l+r)/2; //take mid index

int f=mergeSearch(ar,l,mid,k); //call mergesort on first half and return smaller in first half subaaray

int s=mergeSearch(ar,mid+1,r,k); //call mergesort on second half and return smaller in second half subaaray

int res=marge(ar,l,r,mid,k); //find the max in both side

return res; //now return max of all three

}

return -1;

}

public static int marge(int ar[],int l,int r,int mid,int k){

int res=0;

for(int i=l;i<=mid;i++){ //find the smaller in first half

if(ar[i]<k){

res++;

}

}

for(int i=mid+1;i<=r;i++){ //find the smaller in second half

if(ar[i]<k){

res++;

}

}

return res;

}

//driver program to test

public static void main(String[] args) {

int ar[]={1,2,3,4,5,6,7};

int k=mergeSearch(ar,0,ar.length-1,5);

System.out.println(k);

ar=new int[]{1,2,2,4,5,5,7};

k=mergeSearch(ar,0,ar.length-1,10);

System.out.println(k);

ar=new int[]{1,4,8,9,10,11};

k=mergeSearch(ar,0,ar.length-1,0);

System.out.println(k);

}

}

output

4

7

0

Add a comment
Know the answer?
Add Answer to:
In the text box below, write down a divide and conquer algorithm for counting the number...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct...

    1. Design and write a Divide& Conquer algorithm that, given an array A of n distinct integers which is already sorted into ascending order, will find if there is some i such that Ali] in worst-case 0(log n) time.

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...

  • 6.3.1 [10] <§6.2> Consider the following binary search algorithm (a classic divide and conquer algorithm) that...

    6.3.1 [10] <§6.2> Consider the following binary search algorithm (a classic divide and conquer algorithm) that searches for a value X in a sorted N-element array A and returns the index of matched entry: BinarySearch(A[0..N−1], X) { low = 0 high = N −1 while (low <= high) { mid = (low + high) / 2 if (A[mid] >X) high = mid −1 else if (A[mid] <X) low = mid + 1 else return mid // found } return −1...

  • Question 1. Below is the pseudo code for a divide and conquer algorithm that finds the...

    Question 1. Below is the pseudo code for a divide and conquer algorithm that finds the minimum value in an array. Suppose that the input array, A, is of size n, analyze the computational cost of this algorithm in the form of Tin) and prove your conclusion findMin (A, left, right) if (left == right) return Alleft) center - (left + right) / 2 return min (findMin (A, left, center), findMin (A, center + 1, right)

  • I am working on the divide/conquer algorithm. I am having a trouble with a print for...

    I am working on the divide/conquer algorithm. I am having a trouble with a print for output from reading the file. Here is my work. When you see I put the comment with TODO. that one I am struck with readfile. I wonder if you'd able to help me to fix the readfile to find a single number. Here is the input3.txt (1 12 13 24 35 46 57 58 69). after that, the output should be 0. int mergeInversion(int...

  • Algorithms and Basic Data Structures

    This part involves finding a new algorithm by yourself. You do NOT have to implement it with C++. You may just state the algorithm in pseudo-code. The problem is as follows: You have an integer array of n elements. You want to return true if there is a set of more than n/2 elements in the array with the same value, false otherwise. Notice that if the array is sorted (that is you are allowed to use a sorting algorithm),...

  • 6.) (a) A string contains several X's followed by several O's. Devise a divide-and-conquer algorithim that finds the number of X's in the string in log2n steps, where n is the length of th...

    6.) (a) A string contains several X's followed by several O's. Devise a divide-and-conquer algorithim that finds the number of X's in the string in log2n steps, where n is the length of the string. (b) An array originally contained different numbers in ascending order but may have been subsequently rotated by a few positions. For example, the resulting array may be: 21 34 55 1 2 3 4 5 8 13 Is it possible to adapt the Binary Search...

  • 3. For each of the following situations, name the best sorting algorithm we studied. (For one...

    3. For each of the following situations, name the best sorting algorithm we studied. (For one or two questions, there may be more than one answer deserving full credit, but you only need to give one answer for each.) The array is mostly sorted already (a few elements are in the wrong place). (a) You need an O(n log n) sort even in the worst case and you cannot use any extra space except for a few local variables. (b)...

  • IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are...

    IN C++ ADD COMMENTS AS MUCH AS POSSIBLE Exercise 1: Duplicate the Arrays Suppose you are developing a program that works with arrays of integers, and you find that you frequently need to duplicate the arrays. Rather than rewriting the array-duplicating code each time you need it, you decide to write a function that accepts an array and its size as arguments. Creates a new array that is a copy of the argument array, and returns a pointer to the...

  • Data Structures: For each of the following situations, name the best sorting algorithm we studied. (For...

    Data Structures: For each of the following situations, name the best sorting algorithm we studied. (For one or two questions, there may be more than one answer deserving full credit, but you only need to give one answer for each.) (a) The array is mostly sorted already (a few elements are in the wrong place). (b) You need an O(n log n) sort even in the worst case and you cannot use any extra space except for a few local...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT