Advertisement
NicholasCSW

SortProjectStrings

Apr 10th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.21 KB | None | 0 0
  1. /**
  2.  * Sorts an unordered file of Strings using the insertion method
  3.  * Only after finishing did I realize the similarity to selection sort
  4.  *
  5.  * Thanks for the extension, it was well used.
  6.  *
  7.  * @author Ulizio
  8.  * @version 6
  9.  */
  10. import java.lang.Math;
  11. import java.io.*;
  12. import java.util.*;
  13. import java.util.Scanner;
  14. public class SortProjectStrings
  15. {
  16.     public static void main(String [] args) {
  17.        
  18.         try {
  19.            
  20.             //Array Creation Section:
  21.            
  22.             Scanner scan = new Scanner(new BufferedReader(new FileReader("words.txt")));
  23.             int size = 0;
  24.            
  25.             while(scan.hasNext()) {
  26.                  size++;
  27.                  scan.next();
  28.             }
  29.            
  30.             String [] strs = new String[size];
  31.            
  32.             scanStrs(strs);
  33.            
  34.             System.out.println("The Unsorted List:");
  35.            
  36.             print(strs);
  37.            
  38.             scan.close();
  39.            
  40.             //Moved code to method to simplify
  41.            
  42.             sort(strs);
  43.            
  44.             System.out.println("");
  45.             System.out.println("");
  46.             System.out.println("");
  47.            
  48.             System.out.println("The Sorted List:");
  49.            
  50.             print(strs);
  51.            
  52.         } catch(Exception e) {
  53.             System.out.println(e.getMessage());
  54.         }
  55.        
  56.        
  57.     }
  58.    
  59.     public static void scanStrs(String [] array) {
  60.        
  61.         //Here we will sort through the strings on file as we scan them into the array to
  62.        
  63.         try {
  64.            
  65.             Scanner scan = new Scanner(new BufferedReader(new FileReader("words.txt")));
  66.            
  67.            
  68.             int i = 0;
  69.            
  70.            
  71.             while(scan.hasNext()) {
  72.                
  73.                 array[i] = scan.next();
  74.                
  75.                 i++;
  76.             }
  77.            
  78.         } catch(Exception e) {
  79.             System.out.println(e.getMessage());
  80.         }
  81.        
  82.        
  83.     }
  84.    
  85.     public static void print(String [] array) {
  86.        
  87.         int i = 0;
  88.        
  89.         for(i = 0; i < array.length; i++) {
  90.            
  91.             System.out.println(array[i]);
  92.         }
  93.        
  94.     }
  95.    
  96.     public static void sort(String [] strs) {
  97.         try {
  98.             int currentIndex = 0;
  99.             int count = 0;
  100.             //No longer necessary
  101.             //String a = strs[currentIndex];
  102.             //String b = strs[currentIndex + 1];
  103.             String temp = "";
  104.             //int max = (int) Math.pow(size, 4);
  105.             int x = 0;
  106.            
  107.             //Needs to start at 1 because x is decrementing(checks the index before count)
  108.             for(count = 1; count < strs.length; count++) {
  109.                
  110.                 //This loop runs through each indices, then executes the sorting (below) for each index effectively
  111.                 //currentIndex = 0;
  112.                
  113.                 //Temp
  114.                 temp = strs[count];
  115.                
  116.                 //To conserve the # of loops we'll evaluate both conditions here. Should help with skipping issue I was having
  117.                 for(x = count - 1; x >= 0 && temp.compareToIgnoreCase(strs[x]) < 0; x--) {
  118.                    
  119.                    
  120.                     //This part of the loop runs through the process for each indices
  121.                     //The wikipedia gif shows a "slide" involved
  122.                     //This chunk "slides" the values as we go.
  123.                        
  124.                        
  125.                     //Swap
  126.                     strs[x + 1] = strs[x];
  127.                        
  128.  
  129.                    
  130.                    
  131.                 }
  132.                 strs[x + 1] = temp;
  133.                
  134.                
  135.                
  136.                 //System.out.println("--------------------");
  137.                 //print(strs);
  138.                
  139.             }
  140.            
  141.             //System.out.println("--------------------");
  142.             //print(strs);
  143.            
  144.         }
  145.        
  146.         catch(Exception e) {
  147.             System.out.println(e.getMessage());
  148.            
  149.         }
  150.        
  151.     }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement