Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.64 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.FileInputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.util.*;
  6.  
  7.  
  8. public class Test {
  9.  
  10.    
  11.           private static FileInputStream inFile;
  12.           private static InputStreamReader inReader;
  13.           private static  BufferedReader reader;
  14.          
  15.           private static Scanner inputReader = new Scanner (System.in);
  16.          
  17.          private static List<Student> classroom = new ArrayList<Student>(); // ArrayList to store the classroom.
  18.          
  19.          
  20.          
  21.          
  22.          
  23.  
  24.               public static void main (String args[]) throws IOException
  25.               {
  26.                
  27.  
  28.                 if(type()){
  29.                     initFile();  
  30.                     getData();
  31.                     System.out.print(classroom);  //output of the complete class.
  32.                     mergeSort(classroom);
  33.                     System.out.print(classroom); //output after sorting.
  34.                     inFile.close();
  35.                     }else{
  36.                         initFile();  
  37.                         getData();
  38.                         System.out.print(classroom);  //output of the complete class.
  39.                         int left = 0;
  40.                     int right = classroom.size() -1;
  41.                     quickSort(classroom, left, right);
  42.                System.out.print(classroom); //output after sorting.
  43.                 inFile.close(); }
  44.                
  45.                
  46.               }
  47.              
  48.              // preparing the file for input
  49.              
  50.               public static void initFile() throws IOException
  51.  
  52.              
  53.               {
  54.                 inFile = new FileInputStream ("c:\\!!VHSAPCSData\\truefalse.txt");
  55.                 inReader = new InputStreamReader(inFile);
  56.                 reader = new BufferedReader(inReader);
  57.                
  58.               }
  59.    
  60.              
  61.               //  Separate the id from the answers and store the answers in an array.
  62.              
  63.               public static void getData() throws IOException
  64.               {
  65.                 String line = reader.readLine();  //Seed
  66.                
  67.                 String[] answerkey = new String[10];  //Store the answer key from the first line of the txt file.
  68.                
  69.                 for(int i=0; i<answerkey.length; i++){ // take that line and place each answer in an array.
  70.                    
  71.                     answerkey[i]=line.substring(i,i+1);                
  72.                 }
  73.                
  74.                 line = reader.readLine();    // read the following line of the txt file.
  75.                                  
  76.                                            
  77.                 while(line != null)   // Read and create a student for each line.
  78.                 {
  79.                   String[] answers = new String[10];
  80.                   StringTokenizer strTkn = new StringTokenizer(line);
  81.                   String id = strTkn.nextToken();
  82.                   String answerline = strTkn.nextToken();
  83.                  
  84.                              
  85.                   for(int i=0; i<answers.length; i++){
  86.                      
  87.                       answers[i]=answerline.substring(i, i+1);
  88.                      
  89.                   }
  90.                  
  91.                   Student stu = new Student(id,answers);
  92.                  
  93.                   stu.grade(answerkey, answers);
  94.                  
  95.                   classroom.add(stu);  
  96.                  
  97.                  
  98.                  
  99.                                  
  100.                   line = reader.readLine();   //updating what is being read
  101.                                              
  102.                 }
  103.                
  104.               }
  105.              
  106.            
  107.               public static void swap(List<Student> example, int x, int y) {
  108.                     Student temp = example.remove(x);
  109.                     example.add(x, classroom.remove(y - 1));
  110.                     example.add(y, temp);
  111.              }
  112.  
  113.           // In this method you should sort the classroom in ascending order depending on the score obtained on the quiz.
  114.               public static void mergeSort(List<Student> a){
  115.                   divide(0, a.size()-1);
  116.                 }
  117.                  
  118.                 public static void divide(int startIndex,int endIndex){
  119.                      
  120.                     //Divide till you breakdown your list to single element
  121.                     if(startIndex<endIndex && (endIndex-startIndex)>=1){
  122.                         int mid = (endIndex + startIndex)/2;
  123.                         divide(startIndex, mid);
  124.                         divide(mid+1, endIndex);        
  125.                          
  126.                         //merging Sorted array produce above into one sorted array
  127.                         merger(startIndex,mid,endIndex);            
  128.                     }      
  129.                 }  
  130.                  
  131.                 public static void merger(int startIndex,int midIndex,int endIndex){
  132.                      
  133.                     //Below is the mergedarray that will be sorted array Array[i-midIndex] , Array[(midIndex+1)-endIndex]
  134.                     List<Student> mergedSortedArray = new ArrayList<Student>();
  135.                      
  136.                     int leftIndex = startIndex;
  137.                     int rightIndex = midIndex+1;
  138.                      
  139.                     while(leftIndex<=midIndex && rightIndex<=endIndex){
  140.                         if(classroom.get(leftIndex).getGrade()<=classroom.get(rightIndex).getGrade()){
  141.                             mergedSortedArray.add(classroom.get(leftIndex));
  142.                             leftIndex++;
  143.                         }else{
  144.                             mergedSortedArray.add(classroom.get(rightIndex));
  145.                             rightIndex++;
  146.                         }
  147.                     }      
  148.                      
  149.                     //Either of below while loop will execute
  150.                     while(leftIndex<=midIndex){
  151.                         mergedSortedArray.add(classroom.get(leftIndex));
  152.                         leftIndex++;
  153.                     }
  154.                      
  155.                     while(rightIndex<=endIndex){
  156.                         mergedSortedArray.add(classroom.get(rightIndex));
  157.                         rightIndex++;
  158.                     }
  159.                      
  160.                     int i = 0;
  161.                     int j = startIndex;
  162.                     //Setting sorted array to original one
  163.                     while(i<mergedSortedArray.size()){
  164.                         classroom.set(j, mergedSortedArray.get(i++));
  165.                         j++;
  166.                     }
  167.                 }
  168.            
  169.                                                  
  170.                                  
  171.                  
  172.               public static void quickSort(List<Student> example, int left, int right){
  173.                   if (left >= right) return;
  174.                  
  175.                   int i = left;
  176.                   int j = right;
  177.                   int pivotValue = classroom.get((i+j)/2).getGrade();
  178.                  
  179.                   while (i < j) {
  180.                    while (example.get(i).getGrade() < pivotValue) i++;
  181.                    while (pivotValue < example.get(j).getGrade()) j--;
  182.                    if(i <= j) {
  183.                     Student temp = example.remove(i);
  184.                     example.add(i, classroom.remove(j - 1));
  185.                     example.add(j, temp);
  186.                              j--;
  187.                              i++;
  188.                    }
  189.                   }
  190.                   quickSort (example, left, j);
  191.                   quickSort (example, i, right);
  192.                }
  193.              
  194.              
  195.                   public static boolean type(){
  196.                   System.out.print("If you wish to use merge sort, type 'yes' if you would rather quick sort, enter anything: ");
  197.                  String reply = inputReader.next();
  198.                   if(isYes(reply)){
  199.                       return true;
  200.                   }else{
  201.                       return false;
  202.                   }
  203.               }
  204.               //Method that returns true if the user inputs yes
  205.                   public static boolean isYes(String response){
  206.                             if(response.equals("yes")){
  207.                                 return true;
  208.                             }else{
  209.                                     return false;
  210.                             }
  211. }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement