Skip to content

Instantly share code, notes, and snippets.

@dmjio
Created April 2, 2012 03:50
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 dmjio/2280551 to your computer and use it in GitHub Desktop.
Save dmjio/2280551 to your computer and use it in GitHub Desktop.
Recursive && Non-Recursive Inversion Finder
public class Program
{
static void Main(string[] args)
{
var list = new int[] { 1, 5, 4, 8, 10, 2, 6, 9, 12, 11, 3, 7 };
var a = InversionCounter(list, 0, 1, 0);
Console.WriteLine(a);
Console.ReadLine();
}
public static int InversionCounter(int[] A, int i, int j, int count)
{
if (j == A.Length)
{ return count; }
if (A[i] > A[j])
{ count++; }
i++; j++;
return InversionCounter(A, i, j, count);
}
}
public class Program
{
static void Main(string[] args)
{
var list = new int[] { 1, 5, 4, 8, 10, 2, 6, 9, 12, 11, 3, 7 };
var a = InversionCounter(list);
Console.WriteLine(a);
Console.ReadLine();
}
public static int InversionCounter(int[] A)
{
int count = 0; int j = 1;
for (int i = 0; j < A.Length; i++)
{
if (A[i] > A[j])
{
count++;
}
j++;
}
return count;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment