Advertisement
NicholasCSW

Untitled

Jan 31st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.66 KB | None | 0 0
  1.  
  2. /**
  3.  * Take a list of numbers from file, insert them into an array and sort them.
  4.  *
  5.  * @author Ulizio
  6.  * @version 2.5
  7.  */
  8. import java.util.*;
  9. import java.io.*;
  10. import java.util.Scanner;
  11.  
  12. public class mergeSort {
  13.     public static void main(String [] args) {
  14.        
  15.         try {
  16.             /*
  17.             * Read file, find amount of numbers, and scan contents into array
  18.             */
  19.            
  20.             Scanner file = new Scanner(new BufferedReader(new FileReader("nums.txt")));
  21.            
  22.             int i = 0;
  23.            
  24.             while(file.hasNextLine()) {
  25.                file.nextInt();
  26.                i++;
  27.             }
  28.            
  29.             file.close();
  30.            
  31.             Scanner file2 = new Scanner(new BufferedReader(new FileReader("nums.txt")));
  32.             List<Integer> nums = new ArrayList<Integer>();
  33.  
  34.            
  35.             for (int x = 0; x < i; x++) {
  36.                
  37.                 nums.add(file2.nextInt());
  38.                
  39.             }
  40.            
  41.             /*
  42.              * Converting arraylist to an array so that I can work with
  43.              * fixed boundaries
  44.              */
  45.             int length = nums.size();
  46.             int [] nums2 = new int [length];
  47.             for(int z = 0; z < length; z++) {
  48.                 nums2[z] = nums.get(z);
  49.             }
  50.            
  51.             file2.close();
  52.            
  53.             for(int x = 0; x < nums2.length; x++) {
  54.                 System.out.println(nums2[x]);
  55.             }
  56.            
  57.             //Begin mergesort
  58.             sort(nums2, 0, nums2.length - 1);
  59.            
  60.         }
  61.         catch (Exception e) {
  62.            
  63.             System.out.println("YEET" + e);
  64.         }
  65.        
  66.        
  67.        
  68.     }
  69.    
  70.     private static void sort(int[] sortArray, int leftBound, int rightBound) {
  71.         /*
  72.          * Method to sort numbers, will be called recursively.
  73.          * Takes 3 arguments:
  74.          * The array of numbers
  75.          * "leftBound" or the beginning of the array
  76.          * "rightBound" or the end of the array
  77.          */
  78.        
  79.         int center = 0;
  80.        
  81.         if (leftBound < rightBound) {
  82.             /*
  83.              * If the array is not a single number, more sorty
  84.              */
  85.            
  86.             center = (leftBound + rightBound) / 2;
  87.            
  88.             //First Half
  89.             sort(sortArray, leftBound, center);
  90.             //Second Half
  91.             sort(sortArray, leftBound, center);
  92.            
  93.             /*
  94.              * On the way back up, splice back together and print
  95.              */
  96.            
  97.             merge(sortArray, rightBound, leftBound, center);
  98.            
  99.         }
  100.     }
  101.    
  102.     private static void merge(int [] nums, int rightOrig, int leftOrig, int centerOrig) {
  103.         /*
  104.          * Takes 4 Arguments:
  105.          * The array
  106.          * rightBound - end of array
  107.          * leftBound - beginning of array
  108.          * center - center of array
  109.          */
  110.        
  111.         //Initialize array for merging
  112.         int sortedNums[] = new int[nums.length];
  113.         int position = 0;
  114.        
  115.         /*
  116.          * Temp vars to play with
  117.          */
  118.         int left = leftOrig;
  119.         int right = rightOrig;
  120.         int center = centerOrig;
  121.        
  122.         /*
  123.          * While loops self explanatory, read like sentences
  124.          */
  125.         while (left <= center && center <= right) {
  126.             //Compare values
  127.             if(nums[left] <= nums[right]) {
  128.                 //add relevant number to final array, increment vars to continue working
  129.                 //through array.
  130.                 sortedNums[position] = nums[left];
  131.                 position++;
  132.                 left++;
  133.             }
  134.             else {
  135.                 sortedNums[position] = nums[center];
  136.                 position++;
  137.                 center++;
  138.             }
  139.         }
  140.        
  141.         while (left <= center) {
  142.             //add relevant number to final array, increment vars to continue working
  143.             //through array.
  144.             sortedNums[position] = nums[left];
  145.             position++;
  146.             left++;
  147.         }
  148.        
  149.         while (center <= right) {
  150.             //add relevant number to final array, increment vars to continue working
  151.             //through array.
  152.             sortedNums[position] = nums[center];
  153.         }
  154.        
  155.         //Finalize array by transferring sorted numbers
  156.         for(int y = 0; y <= rightOrig; y++) {
  157.             nums[y] = sortedNums[y];
  158.         }
  159.        
  160.         //print result
  161.         for(int x = 0; x < nums.length; x++) {
  162.             System.out.println(nums[x]);
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement