Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                
<!-- Fonts -->
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Droid+Sans+Mono|Open+Sans" >

<!-- 
Theme Colors:
https://github.com/lonekorean/gist-syntax-themes/tree/master/stylesheets

Blog: http://codersblock.com/blog/customizing-github-gists/
-->
<link rel="stylesheet" type="text/css" href="https://cdn.rawgit.com/lonekorean/gist-syntax-themes/00d7074c/stylesheets/monokai.css" >

<!-- Gist Link -->
<script src="https://gist.github.com/scriptonian/35210e0d4bc384c3f2e2782becd8d44b.js"></script>
              
            
!

CSS

              
                body .gist .gist-file {
    margin-bottom: 0;
    border: 1px dashed #adb5bd;
    border-radius: 0;
}

body .gist .gist-data {
    border-bottom: none;
    border-radius: 0;
    background-color: #f1f3f5;
}

body .gist .blob-wrapper {
    border-radius: 0;
}

body .gist .highlight {
    font-family: 'Droid Sans Mono', monospace;
    font-size: 14px;
}

body .gist .highlight td {
    padding: 5px 15px !important;
    line-height: 1;
    font-family: inherit;
    font-size: inherit;
}

body .gist tr:first-child td {
    padding-top: 15px !important;
}

body .gist tr:last-child td {
    padding-bottom: 15px !important;
}

body .gist .blob-num {
    color: #ced4da;
    background-color: #495057;
    pointer-events: none;
}

body .gist .gist-meta {
    display: none;
}

.gist-meta {
    display: none;
    border: none;
}

body .gist .gist-file {
    margin-bottom: 10px;
}

.gist .pl-smi, .gist .pl-s .pl-s1 {
    color: #66d9ef !important;
}
              
            
!

JS

              
                function ScriptoniteSort() {
    this.arr = [];
}

ScriptoniteSort.prototype = {
    swap: function(arr, j, nextslot) {
        //swap
        var temp = arr[j];
        arr[j] = arr[nextslot];
        arr[nextslot] = temp;
    },

    bubbleSort: function(datalist, logging) {
        //assign the data property to the passed in datalist
        this.arr = datalist;
        var numberOfPasses = 0,
            dataLength = this.arr.length;
        
        for(var i = 0; i < dataLength; i++) {
            //inner loop does the swapping
            for(var j = 0; j < dataLength - 1; j++) {
                if(this.arr[j] > this.arr[j+1]) {
                    this.swap(this.arr, j, j+1);
                }
            }
            numberOfPasses++;
        }
        //if logging is true, output in console
        if(logging){
            console.log("Number of passes: ", numberOfPasses);
            console.log("Bubble Sort Result: ", this.arr.toString());
        }
    },//end bubbleSort
    
    bubbleSortEnhanced: function(datalist) {
        //assign the data property to the passed in datalist
        this.arr = datalist;

        //check if passed in is an array
        if(arguments.length === 0 || !Array.isArray(this.arr)) {
            throw new Error('Either No Arguments Passed, Or Passed Param is not an Array');
        }

        var dataLength = this.arr.length,
            numberOfPasses = 0,
            innerLoopSwap;

        for(var i = 0; i < dataLength; i++) {
            //at the beginning of each pass inner swap is false. during the inner loop we set to 
            //true if a swap really happens. If it doesnt we end the nested loops
            innerLoopSwap = false;

            // by doing dataLength - 1 - i, we avoid unnecessary comparision.
            // for example if the highest # is already at the end, we compare it again?
            for(var j = 0; j < dataLength - 1 - i; j++) {
                if(this.arr[j] > this.arr[j+1]){
                    this.swap(this.arr, j, j+1);
                    //if swap happened set to true
                    innerLoopSwap = true;
                }
            }
            //increate count
            numberOfPasses++;

            //if a swap never happened, the entire collection is sorted.
            //if it did, then lets keep iterating through collection. note: on the next iteration
            //inner loop swap is set to false on the outter for loop
            if(!innerLoopSwap) {
                console.log("Number of passes: " + numberOfPasses);
                console.log("Enhanced Bubble Sort Result: ", this.arr.toString());
                return;
            }
        }
    } //end bubbleSortEnhanced
};

/*
define data collection
*/
var myArr = [9,11,5,1,7,2,15,1];
//var myArr = [1,1,2,5,7,9,11,15]; //this is already sorted

/*
instantiate sorting class
*/
var collection = new ScriptoniteSort();

/*
call the normal bubble sort
*/
collection.bubbleSort(myArr, true);

/*
call the enhanced better bubble sort
*/
//collection.bubbleSortEnhanced(myArr, true);
              
            
!
999px

Console