Skip to content

Instantly share code, notes, and snippets.

@mustafadalga
Created October 28, 2025 15:50
Show Gist options
  • Select an option

  • Save mustafadalga/a878104bcf50b99af94703948044d3f1 to your computer and use it in GitHub Desktop.

Select an option

Save mustafadalga/a878104bcf50b99af94703948044d3f1 to your computer and use it in GitHub Desktop.
Selection sort is a sorting algorithm that repeatedly scans an unsorted array and with each iteration finds the minimum element to build up a sorted array.
function selectionSort(arr) {
for(let i=0;i<arr.length;i++){
for(let j=i+1;j<arr.length;j++){
if(arr[i]>arr[j]){
const temp=arr[i]
arr[i]=arr[j]
arr[j]=temp
}
}
}
return arr
}
selectionSort([9, 3, 6, 2, 1, 11]); // [1, 2, 3, 6, 9, 11]
selectionSort([12, 16, 14, 1, 2, 3]); // [1, 2, 3, 12, 14, 16]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment