Skip to content

Instantly share code, notes, and snippets.

@dhonig
Created April 17, 2017 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhonig/09fe85af1a8f8dc38518499e100e53c7 to your computer and use it in GitHub Desktop.
Save dhonig/09fe85af1a8f8dc38518499e100e53c7 to your computer and use it in GitHub Desktop.
Linear version of find all integer inversions
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