Pretty self explanatory. Its a step by step explanation on how to sort an array without built in sort function.
Пікірлер: 15
@a4ankit274 жыл бұрын
Seems like your final result is still not sorted . Is it ? [2,5,3,7,1....] ... how is this sorted ? And the reason for that is, you are putting your if swapped == 0 condition inside the for loop. You need to move that outside
@erickgeneric2 жыл бұрын
DIdn't know I could vibe to a js sort tutorial
@mahermadany6030 Жыл бұрын
Excellent code, simple bug that can be fixed easily
@aakashchauhan963 жыл бұрын
For better output use below code. function Sort(array) { var done = false; while (!done) { done = true; for (var i = 1; i < array.length; i++) { if (array[i - 1] > array[i]) { done = false; var tmp = array[i - 1]; array[i - 1] = array[i]; array[i] = tmp; } } } return array; } var numbers = [121,12, 10, 15,444, 11, 14, 13, 16,1]; Sort(numbers); console.log(numbers);
@shwethahv16842 жыл бұрын
Such a complex code , why cant you use two i and j loop for (let i = 0; i < arr.length; i++) { for (j = i + 1; j < arr.length; j++) { if (arr[i] > arr[j]) { temp = arr[j]; arr[j] = arr[i]; arr[i] = temp } } }
@biswajitghosh3010 Жыл бұрын
just becoz of time complexity its TC is O(n^2)
@hariharan5801 Жыл бұрын
But this gives ascending order na? 🙃
@thiernodem56819 ай бұрын
@@hariharan5801 you just gotta reverse the greater than sign to less than, you will get descending order
@kushaldavda58922 жыл бұрын
This doesn't sory negative numbers in an array
@shwethahv16842 жыл бұрын
Please dont confuse beginners, they might start hating to code
@jorasimg22332 жыл бұрын
I am at beginner level, can you paste a complete code here.
@lv10chiju19 Жыл бұрын
I dont care how you code but the output is not true