<file_sep>function swap(arr, index) {
  var oldLeft = arr[index];

  arr[index] = arr[index + 1];
  arr[index + 1] = oldLeft;

  return arr;
}

function bubbleSort(array) {
  var count = 1;
  while (count) {
    count = 0;
    for (var i = 0; i < array.length - 1; i++) {
      if (array[i] > array [i+1]) {
        array = swap(array, i);
        count++;
      }
    }
  }
  return array;
}
<file_sep>//--------- Bubble Sort

//empty array
describe('Bubble Sort', function(){
  it('handles an empty array', function(){
    expect( bubbleSort([]) ).toEqual( [] );
  });
});

//single items
describe('Bubble Sort', function(){
  it('handles an array of length 1', function(){
    expect( bubbleSort([5]) ).toEqual( [5] );
  });
});

// multiple items
describe('Bubble Sort', function(){
  it('handles an array with multiple elements', function(){
    expect( bubbleSort([2, 3, 1]) ).toEqual( [1, 2, 3] );
  });
});

describe('Bubble Sort', function(){
  it('handles an array with multiple elements', function(){
    expect( bubbleSort([1, 3, 1, 5, 7]) ).toEqual( [1, 1, 3, 5, 7] );
  });
});

<file_sep># sorting
Implementation of bubble and merge sorting algorithms
<file_sep>describe('Split Array function', function() {
  it('is able to split an array into two halves', function() {
    expect(split([1,2,3,4])).toEqual([[1,2],[3,4]]);
  });
});

describe('Split Array function', function() {
  it('is able to split an odd array into two halves', function() {
    expect(split([1,2,3,4,5])).toEqual([[1,2],[3,4,5]]);
  });
});

describe('Merge Sort', function(){
  it('is able to merge two sorted arrays into one sorted array', function(){
    expect(merge([1],[2])).toEqual([1,2]);
  });
});

describe('Merge Sort', function(){
  it('is able to merge two sorted arrays into one sorted array', function(){
    expect(merge([1,3], [2,4])).toEqual([1,2,3,4]);
  });
});

describe('Mergesort', function(){
  it('fdsfdsfsdfsd', function(){
    expect(mergeSort([4,1,5,9,3,8])).toEqual([1,3,4,5,8,9])
  });
});

describe('Mergesort', function(){
  it('fdsfdsfsdfsd', function(){
    expect(mergeSort([1,4,5,9,3,1,8])).toEqual([1,1,3,4,5,8,9])
  });
});
<file_sep>function split(wholeArray) {
    var firstHalf = wholeArray.slice(0, wholeArray.length / 2);
    var secondHalf = wholeArray.slice(wholeArray.length / 2);
    return [firstHalf, secondHalf];
  }

  function merge(left, right){
    var output = [],
        leftIndex = 0,
        rightIndex = 0;

    while (leftIndex < left.length && rightIndex < right.length) {
      if (left[leftIndex] < right[rightIndex]) {
        output.push(left[leftIndex]);
        leftIndex++;
      }
      else {
        output.push(right[rightIndex]);
        rightIndex++;
      }
    }
    for (; leftIndex < left.length; leftIndex++) output.push(left[leftIndex]);
    for (; rightIndex < right.length; rightIndex++) output.push(right[rightIndex]);
    return output;
  }

  function mergeSort(array) {
    if (array.length > 1) {
      var arrLeft = split(array)[0];
      var arrRight = split(array)[1];
      return merge(mergeSort(arrLeft), mergeSort(arrRight));
    }
    return array;
  }
