From e2a64020e329ea45f1713723b35b77622b61b32a Mon Sep 17 00:00:00 2001 From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com> Date: Tue, 29 Oct 2024 22:33:39 +0530 Subject: [PATCH 1/3] Made the modification #1047 --- .../Count Inversions/Program.c | 75 ------------------- .../Count Inversions/Program_count.c | 28 +++++++ .../Count Inversions/Readme.md | 3 +- 3 files changed, 30 insertions(+), 76 deletions(-) delete mode 100644 Searching Algorithms/Count Inversions/Program.c create mode 100644 Searching Algorithms/Count Inversions/Program_count.c diff --git a/Searching Algorithms/Count Inversions/Program.c b/Searching Algorithms/Count Inversions/Program.c deleted file mode 100644 index 5cfc1a20..00000000 --- a/Searching Algorithms/Count Inversions/Program.c +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include - -using namespace std; - -int merge(vector &arr, int low, int mid, int high) { - vector temp; // temporary array - int left = low; // starting index of left half of arr - int right = mid + 1; // starting index of right half of arr - - //Modification 1: count variable to count the pairs: - int count = 0; - - //storing elements in the temporary array in a sorted manner// - - while (left <= mid && right <= high) { - if (arr[left] <= arr[right]) { - temp.push_back(arr[left]); - left++; - } - else { - temp.push_back(arr[right]); - count += (mid - left + 1); //Modification 2 - right++; - } - } - - // if elements on the left half are still left // - - while (left <= mid) { - temp.push_back(arr[left]); - left++; - } - - // if elements on the right half are still left // - while (right <= high) { - temp.push_back(arr[right]); - right++; - } - - // transfering all elements from temporary to arr // - for (int i = low; i <= high; i++) { - arr[i] = temp[i - low]; - } - - return count; // Modification 3 -} - -int mergeSort(vector &arr, int low, int high) { - int count = 0; - if (low >= high) return count; - int mid = (low + high) / 2 ; - count += mergeSort(arr, low, mid); // left half - count += mergeSort(arr, mid + 1, high); // right half - count += merge(arr, low, mid, high); // merging sorted halves - return count; -} - -int numberOfInversions(vector&a, int n) { - - // Count the number of pairs: - return mergeSort(a, 0, n - 1); -} - -int main() -{ - vector a = {3,5,1,10,9,2,6,8}; - int n = 5; - int count = numberOfInversions(a, n); - cout << "The number of inversions are: " - << count << endl; - return 0; -} - - diff --git a/Searching Algorithms/Count Inversions/Program_count.c b/Searching Algorithms/Count Inversions/Program_count.c new file mode 100644 index 00000000..ed3606d7 --- /dev/null +++ b/Searching Algorithms/Count Inversions/Program_count.c @@ -0,0 +1,28 @@ +// C program to Count Inversions in an array +// using nested loop + +#include + +// 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; +} \ No newline at end of file diff --git a/Searching Algorithms/Count Inversions/Readme.md b/Searching Algorithms/Count Inversions/Readme.md index ea25fe45..3d57cff0 100644 --- a/Searching Algorithms/Count Inversions/Readme.md +++ b/Searching Algorithms/Count Inversions/Readme.md @@ -31,8 +31,9 @@ Input: arr[] = {5,4,3,2,1} Output: 10 Explanation: There is no pair of indexes i, j exists in the given array such that arr[i] > arr[j] and i < j -## Time Complexity and Space Complexity +## Time Complexity 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 Space Complexity: O(N), as in the merge sort We use a temporary array to store elements in sorted order. From abda752ee250ffe42d06e60b86e804ef62afe256 Mon Sep 17 00:00:00 2001 From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com> Date: Tue, 29 Oct 2024 22:37:16 +0530 Subject: [PATCH 2/3] Update Readme.md --- Searching Algorithms/Count Inversions/Readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Searching Algorithms/Count Inversions/Readme.md b/Searching Algorithms/Count Inversions/Readme.md index 3d57cff0..bdad98f6 100644 --- a/Searching Algorithms/Count Inversions/Readme.md +++ b/Searching Algorithms/Count Inversions/Readme.md @@ -31,9 +31,9 @@ Input: arr[] = {5,4,3,2,1} Output: 10 Explanation: There is no pair of indexes i, j exists in the given array such that arr[i] > arr[j] and i < j -## Time Complexity +## Time Complexity and Space Complexity 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 + Space Complexity: O(N), as in the merge sort We use a temporary array to store elements in sorted order. From 9f1040ab262895eceffc9d4b4a4ce7cbbc14c79f Mon Sep 17 00:00:00 2001 From: Dipanita45 <132455672+Dipanita45@users.noreply.github.com> Date: Wed, 30 Oct 2024 19:41:24 +0530 Subject: [PATCH 3/3] Made the changes in file name #1047 --- .../Program_count.c => Count_Inversions/CountInversion.c} | 0 .../{Count Inversions => Count_Inversions}/Readme.md | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename Searching Algorithms/{Count Inversions/Program_count.c => Count_Inversions/CountInversion.c} (100%) rename Searching Algorithms/{Count Inversions => Count_Inversions}/Readme.md (100%) diff --git a/Searching Algorithms/Count Inversions/Program_count.c b/Searching Algorithms/Count_Inversions/CountInversion.c similarity index 100% rename from Searching Algorithms/Count Inversions/Program_count.c rename to Searching Algorithms/Count_Inversions/CountInversion.c diff --git a/Searching Algorithms/Count Inversions/Readme.md b/Searching Algorithms/Count_Inversions/Readme.md similarity index 100% rename from Searching Algorithms/Count Inversions/Readme.md rename to Searching Algorithms/Count_Inversions/Readme.md