Question

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 algorithm for such data? Explain.

[Binary Search Algorithm below]

public class BinarySearch {

/**

* Uses Binary Search to look for target in an array a, sorted in ascending order. If found, returns the index of the matching element; otherwise returns -1.

*/

public static <T> int find(T[] a, Comparable<? super T> target) {

int left = 0, right = a.length - 1;

while (left <= right)

//Take the index of the middle element between "left" and "right":

int middle = (left + right) / 2;

//Compare this element to the target value and adjust the search range accordingly:

int diff = target.compareTo(a[middle]);

if(diff < 0)

right - middle - 1;

else if (diff > 0)

left = middle + 1;

else

return middle;

}

return -1;

}

}

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

6.

a)

As in our input, string S contains X's followed by O's

So we can use the below algorithm to find the last index of X, Which is the number of X's in S.

  1. left = 1, right = length(Input string)
  2. while( left < right )
  3. { find middle = (left + right )/2
  4. if ( S[middle] == 'O' )
  5. then right = middle -1.
  6. else
  7. if ( S[middle]=='X' and S[middle+1]=='O')
  8. return(middle) //middle is the last index of X
  9. else
  10. left = middle +1.
  11. }

Here, in each iteration problem size is reduced by 2, So overall it takes log2n steps.

b)

Yes, we can adapt the binary search algorithm, but before that, we have to find the pivot element such that the preceding element is greater than the pivot and the following element is smaller than the pivot.

Then dividing the array into two subarrays at pivot element, we can adapt the binary search algorithm on both subarrays to find some element.

And now how to find the pivot element:

  • each time find the middle index and then check if the condition to be the pivot element is true or not.
  • if true then stop and choose the middle element as pivot
  • else repeat the first step to find pivot
Add a comment
Know the answer?
Add Answer to:
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...
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
  • Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary...

    Searching/sorting tasks and efficiency analysis - Binary Search Search for the character S using the binary search algorithm on the following array of characters: A E G K M O R S Z. For each iteration of binary search use a table similar to the table below to list: (a) the left index and (b) the right index of the array that denote the region of the array that is still being searched, (c) the middle point of the array,...

  • Need help with this Java project implementing an interpolation search, bolded the missing parts: /**   ...

    Need help with this Java project implementing an interpolation search, bolded the missing parts: /**    A class that implements an interpolation search and    a binary search of a sorted array of integers.    @author Frank M. Carrano    @author Timothy M. Henry    @version 4.0 */ public class SearchComparer {    private int[] a;    private int interpolationSearchCounter;    private int binarySearchCounter;    private static final int CAPACITY = 50;    public SearchComparer(int[] array, int n)    {...

  • Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch...

    Java Programming Write a program to find the number of comparison using binarySearch and the sequentialSearch algorithms as follows: Suppose list is an array of 2500 elements. 1. Use a random number generator to fill list; 2. Use a sorting algorithm to sort list; 3. Search list for some items as follows: a) Use the binary search algorithm to search list (please work on SearchSortAlgorithms.java and modify the algorithm to count the number of comparisons) b) Use the sequential search...

  • //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded....

    //Generic interface that describes various searching and sorting //algorithms. Note that the type parameter is unbounded. However, //for these algorithms to work correctly, the data objects must //be compared using the method compareTo and equals. //In other words, the classes implementing the list objects //must implement the interface Comparable. The type parameter T //is unbounded because we would like to use these algorithms to //work on an array of objects as well as on objects of the classes //UnorderedArrayList and...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • plz solve Modify binary search so that it always returns the element with the smallest index...

    plz solve Modify binary search so that it always returns the element with the smallest index * that matches the search element (and still guarantees logarithmic running time). import java.util.Arrays; public class BinarySearch2 { public static int rank(int key, int[] a) { // Array must be sorted. int lo = 0; int hi = a.length - 1; while (lo <= hi) { // Key is in a[lo..hi] or not present. int mid = lo + (hi - lo) / 2;...

  • Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and...

    Copy the following java codes and compile //// HighArray.java //// HighArrayApp.java Study carefully the design and implementation HighArray class and note the attributes and its methods.    Create findAll method which uses linear search algorithm to return all number of occurrences of specified element. /** * find an element from array and returns all number of occurrences of the specified element, returns 0 if the element does not exit. * * @param foundElement   Element to be found */ int findAll(int...

  • You are going to implement Treesort algorithm in C++ to sort string data. Here are the...

    You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...

  • With the following code answer the following questions. describe what happens when the following code is...

    With the following code answer the following questions. describe what happens when the following code is executed: String[] searchMe = {"apple","bear","cat","dog","elephant"}; describe what is being created when this statement executes System.out.println(linearFind("cat",searchMe)); describe the values passed to the method describe how each of the specific values are compared to each other describe when the method stops executing and/or when the loop stops executing describe what is returned to beoutprinted System.out.println(binaryFind("apple",searchMe)); describe the values passed to the method describe how each of...

  • 5. A three-heap with n elements can be stored in an array A, where A[O] contains...

    5. A three-heap with n elements can be stored in an array A, where A[O] contains the root of the tree. a) Draw the three-heap that results from inserting 5, 2, 8, 3, 6, 4, 9, 7, 1 in that order into an initially empty three-heap. You do not need to show the array representation of the heap. You are only required to show the final tree, although if you draw intermediate trees. b) Assuming that elements are placed in...

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
ADVERTISEMENT