<commit_before>'use strict';

const Srtr = function() {};

/**
 * Fallback sort predicate. Perform ternary comparsion of numbers.
 *
 * TODO: Explain all this.
 * TODO: Document spaceship (<=>) operator.
 *
 * @example
 *
 *      2 <=> 3 // -1
 *      3 <=> 3 //  0
 *      3 <=> 2 //  1
 *
 * @example
 *
 *      10 - 7  //  3
 *      10 - 10 //  0
 *      10 - 13 // -3
 *
 * @private
 * @see https://en.wikipedia.org/wiki/Three-way_comparison
 * @see https://en.wikipedia.org/wiki/%3F:
 * @param {number} value - Current value in collection.
 * @param {number} comparison - Comparison value in collection.
 * @return {number} - Next value subtracted from current.
 */

function spaceship(value, comparison) {
    return value < comparison ? -1 : value === comparison ? 0 : 1;
}

/**
 * Use recursive quicksort to reorder a collection.
 *
 * TODO: Document this for newbies.
 *
 * @public
 * @see https://en.wikipedia.org/wiki/Quicksort
 * @param {array} collection - Unsorted collecton.
 * @return {array} collection - Sorted collecton.
 */

Srtr.prototype.quicksort = function(collection, predicate = spaceship) {
    /* eslint no-magic-numbers: "off" */
    if (collection.length < 2) {
        return collection;
    }

    const pivot = collection[collection.length >> 1];

    return [
        ...this.quicksort(collection.filter(element => predicate(element, pivot) < 0), predicate),
        ...collection.filter(element => predicate(element, pivot) === 0),
        ...this.quicksort(collection.filter(element => predicate(element, pivot) > 0), predicate)
    ];
};

/**
 * Use iterative bubble sort to reorder a collection.
 *
 * TODO: Document this for newbies.
 *
 * @public
 * @see https://en.wikipedia.org/wiki/Bubble_sort
 * @param {array} collection - Unsorted collection
 * @param {function} predicate - Predicate function to test values.
 * @return {array} collection - Sorted collection.
 */

Srtr.prototype.bubblesort = function(collection, predicate = spaceship) {
    if (collection.length < 2) {
        return collection;
    }

    collection = collection.slice();

    for (let index = 0; index < collection.length - 1; index++) {
        if (parseInt(predicate(collection[index], collection[index + 1])) > 0) {
            [collection[index], collection[index + 1]] = [collection[index + 1], collection[index]];
            index = 0;
        }
    }

    return collection;
};

module.exports = Object.create(Srtr.prototype);
<commit_msg>Added explanation for quicksort algorithm.<commit_after>'use strict';
/* eslint no-magic-numbers: "off" */

const Srtr = function() {};

/**
 * Fallback sort predicate. Perform ternary comparsion of numbers.
 *
 * TODO: Explain all this.
 * TODO: Document spaceship (<=>) operator.
 *
 * @example
 *
 *      2 <=> 3 // -1
 *      3 <=> 3 //  0
 *      3 <=> 2 //  1
 *
 * @example
 *
 *      10 - 7  //  3
 *      10 - 10 //  0
 *      10 - 13 // -3
 *
 * @private
 * @see https://en.wikipedia.org/wiki/Three-way_comparison
 * @see https://en.wikipedia.org/wiki/%3F:
 * @param {number} value - Current value in collection.
 * @param {number} comparison - Comparison value in collection.
 * @return {number} - Next value subtracted from current.
 */

function spaceship(value, comparison) {
    return value < comparison ? -1 : value === comparison ? 0 : 1;
}

/**
 * Use recursive quicksort to reorder a collection. Quicksort works by:
 *
 *  1. Identifying a pivot point (the midpoint rounded down in this case) in the
 *     array. Other implementations apply more intelligence or start at tne end.
 *  2. Think of a left-to-right arrangement:
 *        a. Values smaller than the pivot go into a new array put before the pivot.
 *        b. Values larger than the pivot go into a new array put after the pivot.
 *  3. Repeat this recursively with each sub array until arrays of a single
 *     element in size.
 *  4. Merge and return the array.
 *
 * @example
 *
 *      [0, 9, 2, 7, 5, 1, 10]
 *      [0, 2, 5, 1] [7] [9, 10]
 *      [0, 1] [2] [5] [7] [9, 10]
 *      [0] [1] [2] [5] [7] [9] [10]
 *      [0, 1, 2, 5, 7, 9, 10]
 *
 * @public
 * @see https://en.wikipedia.org/wiki/Quicksort
 * @param {array} collection - Unsorted collecton.
 * @return {array} collection - Sorted collecton.
 */

Srtr.prototype.quicksort = function(collection, predicate = spaceship) {
    if (collection.length < 2) {
        return collection;
    }

    const pivot = collection[collection.length >> 1];

    return [
        ...this.quicksort(collection.filter(element => predicate(element, pivot) < 0), predicate),
        ...collection.filter(element => predicate(element, pivot) === 0),
        ...this.quicksort(collection.filter(element => predicate(element, pivot) > 0), predicate)
    ];
};

/**
 * Use iterative bubble sort to reorder a collection.
 *
 * TODO: Document this for newbies.
 *
 * @public
 * @see https://en.wikipedia.org/wiki/Bubble_sort
 * @param {array} collection - Unsorted collection
 * @param {function} predicate - Predicate function to test values.
 * @return {array} collection - Sorted collection.
 */

Srtr.prototype.bubblesort = function(collection, predicate = spaceship) {
    if (collection.length < 2) {
        return collection;
    }

    collection = collection.slice();

    for (let index = 0; index < collection.length - 1; index++) {
        if (parseInt(predicate(collection[index], collection[index + 1])) > 0) {
            [collection[index], collection[index + 1]] = [collection[index + 1], collection[index]];
            index = 0;
        }
    }

    return collection;
};

module.exports = Object.create(Srtr.prototype);

{'fraction_non_alphanumeric': 0.14460610361958837, 'fraction_numerical': 0.0220014194464159, 'mean_word_length': 2.73558648111332, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 25, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2284983', 'n_tokens_mistral': 1851, 'n_tokens_neox': 1704, 'n_words': 529}