css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              
<!-- 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>
            
          
!
            
              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;
}
            
          
!
            
              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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console