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.

            
              <div class="wrapper">
    <canvas id='world'></canvas>
    <br />
    <button class="reset-btn">reset</button>
    <button class="sort-btn">bubble</button>
    <button class="sort-btn">bidirectionalBubble</button>
    <button class="sort-btn">selection</button>
    <button class="sort-btn">quick</button>
</div>

            
          
!
            
              * {
    margin: 0px;
    padding: 0px;
}

body {
    background-color: rgb(252,252,252);
}

.wrapper {
    margin-top: 15px;
    text-align: center;
}

.wrapper canvas {
    box-shadow: 0px 0px 8px 0px #888;
}

.wrapper button {
    margin: 4px 0px;
    padding: 2px 4px;
}
            
          
!
            
              /*
 * constant
 */
var SCREEN_WIDTH    = 400;
var SCREEN_HEIGHT   = 380;
var SCREEN_CENTER_X = SCREEN_WIDTH/2;
var SCREEN_CENTER_Y = SCREEN_HEIGHT/2;

var BASE_X = 12;
var BASE_Y = 370;
var VALUE_MAX_NUM         = 360;
var VALUE_LIST_MAX_LEN = 38;


/*
 * global
 */
var canvas    = null;
var valueList = null;


/*
 * main
 */
tm.main(function() {
    // canvas インスタンス生成
    canvas = tm.graphics.Canvas("#world");
    // リサイズ
    canvas.resize(SCREEN_WIDTH, SCREEN_HEIGHT);
    
    reset();
    tm.dom.Element(".reset-btn").event.click(function() {
        reset();
    });
    
    var btnList = tm.dom.ElementList(".sort-btn");
    btnList.forEach(function(elm) {
        elm.event.click(function() {
            start(this.html + "Sort");
        });
    });
  
    //start("bubble");
});


var reset = function() {
    valueList = [];
    for (var i=0;i<VALUE_LIST_MAX_LEN; ++i) {
        valueList.push( Math.rand(0, VALUE_MAX_NUM) );
    }
    draw(valueList);
};


var start = function(sortName) {
    var sortFunc = window[sortName] || quickSort;
    console.time(sortName);
    var sortProcess = sortFunc(valueList);
    console.timeEnd(sortName);
    
    
    var index = 0;
    var id = setInterval(function() {
        var list = sortProcess[index++];
        draw(list);
        if (index >=sortProcess.length) clearInterval(id);
    }, 10);
};


var draw = function(list) {
    canvas.clearColor("rgba(255, 255, 255, 1.0)");
    
    valueList.forEach(function(v, i) {
        drawBar(BASE_X+i*10, BASE_Y, list[i]);
    });
};


var drawBar = function(x, y, value) {
    var rect = [
        x,
        y-value,
        5,
        value
    ];
    canvas.fillStyle = "hsla(" + value + ", 100%, 50%, 1.0)";
    canvas.fillRect(rect[0], rect[1], rect[2], rect[3]);
};


/**
 * バブルソート
 */
var bubbleSort = function(list)
{
    var sortProcess = [];
    var len = list.length;
    
    for (var i=0; i<len-1; ++i) {
        for (var j=len-1; j>i; --j) {
            // 交換
            if (list[j] < list[j-1]) {
                list.swap(j, j-1);
                // 過程を記録
                sortProcess.push(list.clone());
            }
        }
    }
    
    return sortProcess;
};


var bidirectionalBubbleSort = function(list)
{
    var sortProcess = [];
        
    var start   = -1;
    var end     = list.length;
    var flag = 0;
    
    while (start<end) {
        ++start;
        --end;
        
        flag = 0; 
        for (var i=start; i<end; ++i) {
            if (list[i] > list[i+1]) {
                list.swap(i, i+1);
                flag |= 1;
            }
            // 過程を記録
            sortProcess.push(list.clone());
        }
        if (!flag) break;
        
        flag = 0; 
        for (var i=end; i>=start; --i) {
            if (list[i] > list[i+1]) {
                list.swap(i, i+1);
                flag |= 1;
            }
            // 過程を記録
            sortProcess.push(list.clone());
        }
        if (!flag) break;
    };
    
    return sortProcess;
};

var selectionSort = function(list)
{
    var sortProcess = [];
    
    var len=list.length;
    for (var i=0; i<len-1; ++i) {
        for (var j=i+1; j<len; ++j) {
            if (list[j]<list[i]) { list.swap(i, j); }
            // 過程を記録
            sortProcess.push(list.clone());
        }
    }
    
    return sortProcess;
};

var quickSort = function(list)
{
    var sortProcess = [];
    
    (function(start, end) {
        var key = list[ Math.floor((start+end)/2) ];
        var i = start;
        var j = end;
        while (true) {
            while (list[i]<key) ++i;
            while (key<list[j]) --j;
            if (i>=j) break;
            list.swap(i, j);
            ++i;
            --j;
            // 過程を記録
            sortProcess.push(list.clone());
        }
        
        // 再帰呼び出し
        if (start < i-1) arguments.callee(start, i-1);
        if (j+1 < end)   arguments.callee(j+1, end);
    })(0, list.length-1);
    
    return sortProcess;
};
            
          
!
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