What is the time complexity of the following code that determines the number of inversions in an array?
int InvCount(int arr[], int n)
{
int count = 0;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (arr[i] > arr[j])
count++;
return count;
}
int InvCount(int arr[], int n)
{
int count = 0;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
if (arr[i] > arr[j])
count++;
return count;
}A. O(n)
B. O(n log n)
C. O(n2)
D. O(log n)
Answer: Option C

Join The Discussion