1
Sorts an array by repeatedly locating the minimum element of an unsorted part and placing it at the beginning.
Sort the following data from small to large
60 80 95 50 70
Create a TestSelectSort.html with Notepad and open it in your browser.
```js
class SelectSort{
static sort(arrays){
var len = arrays.length -1;
var minIndex; //The index of the selected minimum
for(var i=0; i< len; i++){
minIndex =i;
var minValue = arrays[minIndex];
for(var j=i; j< len; j++){
if(minValue > arrays[j + 1]){
minValue = arrays[j+1];
minIndex = j + 1;
}
}
// The minimum of arrays[i] is replaced by arrays[minIndex]
if(i != minIndex){
var temp = arrays[i];
arrays[i] = arrays[minIndex];
arrays[minIndex] = temp;
}
}
}
}
//testing
var scores = [90, 70, 50, 80, 60, 85];
SelectSort.sort(scores);
for(var i=0; i< scores.length; i++){
document.write(scores[i]+ ",");
}
```
**Results:**
50,60,70,80,95,
Leave a comment in the comments to let me know if your result was the same as mine or if you had any difficulties with the code.
You must log in or register to comment.