Approach 2 : Using Map() method

In this approach, we calculates the minimum swaps required to sort an input array. It uses a Map to track element positions, then iterates through the array, swapping elements to their correct positions. The total swaps are counted, and the sorted array is logged. This approach ensures elements are in ascending order with the fewest swaps.

Example:

Javascript
const arr = [9, 5, 8, 2, 1, 6, 3, 4, 7];
const elementToIndex = new Map();

for (const [index, value] of arr.entries()) {
    elementToIndex.set(value, index);
}

let swaps = 0;

for (const [i, element] of arr.entries()) {
    if (element !== i + 1) {
        const correctElement = i + 1;
        const currentElement = element;

        // Swap the elements
        [arr[i], arr[elementToIndex.get(correctElement)]] =
            [correctElement, currentElement];

        
        elementToIndex.set(
            currentElement, elementToIndex.get(correctElement));
        elementToIndex.set(correctElement, i);

        swaps++;
    }
}

console.log("Minimum swaps required:", swaps);
console.log("Sorted array:", arr);

Output
Minimum swaps required: 7
Sorted array: [
  1, 2, 3, 4, 5,
  6, 7, 8, 9
]

Time Complexity: O(n) for iterating the array once.

Space Complexity: O(n) for using a Map data structure.

JavaScript Program to Find Minimum Number of Swaps to Sort Array

The minimum number of swaps to sort an array is the smallest quantity of element swaps needed to arrange the array in ascending or descending order. This minimizes the number of exchanges and is a key metric in sorting algorithms, as reducing swaps often improves efficiency and performance when organizing data structures.

Table of Content

  • Approach 1: Using nested loop
  • Approach 2 : Using Map() method
  • Approach 3: Using Bubble Sort
  • Approach 4: Compare the original array with a sorted copy
  • Approach 5: Cycle Detection in Graph

Similar Reads

Approach 1: Using nested loop

In this approach, we use loops with the help of the function swapsToSort, which calculates the minimum number of swaps needed to sort an array. It uses a loop to swap elements to their correct positions until the array is sorted, counting swaps. The sorted array and swap count are then printed....

Approach 2 : Using Map() method

In this approach, we calculates the minimum swaps required to sort an input array. It uses a Map to track element positions, then iterates through the array, swapping elements to their correct positions. The total swaps are counted, and the sorted array is logged. This approach ensures elements are in ascending order with the fewest swaps....

Approach 3: Using Bubble Sort

In the Bubble Sort approach to find the minimum number of swaps, perform the bubble sort algorithm on the array while counting the number of swaps needed. The total swaps performed represent the minimum number of swaps required to sort the array....

Approach 4: Compare the original array with a sorted copy

In this approach, we finds the minimum number of swaps needed to sort an array. It uses a sorted copy to compare elements, a swap function to rearrange them, and an indexOf function to find the correct position of elements. The final sorted array is displayed....

Approach 5: Cycle Detection in Graph

In this approach, the minimum number of swaps required to sort an array is calculated using the concept of cycle detection in graphs. This method involves creating a graph where each node represents an element in the array, and there is an edge between nodes if the elements need to be swapped. By detecting cycles in this graph, we can determine the number of swaps needed to sort the array....