1

Okay, I am trying to create a sorting application but, its kinda busting my mind. Maybe because of thinking too much.

What I wanted

  1. I want to sort values from an array, maybe using bubble sort
  2. I want to push each iteration to <tr> of a table
  3. and be able to know which values has been replaced

What must happen

  1. Each iteration, I will get a list of values
  2. each list will highlight the values affected

What I currently have

var sort = {
    init : function() {
        sort.vars.$oldList = [6,4,7,1,8];
        sort.play.bubble();
    }
}

sort.vars =  {
    $oldList : new Array(),
    $newList : new Array()
}

sort.play = {
    bubble : function() {
        var list = sort.vars.$oldList;
        var n = list.length;
        var isSorted = false;
        while(!isSorted) {
            var tmp, i;
            isSorted = true;
            for (i = 0; i < n; i++) {
                if (list[i] > list[i+1]) {
                    tmp = list[i];
                    list[i] = list[i+1];
                    list[i+1] = tmp;
                    isSorted = false;
                }
                sort.ui.pushtToTable(list);
            }
        }        
    }
}

sort.ui = {
    pushtToTable : function(list) {
        $.each(list, function(n, val){
            $('tr').append('<td>' + val);
        })
    }
}

$(document).ready(function(){
    sort.init();
})

If possible I wanted to display values one by one, maybe setting a timeout, right?

1 Answer 1

3

Yes, setTimeout is a good idea if you want to "see" the algorithm's progress. However, setTimeout only accepts functions as arguments, so each iteration of the sorting algorithm has to be performed in a separate function. See the following code for an example (the code doesn't produce output in each iteration, instead it "animates" the swapping action - but I'm sure you can easily adjust this to fit your needs).

DEMO (JSFiddle)

var SORT = function(type, list, selector){
    var container, containerTr, animSteps = [];
    // Show all elements in the container
    var printArray = function(list){
        var str = ["<table>"], i = 0, l = list.length;
        for (i; i < l; ++i) {
            str.push("<tr><td>", list[i], "</td></tr>");
        }
        str.push("</table>");
        container.html(str.join(""));
    };
    // This is the interesting part... ;)
    var swap = function(list, i1, i2) {
        var tmp = list[i1];
        list[i1] = list[i2];
        list[i2] = tmp;
        // Add 3 functions for each swapping action:
        // 1. highlight elements, 2. swap, 3. remove highlight
        animSteps.push(function(){
            containerTr.eq(i1).add(containerTr.eq(i2)).addClass("highlight");
        }, function(){
            var tmp = containerTr.eq(i1).text();
            containerTr.eq(i1).text(containerTr.eq(i2).text());
            containerTr.eq(i2).text(tmp);
        }, function(){
            containerTr.eq(i1).add(containerTr.eq(i2)).removeClass("highlight");
        });
    };
    var animation = function(){
        // Execute all iteration functions one after another
        if (animSteps.length) {
            setTimeout(function(){
                animSteps.splice(0,1)[0]();
                animation();
            }, 250);
        }
    };

    // Collection of sorting algorithms
    var algorithms = {
        bubblesort: function(list) {
            for (var n = list.length; n > 1; --n) {
                for (var i = 0; i < n-1; ++i) {
                    if (list[i] > list[i+1]) {
                        swap(list, i, i+1);
                    }
                }
            }
        }
        // Add more algorithms using "swap" here...
    };

    if (algorithms[type] != undefined) {
        container = $(selector);
        printArray(list);
        containerTr = container.find("tr");
        algorithms[type](list);
        this.sorted = list;
        animation();
    }
};

// Usage:
var s = new SORT("bubblesort", [5,8,2,4,1,9,7,3,0,6], "#container");
console.log(s.sorted);    //the sorted array
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.