Javascript Challenge Sort Array without Built In Step By Step

preview_player
Показать описание
Pretty self explanatory. Its a step by step explanation on how to sort an array without built in sort function.
Рекомендации по теме
Комментарии
Автор

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);

aakashchauhan
Автор

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

aankit
Автор

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
}
}
}

shwethahv
Автор

Excellent code, simple bug that can be fixed easily

mahermadany
Автор

DIdn't know I could vibe to a js sort tutorial

erickgeneric
Автор

Please dont confuse beginners, they might start hating to code

shwethahv
Автор

This doesn't sory negative numbers in an array

kushaldavda
Автор

I dont care how you code but the output is not true

lvchiju