C++程式:計算給定陣列中大小為三的反轉次數
反轉計數是一種步數計數方法,我們可以用它來計算特定陣列的排序步驟數。它也可以計算陣列的操作時間跨度。但是,如果我們想以相反的方式排序陣列,則計數將是該陣列中存在的最大數字。
Array: { 5, 4, 3, 2, 1} // for the reverse manner Pairs: {5, 4}, {5,3} , {3,2}, {3,1}, {2,1},{4,3}, {4,2}, {4,1},}, {5,2}, {5,1} Output: 10 Array: {1, 2, 3, 4, 5} // for the increasing manner Pairs: No Pairs Output: 0 Array: {1,5,2,8,3,4} Pairs: {5, 2}, {5, 3}, {5, 4}, {8, 3}, {8, 4} Output: 5
反轉計數表明特定陣列與按升序排序的距離有多遠。這裡有兩個具體的流程來描述這種情況以及解決方案:
查詢較小的元素 - 要從陣列中找出較小的元素,我們需要從 n-1 到 0 迭代索引。透過應用 (a[i]-1),我們可以在這裡計算 getSum()。該過程將一直執行到 a[i]-1。
查詢更大的數字 - 要從索引中查詢更大的數字,我們需要執行從 0 到 n-1 的迭代。對於每個元素,我們需要對每個數字進行計算,直到 a[i]。從中減去 i。然後我們將得到一個大於 a[i] 的數字。
計算陣列中大小為三的反轉次數的演算法:
在這個演算法中;我們學習如何在特定的程式設計環境中計算給定陣列中大小為三的反轉次數。
步驟 1 - 開始
步驟 2 - 宣告一個數組和反轉計數 (作為 arr[] --> 陣列和 invCount --> 反轉計數)
步驟 3 - 內迴圈 y=x+1 到 N
步驟 4 - 如果 x 處的元素大於 y 索引處的元素
步驟 5 - 然後,增加 invCount++
步驟 6 - 列印對
步驟 7 - 終止
計算陣列中大小為三的反轉次數的語法
如果滿足以下條件,則一對 (A[i], A[j]) 被認為處於反轉狀態:A[i] > A[j] 且 i < j
C++ 實現
int getInversions(int * A, int n) { int count = 0; for (int i = 0; i < n; ++i) { for (int j = i + 1; j < n; ++j) { if (A[i] > a[j]) { ++count; } } } return count; }
Java 實現
public static int getInversions(int[] A, int n) { int count = 0; for (int i = 0; i < n; i++) { for (int j = i + 1; j < n; j++) { if (A[i] > A[j]) { count += 1; } } } return count; }
Python 實現
def getInversions(A, n): count = 0 for i in range(n): for j in range(i + 1, n): if A[i] > A[j]: count += 1 return count; }
這裡我們提到了在給定陣列中計算大小為三的反轉次數的可能語法。對於此方法;時間複雜度為 O(N^2),其中 N 是陣列的總大小;空間複雜度:O(1),因為沒有使用額外的空間。
遵循的方法
方法 1 - 透過程式計算給定陣列中大小為 3 的反轉次數來計算給定陣列中大小為三的反轉次數
方法 2 - 更好的方法來計算大小為 3 的反轉次數
方法 3 - 使用二進位制索引樹計算大小為 3 的反轉次數
透過程式計算給定陣列中大小為 3 的反轉次數來計算給定陣列中大小為三的反轉次數
對於計算大小為三的反轉次數的簡單方法,我們需要為 i、j 和 k 的所有可能值執行迴圈。時間複雜度為 O(n^3),O(1) 反映輔助空間。
條件是:
a[i] > a[j] > a[k] 且 i < j < k。
示例 1
#include<bits/stdc++.h> using namespace std; int getInvCount(int arr[],int n){ int invcount = 0; for (int a=0; a<n-2; a++){ for (int j=a+1; j<n-1; j++) { if (arr[a]>arr[j]) { for (int k=j+1; k<n; k++) { if (arr[j]>arr[k]) invcount++; } } } } return invcount; } int main(){ int arr[] = {7, 10, 1, 97}; int n = sizeof(arr)/sizeof(arr[0]); cout << "Inversion Count : " << getInvCount(arr, n); return 0; }
輸出
Inversion count after method: 0
更好的方法來計算大小為 3 的反轉次數
在這種方法中,我們將陣列的每個元素都視為反轉的中間元素。這有助於降低複雜度。對於這種方法,時間複雜度為 O(n^2),輔助空間為 O(1)。
示例 2
#include<bits/stdc++.h> using namespace std; int getInvCount(int arr[], int n){ int invcount = 0; for (int i=1; i<n-1; i++){ int small = 0; for (int j=i+1; j<n; j++) if (arr[i] > arr[j]) small++; int great = 0; for (int j=i-1; j>=0; j--) if (arr[i] < arr[j]) great++; invcount += great*small; } return invcount; } int main(){ int arr[] = {8, 4, 2, 1}; int n = sizeof(arr)/sizeof(arr[0]); cout << "Inversion Count After The Method: " << getInvCount(arr, n); return 0; }
輸出
Inversion Count After The Method: 4
使用二進位制索引樹計算大小為 3 的反轉次數
在這種方法中,我們也計算較大和較小的元素。然後執行 greater[] 與 smaller[] 的乘法運算,並將其新增到最終結果中。這裡的時間複雜度為 O(n*log(n)),輔助空間表示為 O(n)。
示例 3
import java.io.*; import java.util.Arrays; import java.util.ArrayList; import java.lang.*; import java.util.Collections; public class rudrabytp { static int N = 100005; static int BIT[][] = new int[4][N]; static void updateBIT(int t, int i, int val, int n) { while (i <= n) { BIT[t][i] = BIT[t][i] + val; i = i + (i & (-i)); } } static int getSum(int t, int i) { int res = 0; while (i > 0) { res = res + BIT[t][i]; i = i - (i & (-i)); } return res; } static void convert(int arr[], int n){ int temp[]=new int[n]; for (int i = 0; i < n; i++) temp[i] = arr[i]; Arrays.sort(temp); for (int i = 0; i < n; i++) { arr[i] = Arrays.binarySearch(temp,arr[i]) + 1; } } public static int getInvCount(int arr[], int n) { convert(arr, n); for (int i = n - 1; i >= 0; i--) { updateBIT(1, arr[i], 1, n); for (int l = 1; l < 3; l++) { updateBIT(l + 1, arr[i], getSum(l, arr[i] - 1), n); } } return getSum(3, n); } public static void main (String[] args) { int arr[] = {8, 4, 2, 1}; int n = arr.length; System.out.print("Inversion Count After The Operation : "+getInvCount(arr,n)); } }
輸出
Inversion Count After The Operation : 4
結論
在本文中,我們今天學習瞭如何在給定陣列中計算大小為三的反轉次數。希望透過本文和使用特定語言提到的程式碼,您已經對這個主題有了廣泛的瞭解。