-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminimumSwaps.js
More file actions
31 lines (27 loc) · 785 Bytes
/
minimumSwaps.js
File metadata and controls
31 lines (27 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//given an unsorted list of consecutive integers, determine the amount of swaps necessary to sort the array
let arr1 = [7, 1, 3, 2, 4, 5, 6];
const minimumSwaps = (arr) => {
let sorted = arr.slice().sort((a, b) => a - b);
let ogArr = arr.slice().sort((a, b) => a - b);
let swapCount = 0;
// for (let i = 0; i < arr.length; i++) {
// if (arr[i] > arr[i + 1]) {
// arr[i] = arr[i + 1];
// arr[i + 1] = ogArr[i];
// swapCount++;
// }
// }
arr.sort(function compare(a, b) {
if (a < b) {
return -1;
}
if (a > b) {
swapCount++;
return 1;
}
return 0;
});
return swapCount;
};
console.log(minimumSwaps(arr1));
//hmm maybe swaps is a little different than sorting, as two numbers need to change places, not neccessarily be replaced by something else