Amazon Online Assessment (OA) 2021 - Number of Swaps to Sort
Given an array and a sorting algorithm, the sorting algorithm will do a selection swap. Find the number of swaps to sort the array.
Example 1:
Input: [5, 4, 1, 2]
Output: 5
Explanation:
- Swap index
0
with1
to form the sorted array[4, 5, 1, 2]
. - Swap index
0
with2
to form the sorted array[1, 5, 4, 2]
. - Swap index
1
with2
to form the sorted array[1, 4, 5, 2]
. - Swap index
1
with3
to form the sorted array[1, 2, 5, 4]
. - Swap index
2
with3
to form the sorted array[1, 2, 4, 5]
.
Solution
Loading full content...