Комментарии:
Excellent code, simple bug that can be fixed easily
ОтветитьI dont care how you code but the output is not true
ОтветитьDont code 💩
ОтветитьThis doesn't sory negative numbers in an array
ОтветитьPlease dont confuse beginners, they might start hating to code
Ответить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
}
}
}
DIdn't know I could vibe to a js sort tutorial
Ответить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);
let arr = [4, 3, 2, 1, 5, 8, 7, 2];
function arrSort(arrName) {
for (let i = 0, n = arrName.length; i < n; i++) {
for (let ii = 0; ii < n; ii++) {
if (arrName[i] < arrName[ii]) {
let arajin = arrName[i];
let verjin = arrName[ii];
arrName[i] = verjin;
arrName[ii] = arajin;
}else{
continue;
}
}
}
}
arrSort(arr);
console.log(arr);
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
Ответить