brewersfan1976

count_inversions.rb

Mar 11th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.44 KB | None | 0 0
  1. def countInversionsNaive(inputArray)
  2.     array_length = inputArray.length
  3.     x = 0
  4.     y = 1
  5.     count = 0
  6.    
  7.     while (x < array_length)
  8.         while (y < array_length)
  9.             if inputArray[x] > inputArray[y] and x < y then
  10.                count = count + 1
  11.             end
  12.                
  13.             y = y + 1
  14.          end
  15.            
  16.          x = x + 1
  17.          y = x + 1
  18.      end
  19.        
  20.      return count
  21. end
Advertisement
Add Comment
Please, Sign In to add comment