So I was going through different sorting algorithms. But almost all the sorting algorithms require 2 loops to sort the array. The time complexity of Bubble sort & Insert
Sorting an array using single loop (javascript)
var arr = [4,5,2,10,3,7,11,5,1]; for(var i = 1; i < arr.length; i++) { if(arr[i] < arr[i-1]) { arr[i] = arr[i] + arr[i-1]; arr[i-1] = arr[i] - arr[i-1]; arr[i] = arr[i] - arr[i-1]; i=0; } }
output : arr = [1, 2, 3, 4, 5, 5, 7, 10, 11]