/*
 * Copyright (C) 2014 Pedro Vicente Gómez Sánchez.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.github.pedrovgs.problem79;

import com.github.pedrovgs.sortingalgorithm.SortingAlgorithm;

/**
 * Given an array full of integers implement a merge sort algorithm to sort the content inside
 * the array.
 *
 * @author Pedro Vicente Gómez Sánchez.
 */
public class MergeSort extends SortingAlgorithm {

  @Override public void sort(int[] numbers) {
    
  }
}

@@ -925,56 +925,2181 @@
 %0A%0A  
-@Override public void sort(int%5B%5D numbers) %7B%0A    
+private int%5B%5D numbers;%0A  private int%5B%5D helper;%0A  private int number;%0A%0A  /**%0A   * Merge sort takes advantage of the ease of merging already sorted lists into a new sorted list.%0A   * It starts by comparing every two elements (i.e., 1 with 2, then 3 with 4...) and swapping them%0A   * if the first should come after the second. It then merges each of the resulting lists of two%0A   * into lists of four, then merges those lists of four, and so on; until at last two lists are%0A   * merged into the final sorted list. Of the algorithms described here, this is the first that%0A   * scales well to very large lists, because its worst-case running time is O(n log n). It is also%0A   * easily applied to lists, not only arrays, as it only requires sequential access, not random%0A   * access. However, it has additional O(n) space complexity, and involves a large number of%0A   * copies in simple implementations.%0A   */%0A  @Override public void sort(int%5B%5D numbers) %7B%0A    validateInput(numbers);%0A%0A    this.numbers = numbers;%0A    number = numbers.length;%0A    this.helper = new int%5Bnumber%5D;%0A    mergeSort(0, number - 1);%0A  %7D%0A%0A  private void mergeSort(int low, int high) %7B%0A    if (low %3C high) %7B%0A      // Get the middle element index%0A      int middle = low + (high - low) / 2;%0A      // Sort the left side of the array%0A      mergeSort(low, middle);%0A      // Sort the right side of the array%0A      mergeSort(middle + 1, high);%0A      // Combine partinal sorting%0A      merge(low, middle, high);%0A    %7D%0A  %7D%0A%0A  private void merge(int low, int middle, int high) %7B%0A    // Copy both parts into the helper array%0A    for (int i = low; i %3C= high; i++) %7B%0A      helper%5Bi%5D = numbers%5Bi%5D;%0A    %7D%0A%0A    int i = low;%0A    int j = middle + 1;%0A    int k = low;%0A    // Copy the smallest values from either the left or the right side back to the original array%0A    while (i %3C= middle && j %3C= high) %7B%0A      if (helper%5Bi%5D %3C= helper%5Bj%5D) %7B%0A        numbers%5Bk%5D = helper%5Bi%5D;%0A        i++;%0A      %7D else %7B%0A        numbers%5Bk%5D = helper%5Bj%5D;%0A        j++;%0A      %7D%0A      k++;%0A    %7D%0A    // Copy the rest of the left side of the array into the target array%0A    while (i %3C= middle) %7B%0A      numbers%5Bk%5D = helper%5Bi%5D;%0A      k++;%0A      i++;%0A    %7D
 %0A  %7D

Add solution to problem 79