Advertisement
Guest User

Untitled

a guest
May 6th, 2011
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. package gp2.ha5.exercise2;
  2.  
  3. import java.util.Random;
  4.  
  5. public class Exercise2 {
  6.     public static Random random = new Random(100);
  7.    
  8.     public static void main(String[] args) {
  9.         int[] array = createRandomArray(10000000);
  10.        
  11.         long time = System.currentTimeMillis();
  12.        
  13.         int[] sortedArray = sort(array);
  14.        
  15.         if(sortedArray.length != array.length || !isSorted(sortedArray)) {
  16.             System.err.println("Failed to sort given array! :-(");
  17.             return;
  18.         }      
  19.         System.out.println("Success! Sorting took " + (System.currentTimeMillis() - time) + "ms.");    
  20.     }
  21.  
  22.     /**
  23.      * Creates a randomly filled array of given length
  24.      * @param length
  25.      * @return
  26.      */
  27.     private static int[] createRandomArray(int length) {
  28.         int[] result = new int[length];
  29.         for(int i = 0; i < length; i++) {
  30.             result[i] = random.nextInt();
  31.         }
  32.         return result;
  33.     }
  34.    
  35.     /**
  36.      * Checks whether a given int array is sorted in ascending order  
  37.      * @param array
  38.      * @return <code>true</code> if the given int array is sorted; <code>false</code> otherwise.
  39.      */
  40.     private static boolean isSorted(int[] array) {
  41.         for(int i = 1; i < array.length; i++) {
  42.             if(array[i] < array[i-1]) {
  43.                 return false;
  44.             }
  45.         }
  46.         return true;
  47.     }
  48.    
  49.     /**
  50.      * Merges two sorted int array into one new sorted array.
  51.      * @param lhs
  52.      * @param rhs
  53.      * @return
  54.      */
  55.     private static int[] merge(int[] lhs, int[] rhs) {
  56.         int[] result = new int[lhs.length + rhs.length];
  57.        
  58.         int leftIndex = 0;
  59.         int rightIndex = 0;
  60.         while(leftIndex < lhs.length && rightIndex < rhs.length) {
  61.             if(lhs[leftIndex] <= rhs[rightIndex]) {
  62.                 result[leftIndex + rightIndex] = lhs[leftIndex];
  63.                 leftIndex++;
  64.             } else {
  65.                 result[leftIndex + rightIndex] = rhs[rightIndex];
  66.                 rightIndex++;
  67.             }
  68.         }
  69.        
  70.         while(leftIndex < lhs.length) {
  71.             result[leftIndex + rightIndex] = lhs[leftIndex];
  72.             leftIndex++;
  73.         }
  74.        
  75.         while(rightIndex < rhs.length) {
  76.             result[leftIndex + rightIndex] = rhs[rightIndex];
  77.             rightIndex++;
  78.         }
  79.        
  80.         return result;
  81.     }
  82.    
  83.     /**
  84.      * Sorts an array from index <code>startIndex</code> (inclusive) to <code>endIndex</code> (exclusive).
  85.      * @param array
  86.      * @param startIndex
  87.      * @param endIndex
  88.      * @return new array that is sorted
  89.      */
  90.     private static int[] mergeSort(int[] array, int startIndex, int endIndex) {
  91.         int length = endIndex - startIndex;
  92.         if(length == 0) {
  93.             return new int[]{};
  94.         }
  95.         if(length == 1) {
  96.             return new int[]{array[startIndex]};
  97.         }
  98.        
  99.         int halfLength = length / 2;
  100.         int[] sortedLeftPart = mergeSort(array, startIndex, startIndex + halfLength);
  101.         int[] sortedRightPart = mergeSort(array, startIndex + halfLength, endIndex);
  102.        
  103.         return merge(sortedLeftPart, sortedRightPart);     
  104.     }
  105.    
  106.    
  107.     /**
  108.      * Sorts a given array (ascending order)
  109.      * @param array
  110.      * @return
  111.      */
  112.     private static int[] sort(int[] array) {
  113.         //TODO: use multiple threads to speed up the sorting
  114.        
  115.         return mergeSort(array, 0, array.length);
  116.     }
  117.    
  118.    
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement