can i sort random array using only one loop without using sort function ??? but can i do the same using one for loop ? how can i do that ? here i'm use nested loop
$(document).ready(function () {
function sortarr(arr) {
for (var i = 0; i < arr.length; i++) {
for (var j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
return arr;
}
console.log(sortarr([10, 18, 4, 5, 9, 6, 16, 12]));
});

console.log(sortarr([10,18,4,5,9,6,16,12]));});what's going on with this line here?one loop. Any comparison sort's time complexity is bounded from below byn * lg ntherefore you can't sort it by justone loop, that would be sorting inntime complexity.O(n*log(n)). The exact implementation in JavaScript is browser-dependent, as it is not defined in the spec.