---
# System prepended metadata

title: Untitled

---

```javascript=


const arr = [4,-1,3,2...,4,100] // n = 100

const sort = (array) => {
    return array.reduce((arr, current) => {
        const index = arr.findIndex((a) => a > current);
        
        if(index === -1) {
            return [...arr, current];
        }
        
        const head = arr.slice(0, index);
        const tail = arr.slice(index);
        
        return [...head, current, ...tail];
    },[]);
}


// array: [4,3,2,1]
// arr, current
// [],  4
// [4], 3
// [3,4] 2

// array: [3,4,1,2]
// arr, current, index, head, tail
// [], 3
// [3], 4,       -1
// [3,4] 1       0

// array: [1, 4, 2, 3]
// arr, current, index, head, tail
// [], 1
// [1], 4, -1
// [1, 4], 2, 1, [1], [4]
// [1, 2, 4], 3, 2, [1, 2], [4]
// [1,2,3,4,,,,,,,,,,,,,,,,,,,,,,,,,99,100]
// 