Advertisement
NicholasCSW

SortProjectNums

Apr 10th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. /**
  2.  * Sort an unsorted array of ints using selection sort
  3.  *
  4.  * @author Ulizio
  5.  * @version 1
  6.  */
  7. import java.io.*;
  8. import java.util.*;
  9. import java.util.Scanner;
  10. public class SortProjectNums {
  11.     public static void main(String [] args) {
  12.        
  13.         try {
  14.             //Array Setup Section:
  15.            
  16.             Scanner scan = new Scanner(new BufferedReader(new FileReader("nums.txt")));
  17.             int size = 0;
  18.                
  19.             while(scan.hasNextInt()) {
  20.                  size++;
  21.                  scan.nextInt();
  22.             }
  23.            
  24.             //This list contains the raw numbers
  25.             int [] nums = new int[size];
  26.            
  27.             //This list will store the sorted numbers
  28.             int [] numsSorted = new int[size];
  29.            
  30.             scanNums(nums);
  31.            
  32.             System.out.println("The Unsorted List:");
  33.            
  34.             //print(nums);
  35.            
  36.             //Sorting Section:
  37.            
  38.             int count = 0;
  39.             int min = nums[0];
  40.             int i = 0;
  41.             int currentInt = 0;
  42.             int temp = 0;
  43.             int minIndex = 0;
  44.            
  45.             for(count = 0; count < size; count++) {
  46.                
  47.                 //This loop tracks the "round" we are on. The total number of rounds is how many times we run through the array.
  48.                 //Also regulates which sections are unsorted.
  49.                
  50.                 min = nums[count];
  51.                 minIndex = count;
  52.                
  53.                 for(i = count; i < size; i++) {
  54.                    
  55.                     //This loop runs through the entire array during each round and finds the minimum within the section that hasn't been sorted yet.
  56.                    
  57.                     currentInt = nums[i];
  58.                    
  59.                     if(currentInt < min) {
  60.                         min = nums[i];
  61.                         minIndex = i;
  62.                     }
  63.                    
  64.                 }
  65.                
  66.                 temp = nums[minIndex];
  67.                 nums[minIndex] = nums[count];
  68.                 nums[count] = temp;
  69.                
  70.             }
  71.            
  72.             System.out.println("The Sorted List:");
  73.             print(nums);
  74.            
  75.         }
  76.         catch(Exception e) {
  77.             System.out.println(e.getMessage());
  78.         }
  79.        
  80.        
  81.     }
  82.    
  83.     public static void scanNums(int [] array) {
  84.        
  85.         //Scan the ints into the array
  86.        
  87.         try {
  88.            
  89.             Scanner scan = new Scanner(new BufferedReader(new FileReader("nums.txt")));
  90.            
  91.            
  92.             int i = 0;
  93.            
  94.            
  95.             while(scan.hasNextInt()) {
  96.                
  97.                 array[i] = scan.nextInt();
  98.                
  99.                 i++;
  100.             }
  101.            
  102.         } catch(Exception e) {
  103.             System.out.println(e.getMessage());
  104.         }
  105.        
  106.        
  107.     }
  108.    
  109.     public static void print(int [] array) {
  110.        
  111.         int i = 0;
  112.        
  113.         for(i = 0; i < array.length; i++) {
  114.            
  115.             System.out.println(array[i]);
  116.         }
  117.        
  118.     }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement