Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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