Skip to content

Instantly share code, notes, and snippets.

@kapiljhajhria
Last active January 10, 2020 05:26
Show Gist options
  • Save kapiljhajhria/35fc483457c812aec6db53a7f4897ea9 to your computer and use it in GitHub Desktop.
Save kapiljhajhria/35fc483457c812aec6db53a7f4897ea9 to your computer and use it in GitHub Desktop.
sorting in javascript
function sortIt(givenList){
for(var i=0;i<givenList.length;i++){
var remainingArray = givenList.slice(i)
var minValue=Math.min.apply(i, remainingArray)
var indexOfMinValue = remainingArray.indexOf(minValue)
// console.log(minValue)
var old_index=i+indexOfMinValue
var new_index=i
//swap elements of old and new index
//old index element and new index element, old index is the index of min value element
var temp = givenList[new_index]
givenList[i]=minValue;
givenList[old_index]=temp
// array_move(givenList, old_index, new_index)
}
console.log(givenList)
}
sortIt([2,6,3,4,5,7,8,3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment