Question

Q. Give a divide- and- conquer algorithm that computes the number of inversions in array A...

Q. Give a divide- and- conquer algorithm that computes the number of inversions in array A in O(n log n) time. Show that your algorithm takes O(n log n).

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

`Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

Suppose we know the number of inversions in the left half and right half of the array (let be inv1 and inv2), what kinds of inversions are not accounted for in Inv1 + Inv2? The answer is – the inversions we have to count during the merge step. Therefore, to get number of inversions, we need to add number of inversions in left subarray, right subarray and merge().

How to get number of inversions in merge()?
In merge process, let i is used for indexing left sub-array and j for right sub-array. At any step in merge(), if a[i] is greater than equal to 2*a[j], then there are (mid – i) inversions. because left and right subarrays are sorted, so all the remaining elements in left-subarray (a[i+1], a[i+2] … a[mid]) will be greater than a[j]

Since we know that merge sort takes time T(n)=O(n*log(n)). The above algo basically modifies merge sort. So, it also takes O(nlog(n))

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Q. Give a divide- and- conquer algorithm that computes the number of inversions in array A...
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
  • 2. Using Python, implement the Divide-and-Conquer algorithm to count the number of inversions between two arrays....

    2. Using Python, implement the Divide-and-Conquer algorithm to count the number of inversions between two arrays. The algorithm is based on Mergesort and counts the inversions while merging the sorted lists.

  • 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...

  • 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.

  • ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real...

    ALGORITHM PROBLEM: A) Significant Inversions: We are given a sequence of n arbitrary but distinct real numbers <a1 , a2 ,..., an>. We define a significant inversion to be a pair i < j such that ai > 2 aj . Design and analyze an O(n log n) time algorithm to count the number of significant inversions in the given sequence. [Hint: Use divide-&-conquer. Do the “combine” step carefully] B) The Maximum-Sum Monotone Sub-Array Problem: Input: An array A[1..n] of...

  • Algorithm Problem - Counting Number of Inversions

    We are given a sequence ofndistinct numbers a1,...,an. We define an inversion to be a pair i<j such that ai>aj. Give an O(nlogn) algorithm to count the number of inversions. (Hint: assume that your algorithmreturns a sorted array of input sequences, as well as the number of inversions. Divide the input sequencesinto two halves, then count inversions and sort each half by doing recursive calls, and find a way to mergethese two sorted halves and to count the number of...

  • Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a...

    Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing and combining steps take a time in Θ(n n). Write a recurrence equation for the running time T (n) , and solve the equation for T (n) 2. Suppose that, in a divide-and-conquer algorithm, we always divide an instance of size n of a problem into 5 sub-instances of size n/3, and the dividing...

  • Design a divide-and-conquer algorithm for computing the number of levels in a binary tree. In particular,...

    Design a divide-and-conquer algorithm for computing the number of levels in a binary tree. In particular, the algorithm should return 0 and 1 for the empty and single-node trees respectively. Please provide the pseudocode for your algorithm. What is the running time of your algorithm in the worst case using O() notation? Design a divide-and-conquer algorithm for computing the number of levels in a COMPLETE binary tree. In particular, the algorithm should return 0 and 1 for the empty and...

  • Analysis Divide & Conquer: Analyze the complexity of algorithm A1 where the problem of size n...

    Analysis Divide & Conquer: Analyze the complexity of algorithm A1 where the problem of size n is solved by dividing into 4 subprograms of size n - 4 to be recursively solved and then combining the solutions of the subprograms takes O(n2) time. Determine the recurrence and whether it is “Subtract and Conquer” or “Divide and Conquer“ type of problem. Solve the problem to the big O notation. Use the master theorem to solve, state which theorem you are using...

  • Design a divide-and-conquer algorithm in pseudocode for computing the number of levels in a binary tree....

    Design a divide-and-conquer algorithm in pseudocode for computing the number of levels in a binary tree. In particular, your algorithm must return 0 and 1 for the empty and single-node trees, respectively. What is the time efficiency class of your algorithm?

  • Suppose   that,   in   a   divide-and-conquer   algorithm,   we   always   divide   an instance   of   size   n   of &nb

    Suppose   that,   in   a   divide-and-conquer   algorithm,   we   always   divide   an instance   of   size   n   of   a   problem   into   n   subinstances   of   size   n/3,   and   the dividing   and   combining   steps   take   linear   time.   Write   a   recurrence   equation for   the   running   time   T(n),   and   solve   this   recurrence   equation   for   T(n).   Show your   solution   in   order   notation. please help solve this..

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