1. Array.prototype.mergeSort = function() {
    2. const rec = (arr) => {
    3. if(arr.length === 1) { return arr };
    4. const mid = Math.floor(arr.length / 2);
    5. const left = arr.slice(0, mid);
    6. const right = arr.slice(mid, arr.length);
    7. const orderLeft = rec(left);
    8. const orderRight = rec(right);
    9. const res = [];
    10. while(orderLeft.length || orderRight.length) {
    11. if(orderLeft.length && orderRight.length) {
    12. res.push(orderLeft[0] < orderRight[0] ? orderLeft.shift() : orderRight.shift())
    13. } else if (orderLeft.length) {
    14. res.push(orderLeft.shift())
    15. } else if (orderRight.length) {
    16. res.push(orderRight.shift())
    17. }
    18. }
    19. return res
    20. }
    21. console.log(this);
    22. rec(this) ;
    23. }
    24. const arr = [5, 4, 3, 2, 1];
    25. arr.mergeSort()