Advertisement
Guest User

lab4

a guest
Feb 17th, 2020
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.00 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class Lab4
  5. {
  6.     /**
  7.      *  Problem: Sort multi-digit integers (with n total digits) in O(n) time.
  8.      */
  9.     private static void problem(byte[][] arr)
  10.     {  
  11.        //get the length of arr
  12.        int length = 0;
  13.        for(int i = 0; i < arr.length; i++){
  14.           if(length < arr[i].length){
  15.           length = arr[i].length;
  16.           }
  17.        }
  18.        //make counting sort array
  19.        int[] countingSort = new int[length+1];
  20.        //add 1 to the index value of the length of arr at i
  21.        for(int i = 0; i < arr.length; i++){
  22.           countingSort[arr[i].length]++;
  23.        }
  24.        //subtract from the first index
  25.        countingSort[0]--;
  26.        //index = index plus previous index
  27.        for(int i = 1; i < countingSort.length; i++){
  28.            countingSort[i] = countingSort[i] + countingSort[i-1];
  29.        }
  30.        //make new array to sort
  31.        byte[][] sortLengths = new byte[arr.length][];
  32.        //put arr in sort array at index counting sort index arr[i].length      
  33.        for(int i = arr.length-1; i >= 0; i--){
  34.           sortLengths[countingSort[arr[i].length]] = arr[i];
  35.           countingSort[arr[i].length]--;
  36.        }
  37.        //var for first and last index in sort that have the same length and new counting sort array
  38.        int beg = 0;
  39.        int end = 0;
  40.        int indexPointer = 0;
  41.        
  42.        byte[][] sortValues = new byte[arr.length][];
  43.        
  44.        for(int i = 0; i < sortLengths.length; i++) {
  45.            //find the index of where the lengths are the same
  46.            if(sortLengths[beg].length == sortLengths[end].length) {
  47.                end++;
  48.            }
  49.            //if not the same -1 and then counting sort the array
  50.          if(end == sortLengths.length || sortLengths[beg].length < sortLengths[end].length) {
  51.            
  52.             for(indexPointer = 0; indexPointer < sortLengths[beg].length; indexPointer++){
  53.             int[] radixSort = new int[128];
  54.             radixSort[0]--;
  55.                   //increase index value of number in sort
  56.                   for(int j = beg; j <= end-1; j++) {
  57.                       radixSort[(int) sortLengths[j][indexPointer]]++;
  58.                   }
  59.                   //index = index + prevoius index
  60.                   for(int j = 1; j < radixSort.length; j++) {
  61.                       radixSort[j] = radixSort[j] + radixSort[j-1];
  62.                   }
  63.    
  64.                   for(int j = end-1; j >= beg; j--) {
  65.                   //take last value and put it in the index
  66.                  sortValues[beg + radixSort[(int) sortLengths[j][indexPointer]]] = sortLengths[j];
  67.                       radixSort[(int) sortLengths[j][indexPointer]]--;
  68.                   }
  69.                
  70.             }
  71.                beg = end;
  72.            }
  73.          
  74.        }
  75.        for(int k = 0; k < arr.length; k++){
  76.           arr[k] = sortValues[k];
  77.        }
  78.        //arr = sortValues;
  79.            
  80.        //for loop with beg end var
  81.        //make array 128
  82.        //
  83.        
  84.        
  85.          
  86.        //System.out.print(" ");
  87.         // Implement me!
  88.     }
  89.  
  90.     // ---------------------------------------------------------------------
  91.     // Do not change any of the code below!
  92.  
  93.     private static final int LabNo = 4;
  94.     private static final String quarter = "Winter 2020";
  95.  
  96.     private static final Random rng = new Random(654321);
  97.  
  98.     private static boolean testProblem(byte[][] testCase)
  99.     {
  100.         byte[][] numbersCopy = new byte[testCase.length][];
  101.  
  102.         // Create copy.
  103.         for (int i = 0; i < testCase.length; i++)
  104.         {
  105.             numbersCopy[i] = testCase[i].clone();
  106.         }
  107.  
  108.         // Sort
  109.         problem(testCase);
  110.         Arrays.sort(numbersCopy, new numberComparator());
  111.  
  112.         // Compare if both equal
  113.         if (testCase.length != numbersCopy.length)
  114.         {
  115.             return false;
  116.         }
  117.  
  118.         for (int i = 0; i < testCase.length; i++)
  119.         {
  120.             if (testCase[i].length != numbersCopy[i].length)
  121.             {
  122.                 return false;
  123.             }
  124.  
  125.             for (int j = 0; j < testCase[i].length; j++)
  126.             {
  127.                 if (testCase[i][j] != numbersCopy[i][j])
  128.                 {
  129.                     return false;
  130.                 }
  131.             }
  132.         }
  133.  
  134.         return true;
  135.     }
  136.  
  137.     // Very bad way of sorting.
  138.     private static class numberComparator implements Comparator<byte[]>
  139.     {
  140.         @Override
  141.         public int compare(byte[] n1, byte[] n2)
  142.         {
  143.             // Ensure equal length
  144.             if (n1.length < n2.length)
  145.             {
  146.                 byte[] tmp = new byte[n2.length];
  147.                 for (int i = 0; i < n1.length; i++)
  148.                 {
  149.                     tmp[i] = n1[i];
  150.                 }
  151.                 n1 = tmp;
  152.             }
  153.  
  154.             if (n1.length > n2.length)
  155.             {
  156.                 byte[] tmp = new byte[n1.length];
  157.                 for (int i = 0; i < n2.length; i++)
  158.                 {
  159.                     tmp[i] = n2[i];
  160.                 }
  161.                 n2 = tmp;
  162.             }
  163.  
  164.             // Compare digit by digit.
  165.             for (int i = n1.length - 1; i >=0; i--)
  166.             {
  167.                 if (n1[i] < n2[i]) return -1;
  168.                 if (n1[i] > n2[i]) return 1;
  169.             }
  170.  
  171.             return 0;
  172.         }
  173.     }
  174.  
  175.     public static void main(String args[])
  176.     {
  177.         System.out.println("CS 302 -- " + quarter + " -- Lab " + LabNo);
  178.         testProblems();
  179.     }
  180.  
  181.     private static void testProblems()
  182.     {
  183.         int noOfLines = 10000;
  184.  
  185.         System.out.println("-- -- -- -- --");
  186.         System.out.println(noOfLines + " test cases.");
  187.  
  188.         boolean passedAll = true;
  189.  
  190.         for (int i = 1; i <= noOfLines; i++)
  191.         {
  192.             byte[][] testCase =  createTestCase(i);
  193.  
  194.             boolean passed = false;
  195.             boolean exce = false;
  196.  
  197.             try
  198.             {
  199.                 passed = testProblem(testCase);
  200.             }
  201.             catch (Exception ex)
  202.             {
  203.                 passed = false;
  204.                 exce = true;
  205.             }
  206.  
  207.             if (!passed)
  208.             {
  209.                 System.out.println("Test " + i + " failed!" + (exce ? " (Exception)" : ""));
  210.                 passedAll = false;
  211.  
  212.                 break;
  213.             }
  214.         }
  215.  
  216.         if (passedAll)
  217.         {
  218.             System.out.println("All test passed.");
  219.         }
  220.  
  221.     }
  222.  
  223.     private static byte[][] createTestCase(int testNo)
  224.     {
  225.         int maxSize = Math.min(100, testNo) + 5;
  226.         int size = rng.nextInt(maxSize) + 5;
  227.  
  228.         byte[][] numbers = new byte[size][];
  229.  
  230.         for (int i = 0; i < size; i++)
  231.         {
  232.             int digits = rng.nextInt(maxSize) + 1;
  233.             numbers[i] = new byte[digits];
  234.  
  235.             for (int j = 0; j < digits - 1; j++)
  236.             {
  237.                 numbers[i][j] = (byte)rng.nextInt(128);
  238.             }
  239.  
  240.             // Ensures that the most significant digit is not 0.
  241.             numbers[i][digits - 1] = (byte)(rng.nextInt(127) + 1);
  242.         }
  243.  
  244.         return numbers;
  245.     }
  246.  
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement