20-CS-4003-001 Organization of Programming Languages Fall 2017
Actual Programs

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures

MergeSort

Java:   Source
   
   import java.util.*;
   import java.io.*;

   class MergeSort {
      int elem[], nelem;

      public MergeSort (int n) {
         nelem = n;
         elem = new int[n];
         for (int i=0 ; i < n ; i++) elem[i] = (int)(10*n*Math.random());
      }

      public void merge(int A[], int f, int mid, int l) {
         int temp[] = new int[A.length];

         int first1 = f, last1  = mid, first2 = mid+1, last2  = l, index = 0;

         for (index=first1 ; first1 <= last1 && first2 <= last2 ; index++) {
            if (A[first1] < A[first2]) temp[index] = A[first1++];
            else temp[index] = A[first2++];
         }

         for ( ; first1 <= last1 ; first1++, index++) temp[index] = A[first1];
         for ( ; first2 <= last2 ; first2++, index++) temp[index] = A[first2];

         for (index=f ; index <= l ; index++) A[index] = temp[index];
      }

      public void mergesort (int f, int l) {
         if (f >= l) return;
         mergesort(f, (f+l)/2);
         mergesort((f+l)/2+1, l);
         merge(elem, f, (f+l)/2, l);
      }

      public void sort () {  mergesort(0, elem.length-1);  }
      
      public void show () {
         for (int i=0 ; i < nelem ; i++) System.out.print(elem[i]+" ");
         System.out.println();
      }
   }

   public class intro_7 {
      public static void main (String args[]) {
         try {
            MergeSort ms = new MergeSort(Integer.parseInt(args[0]));
            ms.show();
            ms.sort();
            ms.show();
         } catch (Exception e) { 
            System.out.println("Usage: java intro_7 ");
         }
      }
   }

The above can be run as follows:

   prompt> java intro_7 20
   14 199 96 43 6 107 68 68 120 140 128 197 3 92 36 14 157 125 196 99
   3 6 14 14 36 43 68 68 92 96 99 107 120 125 128 140 157 196 197 199
 
Haskell:   Source
   {- Merge two lists of increasing numbers -} 
   mrg :: (Ord a) => [a] -> [a] -> [a]
   mrg [] [] = []
   mrg a [] = a
   mrg [] a = a
   mrg x y =
      if ((x !! 0) < (y !! 0)) then (x !! 0) : mrg (tail x) y
      else (y !! 0) : mrg x (tail y)

   {- Split a list: keep even indexed items -}
   splite :: (Num a) => [a] -> [a]
   splite [] = []
   splite (a:[]) = [a]
   splite (a:b:x) = a : splite x

   {- Split a list: keep odd indexed items -}
   splito :: (Num a) => [a] -> [a]
   splito [] = []
   splito (a:[]) = []
   splito (a:b:x) = b : splito x

   {- Mergesort -}
   srt :: (Num a, Ord a) => [a] -> [a]
   srt [] = []
   srt (a:[]) = [a]
   srt x = mrg (srt (splite x)) (srt (splito x))
Run it, for example, like this:
   ghci> srt [14,199,96,43,6,107,68,68,120,140,128,197,3,92,36,14,157,125,196,99]
   [3,6,14,14,36,43,68,68,92,96,99,107,120,125,128,140,157,196,197,199]
 
Scheme:   Source
   ;; merge two increasing lists
   (define merge 
      (lambda (x y)
         (if (null? x)
             y
             (if (null? y)
                 x
                 (if (< (car x) (car y))
                     (cons (car x) (merge (cdr x) y))
                     (cons (car y) (merge x (cdr y))))))))

   ;; Split list x - output even indexed elements
   (define splite
      (lambda (x)
         (if (or (null? x) (null? (cdr x)))
             x
             (cons (car x) (splite (cdr (cdr x)))))))

   ;; Split list x - output odd indexed elements 
   (define splito
      (lambda (x)
         (if (or (null? x) (null? (cdr x)))
             '()
             (cons (car (cdr x)) (splito (cdr (cdr x)))))))

   ;; Mergesort
   (define srt
      (lambda (x)
         (if (or (null? x) (null? (cdr x)))
             x
             (merge (srt (splito x)) (srt (splite x))))))

Run it like this:

   scheme> (srt '(14 199 96 43 6 107 68 68 120 140 128 197 3 92 36 14 157 125 196 99))
   ;Value: (3 6 14 14 36 43 68 68 92 96 99 107 120 125 128 140 157 196 197 199)