Advertisement
Guest User

Untitled

a guest
Jan 3rd, 2012
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.83 KB | None | 0 0
  1. public class Sorter2
  2. {
  3.     public static void toString(int [] list)
  4.     {
  5.         for(int i = 0; i < list.length; i++)
  6.         {
  7.             System.out.print(list[i]);
  8.             if(!(i + 1 == list.length))
  9.             {
  10.                 System.out.print(",");
  11.             }
  12.         }
  13.        
  14.         System.out.println("");
  15.     }
  16.    
  17.     public static void toString(int list[], int from, int to)
  18.     {
  19.         for(int i = from; i <= to; i++)
  20.         {
  21.             System.out.print(list[i]);
  22.             if(i + 1 <= to)
  23.             {
  24.                 System.out.print(",");
  25.             }
  26.         }
  27.        
  28.         System.out.println("");
  29.     }
  30.    
  31.  
  32.     public static void insertAt(int [] list, int insert_at, int taken_from)
  33.     {
  34.         int to_insert = list[taken_from];
  35.         for(int i = taken_from; i >= insert_at; i--)
  36.         {
  37.             if(i != insert_at)
  38.             {
  39.                 list[i] = list[i - 1];
  40.             }
  41.             else
  42.             {
  43.                 list[i] = to_insert;
  44.             }
  45.         }
  46.     }
  47.    
  48.     public static void sortSegments(int [] list ,int segment_one_begin, int midpoint, int segment_two_end)
  49.     {
  50.         toString(list, segment_one_begin, segment_two_end);
  51.    
  52.         int [] temp = new int[segment_two_end - segment_one_begin + 1];
  53.        
  54.         int counter_one = segment_one_begin;
  55.         int counter_two = midpoint + 1;
  56.        
  57.         int i = 0;
  58.        
  59.         while(counter_one <= midpoint && counter_two <= segment_two_end)
  60.         {
  61.             if(list[counter_one] <= list[counter_two])
  62.             {
  63.                 temp[i] = list[counter_one];
  64.                 counter_one++;
  65.                 i++;
  66.             }
  67.             else
  68.             {
  69.                 temp[i] = list[counter_two];
  70.                 counter_two++;
  71.                 i++;
  72.             }
  73.         }
  74.        
  75.         while(i < temp.length && counter_one <= midpoint)
  76.         {
  77.             temp[i] = list[counter_one];
  78.             counter_one++;
  79.             i++;
  80.         }
  81.  
  82.         while(i < temp.length && counter_two <= segment_two_end)
  83.         {
  84.             temp[i] = list[counter_one];
  85.             counter_two++;
  86.             i++;
  87.         }
  88.        
  89.         for(int l = 0; l < temp.length; l++)
  90.         {
  91.             list[segment_one_begin + l] = temp[l];
  92.         }
  93.        
  94.         toString(list, segment_one_begin, segment_two_end);
  95.     }
  96.    
  97.     public static void mergeSort(int [] list, int segment_begining, int segment_end)
  98.     {
  99.         if(segment_begining < segment_end)
  100.         {
  101.             int midpoint = (segment_end + segment_begining) / 2;
  102.             mergeSort(list, segment_begining, midpoint);
  103.             mergeSort(list, midpoint + 1, segment_end);
  104.             sortSegments(list, segment_begining, midpoint, segment_end);
  105.         }
  106.  
  107.     }
  108.     public static void mergeSort(int [] list)
  109.     {
  110.         mergeSort(list, 0, list.length - 1);
  111.     }
  112.    
  113.     public static boolean isInOrder(int [] toCheck)
  114.     {
  115.         for(int i = 1; i < toCheck.length; i++)
  116.         {
  117.             if(toCheck[i] < toCheck[i - 1])
  118.             {
  119.                 return false;
  120.             }
  121.         }
  122.        
  123.         return true;
  124.     }
  125.    
  126.     public static int [] populate(int numOfItems)
  127.     {
  128.         int [] toReturn = new int[numOfItems];
  129.        
  130.         for(int i = 0; i < toReturn.length; i++)
  131.         {
  132.             toReturn[i] = (int) (Math.random() * 100 + 1);
  133.         }
  134.        
  135.         return toReturn;
  136.     }
  137.    
  138.     public static void main(String [] args)
  139.     {
  140.         int [] nums = populate(20);
  141.         mergeSort(nums);
  142.         toString(nums);
  143.         System.out.println(isInOrder(nums));
  144.        
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement