Created
April 2, 2012 03:50
-
-
Save dmjio/2280551 to your computer and use it in GitHub Desktop.
Recursive && Non-Recursive Inversion Finder
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
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