Answer the question
In order to leave comments, you need to log in
What is sorting (JS)?
Greetings!
I tried to make a quick sort (quick sort) without really understanding its theory, I realized that I used not quite the right approach. Here is the code:
const inputArray = [7, 4, 2, 8, 6, 1, 0, 9, 5, 3];
//my version
function sort(arr) {
if (arr.length < 2) {
return arr;
}
const baseElIndex = Math.floor(arr.length / 2);
const baseElValue = arr[baseElIndex];
const arrMin = [];
const arrSame = [];
const arrMax = [];
arr.forEach(item => {
if (item == baseElValue) {
arrSame.push(item);
} else if (item < baseElValue) {
arrMin.push(item);
} else if (item > baseElValue) {
arrMax.push(item);
}
});
return sort(arrMin)
.concat(arrSame)
.concat(sort(arrMax));
}
console.log(sort(inputArray));
> node .\sort.js
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Answer the question
In order to leave comments, you need to log in
not really understanding her theoryIt sounds funny.
This is Merge Sort
Python Example
https://studlearn.com/works/details/sortirovka-sli...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question