Open In App

JavaScript Program for Selection Sort

Last Updated : 20 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

What is Selection Sort in JavaScript?

Selection sort is a simple and efficient algorithm that works on selecting either the smallest or the largest element of the list or array and moving it to the correct position.

Working of Selection Sort

Consider the following array
arr = [ 64, 25, 12, 22, 11]

This array will be sorted in the following steps:

  • For the first iteration, the array will be traversed starting from i = 0 till the last to get the minimum value 11 which will be swapped with the first position i.e. 0 index as shown.

Selection Sort Algorithm | Swapping 1st element with the minimum in array

  • The next iteration will start from index 1 i.e. 25 and search for the minimum value. After iterating the min value will be 12 which like the previous step will be swapped with the 2nd position i.e. 25 and index 1 as shown below.

Selection Sort Algorithm | swapping i=1 with the next minimum element

  • Now starting from index i = 2 i.e 25 it will again traverse and store the index of smallest value of remaining array and again swap it with the ith position i.e. index 2 as shown.

Selection Sort Algorithm | swapping i=3 with the next minimum element

  • Next iteration will start from the index i = 3 and as the smallest value is 25 itself there will be no swapping and similarly remaining iterations will be covered.

Selection Sort Algorithm | swapping i=3 with the next minimum element

  • The final output will be the sorted array after all iterations complete which is given below.

Selection Sort Algorithm | Required sorted array

Example: Here is the implementation of above approach

Javascript




// Function to swap values
function swap(arr,xp,yp){
    [arr[xp],arr[yp]] = [ arr[yp],arr[xp]]
}
  
// Function to implement selection
function selectionSort(arr){
  
    // To get length of array
    let n = arr.length;
      
    // Variable to store index of smallest value
    let min;
      
    // variables to iterate the array
    let i , j;
    
    for( i = 0; i < n-1;++i){
        min = i;
        for(j = i+1; j < n; j++){
            if(arr[j]<arr[min]) min = j;
        }
          
        // Swap if both index are different
        if(min!=i)
        swap(arr,min,i);
    }
}
  
// Input array
const arr = [64, 25, 12, 22, 11];
  
// Display input array
console.log( "Original array: "+ arr)
  
// Sort array using custom selection sort function
selectionSort(arr);
  
// Display output
console.log("After sorting: " +arr);


Output

Original array: 64,25,12,22,11
After sorting: 11,12,22,25,64

Selection sort is a simple algorithm which is easy to understand. It works on the idea selecting the lowest value and fix it at the right place.

The time complexity of the selection sort is O(N2) as it itterate twice (use 2 loops) and the space complexity is O(1) as there is no extra memory space is required.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads