Skip to content

Instantly share code, notes, and snippets.

@ronbeltran
Created April 19, 2012 07:17
Show Gist options
  • Save ronbeltran/2419345 to your computer and use it in GitHub Desktop.
Save ronbeltran/2419345 to your computer and use it in GitHub Desktop.
Inversion
public class Inv
{
public static int array_inversion_count(int array[])
{
int inversion_counter = 0;
for(int p=0;p<array.length;p++)
{
for(int q=0;q<array.length;q++)
{
if((p<q) && (array[q]<array[p]))
inversion_counter += 1;
}
}
if (inversion_counter > 1000000000 )
return -1;
else
return inversion_counter;
}
public static void main(String args[])
{
int array[] = {-1, 6, 3, 4, 7, 4};
System.out.println(array_inversion_count(array));
}//end main
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment