Skip to content

Instantly share code, notes, and snippets.

@genert
Created July 18, 2015 17:16
Show Gist options
  • Save genert/b7cae9df779f31570965 to your computer and use it in GitHub Desktop.
Save genert/b7cae9df779f31570965 to your computer and use it in GitHub Desktop.
bubbleSort.es6.js
class BubbleSort {
private Sorter (arrayList, order) {
var temp;
for (var i = 0; i < arrayList.length - 1; i++) {
for (var j = i; j < arrayList.length - i; j++) {
if ((order == 'asc' ? (arrayList[j-1] > arrayList[j]) : (arrayList[j-1] < arrayList[j]))) {
temp = arrayList[j-1];
arrayList[j-1] = arrayList[j];
arrayList[j] = temp;
}
}
}
}
Ascending (arrayList) {
if (!arrayList.length) {
return;
}
Sorter(arraylist, 'asc');
}
Descending (arrayList) {
if (!arrayList.length) {
return;
}
Sorter(arraylist, 'desc');
}
}
@dejwid
Copy link

dejwid commented Dec 18, 2017

short and sweet in es6

// let iterable = [9,6,7];
for (let i = ( iterable.length - 1); i >= 0; i--)
  for (let j = ( iterable.length - i); j > 0; j--)
    if (iterable[j] < iterable[j - 1])
      [ iterable[j-1], iterable[j] ] = [ iterable[j], iterable[j-1] ];
// iterable here is [6,7,9]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment