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:


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];

        increment = Math.floor(increment / 2);
        var row = '<tr>';

    anada.app.generateTable('result-shell', 'Result of Shell Sort');

Recommended Answers

All 2 Replies

Well spotted LastMitch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.