fix dynamic array issues in sorting folder

This commit is contained in:
Krishna Vedala
2020-05-26 13:02:10 -04:00
parent 717f5c8f01
commit 01b69fcb24
9 changed files with 256 additions and 311 deletions

View File

@@ -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;
}