Selection Sort
Selection Sort. The selection sort algorithm sorts an array by repeatedly finding the minimum element (considering ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two subarrays in a given array. 1) The subarray which is already sorted. 2) Remaining subarray which is unsorted.
by Om Pandey
HTML
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript Array Sort:- Selection Sort Algo</h2>
<p>Selection sort find smallest an put it first</p>
<p>Click the button to sort the array in ascending order.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
var points = [40, 100, 1, 5, 25, 10];
document.getElementById("demo").innerHTML = points;
function myFunction() {//selection sort find smallest an put it first
let len=points.length;
for(let i=0; i<len; i++){
let pos=i;
for(let j=i+1; j<len; j++){
if(points[j]<points[pos])
pos=j;
}
if(i!=pos){
let temp=points[i];
points[i]=points[pos];
points[pos]=temp;
}
}
//points.sort(function(a, b){return a - b});
document.getElementById("demo").innerHTML = points;
}
</script>
</body>
</html>