The merge algorithm that merges 2 sorted array portions

Recall: the merge( ) algorithm:

 public static <T extends Comparable<T>> void merge(T[] A, int s, int m, int e, T[] H)
 {
     int i = s, j = m;  // Current elements in left and right (sorted) portions
     int k = 0;         // Current copy location in helper array H

     while ( i < m || j < e ) // Loop as long as there are unprocessed items
     {
         if ( i < m && j < e )
         {  // Both portions have unprocessed elements
             if ( A[i].compareTo(A[j]) <= 0 )
                 H[k++] = A[i++];
             else
                 H[k++] = A[j++];
         }
         else if ( i == m )     // Left part is exhausted (empty)
             H[k++] = A[j++];
         else if ( j == e )     // Right part is exhausted (empty)
             H[k++] = A[i++];
     }

     // Copy H[ ] back to A[ ]
     for (i = s, k = 0; i < e; i++, k++)
         A[i] = H[k];
 } 

Synopsis of the merge( ) algorithm

Summary of the merge( ) algorithm

 public static <T extends Comparable<T>> void merge(T[] A, int s, int m, int e, T[] H)
 {
     int i = s, j = m;  // Current elements in left and right (sorted) portions
     int k = 0;         // Current copy location in helper array H

     while ( i < m || j < e ) // Loop as long as there are unprocessed items
     {
         doPrimitive( );

	 Then execute:
                         i++
	 Or execute:
	                 j++





     }

     // Copy H[ ] back to A[ ]
     for (i = s, k = 0; i < e; i++, k++)
         A[i] = H[k];
 } 

Runtime analysis of the merge( ) algorithm

The # times that the while-loop is execute is equal to e − s = n:

 public static <T extends Comparable<T>> void merge(T[] A, int s, int m, int e, T[] H)
 {
     int i = s, j = m;  // Current elements in left and right (sorted) portions
     int k = 0;         // Current copy location in helper array H

     while ( i < m || j < e ) // Loop as long as there are unprocessed items
     {
         doPrimitive( );

	 Then execute:
                         i++  -->  i = s; i < m; i++   = m-s times
	 Or execute:
	                 j++  -->  j = m; j < e; j++   = e-m times


         # times = (m-s) + (e-m) = e - m + m - s = e - s = n 

         (n = number of elements in the range [s,e) )
     }

     // Copy H[ ] back to A[ ]
     for (i = s, k = 0; i < e; i++, k++)
         A[i] = H[k];
 } 

Runtime analysis of the merge( ) algorithm

The # times that the for-loop is execute is equal to e − s = n:

 public static <T extends Comparable<T>> void merge(T[] A, int s, int m, int e, T[] H)
 {
     int i = s, j = m;  // Current elements in left and right (sorted) portions
     int k = 0;         // Current copy location in helper array H

     while ( i < m || j < e ) // Loop as long as there are unprocessed items
     {
         doPrimitive( );

	 Then execute:
                         i++  -->  i = s; i < m; i++   = m-s times
	 Or execute:
	                 j++  -->  j = m; j < e; j++   = e-m times


         # times = (m-s) + (e-m) = e - m + m - s = e - s = n 

         (n = number of elements in the range [s,e) )
     }

     // Copy H[ ] back to A[ ]
     for (i = s, k = 0; i < e; i++, k++)  -->  i = s; i < e; i++  = e - s = n
         A[i] = H[k];
 } 

Runtime analysis of the merge( ) algorithm

Therefore:   the running time of the merge( ) algorithm = 2n = O(n):

 public static <T extends Comparable<T>> void merge(T[] A, int s, int m, int e, T[] H)
 {
     int i = s, j = m;  // Current elements in left and right (sorted) portions
     int k = 0;         // Current copy location in helper array H

     while ( i < m || j < e ) // Loop as long as there are unprocessed items
     {
         doPrimitive( );

	 Then execute:
                         i++  -->  i = s; i < m; i++   = m-s times
	 Or execute:
	                 j++  -->  j = m; j < e; j++   = e-m times


         # times = (m-s) + (e-m) = e - m + m - s = e - s = n 

         (n = number of elements in the range [s,e) )
     }

     // Copy H[ ] back to A[ ]
     for (i = s, k = 0; i < e; i++, k++)  -->  i = s; i < e; i++  = e - s = n
         A[i] = H[k];
 }