/*
 * @(#)ExtraStorageMergeSortAlgorithm.java  1.0 97/01/09 Jack Snoeyink
 *
 * Copyright (c) 1995 University of British Columbia
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * UBC MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. UBC SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

/** A merge sort demonstration algorithm using extra space. <br />
 *  <br />
 *  Author: Jack Snoeyink, his version: 1.0, 09 Jan 97
 *  <br />
 *  Modif. 15.11.2005, Albrecht Weinert (a-weinert.de) :<br />
 *  {@link SortApplet.SortAlgorithm} + Kleinigkeiten.<br />  
 *  <br />
 *  @author   Albrecht Weinert
 *  @version  V.161, 28.05.2011
 */
public final class ExtraStorageMergeSortAlgorithm 
                               extends SortApplet.SortAlgorithm {

   void sort(int[] a, int lo, int hi, int scratch[])  {
      if (lo >= hi) return;  //  a[lo] is sorted already
      
      int mid = (lo + hi) / 2;
      sort(a, lo, mid, scratch);   //  Sort sublist a[lo..mid]  
      sort(a, mid+1, hi, scratch); //  Sort sublist a[mid+1..hi]
      
      int k, t_lo = lo, t_hi = mid+1;  
      for (k = lo; k <= hi; ++k)       // Merge sorted sublists
         if ((t_lo <= mid) && ((t_hi > hi) || (a[t_lo] < a[t_hi]))) {
            scratch[k] = a[t_lo++];
            pause(t_lo, t_hi);
         }
         else {
            scratch[k] = a[t_hi++];
            pause(t_lo, t_hi);
         }
      
      for (k = lo; k <= hi; k++) {
         a[k] = scratch[k]; /* Copy back to a   */
         pause(k);
      }
   } // sort(int a[],  
   
   @Override void sort(int[] a) {
      int scratch[] = new int[a.length];
      sort(a, 0, a.length-1, scratch);
   }
} // class ExtraStorageMergeSortAlgorithm (15.11.,27.11.2005, A.W.)

