Assign8

BubbleSort Practice

by ClarenceDowns

HTML

<h3>
Bubble Sort Practice
</h3>

JavaScript

function bubbleSort(_list) {
  var isSorted = false;
  var lastUnsorted = _list.length;
  while (!isSorted) {
    isSorted = true;

    for (var i = 0; i < _list.length - 1; i++)
      if (_list[i] > _list[i + 1]) {
        swap(_list, i, i + 1);
        isSorted = false;
      }
  }
  lastUnsorted--;
}

function swap(arr, a, b) {
  //let temp = arr[a];
  [arr[a], arr[b]] = [arr[b], arr[a]];
}

var test = ['this', 'is', 'just', 'to', 'check', 'if', 'this', 'works'];
bubbleSort(test);
//swap(test, 0, 1);
console.log(test);