0 Replies - 848 Views - Last Post: 17 March 2013 - 07:04 AM

#1 iamspix  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 17-March 13

Determine the affected values when sorting values in javascript

Posted 17 March 2013 - 07:04 AM

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');
},


Is This A Good Question/Topic? 0
  • +

Page 1 of 1