mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-05 03:29:46 +08:00
fix dynamic array issues in sorting folder
This commit is contained in:
@@ -10,7 +10,7 @@ int minSwaps(int arr[], int n) {
|
||||
// Create an array of pairs where first
|
||||
// element is array element and second element
|
||||
// is position of first element
|
||||
std::pair<int, int> arrPos[n];
|
||||
std::pair<int, int> *arrPos = new std::pair<int, int>[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
arrPos[i].first = arr[i];
|
||||
arrPos[i].second = i;
|
||||
@@ -32,8 +32,7 @@ int minSwaps(int arr[], int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
// already swapped and corrected or
|
||||
// already present at correct pos
|
||||
if (vis[i] || arrPos[i].second == i)
|
||||
continue;
|
||||
if (vis[i] || arrPos[i].second == i) continue;
|
||||
|
||||
// find out the number of node in
|
||||
// this cycle and add in ans
|
||||
@@ -53,6 +52,8 @@ int minSwaps(int arr[], int n) {
|
||||
}
|
||||
}
|
||||
|
||||
delete[] arrPos;
|
||||
|
||||
// Return result
|
||||
return ans;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user