Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified file name #1503

Merged
merged 3 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 0 additions & 75 deletions Searching Algorithms/Count Inversions/Program.c

This file was deleted.

28 changes: 28 additions & 0 deletions Searching Algorithms/Count_Inversions/CountInversion.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// C program to Count Inversions in an array
// using nested loop

#include <stdio.h>

// Function to count inversions in the array
int inversionCount(int arr[], int n) {
int invCount = 0;

for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {

// If the current element is greater than the next,
// increment the count
if (arr[i] > arr[j])
invCount++;
}
}
return invCount;
}

int main() {
int arr[] = {4, 3, 2, 1};
int n = sizeof(arr) / sizeof(arr[0]);

printf("%d\n", inversionCount(arr, n));
return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ Explanation: There is no pair of indexes i, j exists in the given array such tha
Time Complexity: O(N*logN), where N = size of the given array.
Reason: We are not changing the merge sort algorithm except by adding a variable to it. So, the time complexity is as same as the merge sort.


Space Complexity: O(N), as in the merge sort We use a temporary array to store elements in sorted order.
Loading