Created
April 17, 2017 02:21
-
-
Save dhonig/09fe85af1a8f8dc38518499e100e53c7 to your computer and use it in GitHub Desktop.
Linear version of find all integer inversions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getInversionCount(arr){ | |
var inv_count = 0; | |
var n=arr.length | |
for (var i = 0; i < n - 1; i++) | |
for (var j = i+1; j < n; j++) | |
if (arr[i] > arr[j]) | |
inv_count++; | |
return inv_count; | |
} | |
var array=[1,5,4,3,6,12,7,13,8]; | |
var result=getInversionCount(array); | |
console.log("result "+result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment