0% found this document useful (0 votes)
82 views6 pages

Divide and Conquer: Coun - NG Inversions I

The document discusses an algorithm for counting inversions in an array. An inversion is a pair of indices (i,j) where i < j but the element at i has a larger value than the element at j. The algorithm uses a divide and conquer approach. It recursively counts the inversions within the first and second halves of the array, and also counts split inversions where one index is in the first half and the other is in the second half, achieving an overall runtime of O(n log n).

Uploaded by

Javier Alvarez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views6 pages

Divide and Conquer: Coun - NG Inversions I

The document discusses an algorithm for counting inversions in an array. An inversion is a pair of indices (i,j) where i < j but the element at i has a larger value than the element at j. The algorithm uses a divide and conquer approach. It recursively counts the inversions within the first and second halves of the array, and also counts split inversions where one index is in the first half and the other is in the second half, achieving an overall runtime of O(n log n).

Uploaded by

Javier Alvarez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Divide

 and  
Conquer  
Coun.ng  
Design  and  Analysis  
Inversions  I  
of  Algorithms  I  
The  Problem  
Input  :  array  A  containing  the  numbers  1,2,3,..,n  in  
some  arbitrary  order  

Output  :  number  of  inversions  =  number  of  pairs  (i,j)  


of  array  indices  with  i<j  and  A[i]  >  A[j]  

Tim  Roughgarden  
Examples  and  Mo.va.on  
Example  

Inversions  :  
       (3,2),        (5,2),        (5,4)  

Mo.va.on  :  numerical  
similarity  measure  
between  two  ranked  lists   eg:  for  collabora.ve  filtering  
Tim  Roughgarden  
Tem
ver

What  is  the  largest-­‐possible  number  of  inversions  that  a  6-­‐


element  array  can  have?  
15   In  general,    
21  
High-­‐Level  Approach  
Brute-­‐force  :            .me  
Can  we  do  be^er  ?        Yes!  

KEY  IDEA  #  1    :    Divide  +  Conquer  

Call  an  inversion  (i,j)    [with  i<j]  


 LeW  :  if  i,j  <  n/2   Note  :  can  compute  these  
recursively  
 Right  :  if  i,j  >  n/2   need    separate  subrou.ne  for  
 Split  :  if  i<=n/2  <  j   these  
Tim  Roughgarden  
High-­‐Level  Algorithm  
Count  (array  A,  length  n)  
 if  n=1,  return  0  
 else  
   X  =  Count  (1st  half  of  A,  n/2)  
   Y  =  Count  (2nd  half  of  A,  n/2_  
   Z  =  CountSplitInv(A,n)  
 return      x+y+z  
Goal  :  implement  CountSplitInv  in  linear  (O(n))  .me  then  
count  will  run  in  O(nlog(n))  .me  [just  like  Merge  Sort]  
Tim  Roughgarden  

You might also like