Discuss / JavaScript / 冒泡排序

冒泡排序

Topic source

水蔓姐姐

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

想起了被数据结构支配的恐惧

var arr = document.getElementById('test-list')
var list = document.getElementsByClassName('lang')

for(let i = 0; i < list.length; i++) {
    for(let j = 0; j < list.length-i-1; j++){
          if(list[j].innerText > list[j+1].innerText){
             arr.insertBefore(list[j+1], list[j]);
             var x = list[j];
             list[j] = list[j+1];
             list[j+1] = x;
        }
    }
}

  • 1

Reply