Discuss / JavaScript / 冒泡排序

冒泡排序

Topic source

Sky_Watcher

#1 Created at ... [Delete] [Delete and Lock User]
var ul = $('#test-div>ul');
var add = function(text){
    return '<li><span>'+ text +'</span></li>';
}
ul.append(add('Pascal'));
ul.append(add('Lua'));
ul.append(add('Ruby'));

var li = $('#test-div>ul>li'); 
for(var i=0; i < li.length - 1; i++){
    for(var j=1; j < li.length - i; j++){
        li = $('#test-div>ul>li');
        var cur = $(li[j-1]);
        var next = $(li[j]);
        if(cur.text() >= next.text()){         
            var tmp = cur.text();
            cur.text(next.text());
            next.text(tmp);
        }
    }
}

vi几

#2 Created at ... [Delete] [Delete and Lock User]

冒泡排序挺好,不过if(cur.text() >= next.text())等号应该是可以去掉的,>就可以了


  • 1

Reply