{"size": 629, "ext": "js", "max_stars_repo_path": "lib/bubble-sort.js", "max_stars_repo_name": "daltonamitchell/js-algorithms", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "lib/bubble-sort.js", "max_issues_repo_name": "daltonamitchell/js-algorithms", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "lib/bubble-sort.js", "max_forks_repo_name": "daltonamitchell/js-algorithms", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 17.0, "max_line_length": 64, "alphanum_fraction": 0.5755166932}
/**
* Write a function `bubbleSort(arr)` which will sort an array of
* integers using the "bubble sort"
* methodology. (http://en.wikipedia.org/wiki/Bubble_sort)
*/

Array.prototype.swap = function (a, b) {
  const c = this[a];
  this[a] = this[b];
  this[b] = c;
  return this;
};

function bubbleSort(array) {
  if (array.length < 2)
    return array;

  let sorted = false;
  while (!sorted) {
    sorted = true;
    let i = 1;

    while (i < array.length) {
      if (array[i-1] > array[i]) {
        sorted = false;
        array.swap((i-1), i);
      }

      i++;
    }
  }

  return array;
}

export default bubbleSort;
