DEV Community

Cover image for Day 33: Stooge Sort
Matt Ryan
Matt Ryan

Posted on

2 2

Day 33: Stooge Sort

Alt Text

function day33 (array, i, j) {
    if (j === undefined) {
        j = array.length - 1;
    }

    if (i === undefined) {
        i = 0;
    }

    if (array[j] < array[i]) {
        var aux = array[i];
        array[i] = array[j];
        array[j] = aux;
    }

    if (j - i > 1) {
        var t = Math.floor((j - i + 1) / 3);
        day33(array, i, j-t);
        day33(array, i+t, j);
        day33(array, i, j-t);
    }
};
Enter fullscreen mode Exit fullscreen mode

Console:

arr = [13,9,60,43,2,39,54,87];
day33(arr);
console.log(arr);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

This post blew up on DEV in 2020:

js visualized

🚀⚙️ JavaScript Visualized: the JavaScript Engine

As JavaScript devs, we usually don't have to deal with compilers ourselves. However, it's definitely good to know the basics of the JavaScript engine and see how it handles our human-friendly JS code, and turns it into something machines understand! 🥳

Happy coding!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay