☰

Console Bubble Sort Visualizer

A simple bubble sort script that runs in the browser console for demonstration purposes.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Console Bubble Sort Visualizer
// @namespace    http://tampermonkey.net
// @version      1.0
// @description  A simple bubble sort script that runs in the browser console for demonstration purposes.
// @author       Your Name
// @match        *://*://*
// @grant        none
// @license riso
// ==/UserScript==

(function() {
    'use strict';

    /**
     * Standard Bubble Sort with console visualization
     * @param {Array} arr - The array to be sorted
     */
    function bubbleSort(arr) {
        let len = arr.length;
        console.log("%c[Bubble Sort] Original Array:", "color: #2196F3; font-weight: bold;", [...arr]);

        for (let i = 0; i < len - 1; i++) {
            let swapped = false;
            
            for (let j = 0; j < len - 1 - i; j++) {
                if (arr[j] > arr[j + 1]) {
                    // Swap elements using destructuring assignment
                    [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]];
                    swapped = true;
                }
            }
            
            console.log(`[Round ${i + 1}] State:`, [...arr]);
            
            // Optimization: If no two elements were swapped by inner loop, then break
            if (!swapped) {
                console.log("%c[Bubble Sort] Array is already sorted early!", "color: #4CAF50;");
                break;
            }
        }
        
        console.log("%c[Bubble Sort] Sorted Array:", "color: #4CAF50; font-weight: bold;", arr);
        return arr;
    }

    // Run the sorting algorithm automatically when the script loads
    const targetArray =[];
    bubbleSort(targetArray);
})();