0

I have these sorting algorithms in my application, and I want to keep track the affected values, or the values that swapped during the sorting process. I want to highlight them.

I have solved this issue for bubble sort and selection sort, but somehow I can't get it work with Insertion sort and shell sort.

Here is the fiddle of what I am trying to do:

http://jsfiddle.net/4jnXf/1/

As you can see, on the last part of the iteration, which is more likely like insertion sort, so I won't ask how to do it in insertion sort. The big problem is the first part, second part, third part of the iteration, because it makes no sense. And in the last part of the iteration. I does highlight too the values that don't swap.

How can I correct this. Thank you!
Here is the faulty code:

shell: function() {
    var list = anada.vars.$list;
    $.each(list, function(key, value){
        if (!isNaN(list[key])) {
            list[key] = parseInt(list[key]);
        } else {

        }
    });
    var n = list.length;
    var increment = Math.floor(n / 2);
    var i;
    while (increment > 0) {
        var unsorted = list;
        for (i = increment; i < n; i++) {
            var temp = list[i];
            var j = i;
            while (j >= increment && list[j - increment] > temp) {
                list[j] = list[j - increment];
                j -= increment;
            }
            list[j] = temp;
            var rows = '<tr>';
            for (ctr = 0; ctr < unsorted.length; ctr++) {
                if (list[ctr] !== unsorted[ctr]) {
                    rows += '<td class="affected">' + list[ctr];
                } else {
                    rows += '<td>' + list[ctr];
                }
            }

            anada.vars.$elements.push(rows);
        }
        increment = Math.floor(increment / 2);
        var row = '<tr>';
        anada.app.generateIterationList(list);

    }
    anada.app.generateTable('result-shell', 'Result of Shell Sort');
},
3
Contributors
2
Replies
14
Views
4 Years
Discussion Span
Last Post by Airshow
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.