DoNgocDuc

SpecialSort

Aug 3rd, 2012
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.78 KB | None | 0 0
  1. //class Random is used to generate randomized input
  2. import java.util.Date;
  3. import java.util.Random;
  4. public class BaseConvTest
  5. {
  6.         private static String digitList = "0123456789abcdefghijklmnopqrstuvwxyz";
  7.        
  8.     public static void main(String[] args)
  9.     {
  10.         Random x = new Random();
  11.         String[] S = new String[1000];
  12.                 SpecialSortingBaseConv baseConv = new SpecialSortingBaseConv();
  13.                
  14.         for (int j = 0; j<S.length; j++)
  15.         {
  16.             S[j] = "";
  17.             for (int i = 0; i<(x.nextInt(5)+1); i++)
  18.                         {
  19.                             S[j] += digitList.charAt(x.nextInt(36));
  20.                         }
  21.         }
  22.                
  23.                 System.out.println("Before sorting: ");
  24.                 for(String t:S)
  25.                 {
  26.                     System.out.print(t+" ");
  27.                 }
  28.                
  29.                 long timebuf1 = (new Date()).getTime();
  30.                 baseConv.quickSortBaseConv(S, 0, S.length-1);
  31.                 long smallTime1 = (new Date()).getTime() - timebuf1;
  32.        
  33.                 System.out.println("\nAfter sorting: ");
  34.         for(String s:S)
  35.                 {
  36.                     System.out.print(s+" ");
  37.                 }
  38.                
  39.                 System.out.println("\nTime taken for quicksort: " + smallTime1 + " ms");
  40.     }  
  41. }
  42.  
  43.  
  44.  
  45. import java.util.Date;
  46. import java.util.Random;
  47. public class RadixTest
  48. {
  49.     private static String digitList = "0123456789abcdefghijklmnopqrstuvwxyz";
  50.    
  51.     public static void main(String[] args)
  52.     {
  53.         Random x = new Random();
  54.         String[] S = new String[1000];
  55.         SpecialSortingRadix radix = new SpecialSortingRadix();
  56.        
  57.         for (int j = 0; j < S.length; j++)
  58.             {
  59.                 S[j] = "";
  60.                 for (int i = 0; i < (x.nextInt(5) + 1); i++)
  61.                 {
  62.                     S[j] += digitList.charAt(x.nextInt(36));
  63.                 }
  64.             }
  65.         System.out.println("Before sorting: ");
  66.         for (String t : S)
  67.         {
  68.             System.out.print(t + " ");
  69.         }
  70.         long timebuf1 = (new Date()).getTime();
  71.         radix.quickSortRadix(S, 0, S.length - 1);
  72.         long smallTime1 = (new Date()).getTime() - timebuf1;
  73.  
  74.         System.out.println("\nAfter sorting: ");
  75.         for (String s : S) {
  76.             System.out.print(s + " ");
  77.         }
  78.  
  79.         System.out.println("\nTime taken for quicksort: " + smallTime1 + " ms");
  80.     }
  81. }
  82.  
  83.  
  84.  
  85.  
  86. public class SpecialSortingBaseConv
  87. {
  88.         private static String digitList = "0123456789abcdefghijklmnopqrstuvwxyz";
  89.        
  90.     public SpecialSortingBaseConv()
  91.     {
  92.     }
  93.        
  94.     public static long baseConv(String S)
  95.     {
  96.         long result = 0;
  97.         for (int i = 0; i < S.length(); i++)
  98.         {
  99.             result += digitList.indexOf(S.charAt(i)) * (long) Math.pow(36, S.length() - i);
  100.         }
  101.         return result;
  102.     }
  103.    
  104.         public static int comparisonBaseConv (String a, String b)
  105.         {
  106.         int max = 0;
  107.         long aConv = baseConv(a);
  108.         long bConv = baseConv(b);
  109.         if (aConv>=bConv)
  110.             max = 1;
  111.         else
  112.             max = 2;
  113.         return max;
  114.     }
  115.    
  116.     public static int partitionBaseConv(String[] S, int left, int right, int pivotIndex)
  117.     {
  118.         String pivotVal = S[pivotIndex];
  119.        
  120.         String temp = S[pivotIndex];
  121.         S[pivotIndex] = S[right];
  122.         S[right] = temp;
  123.        
  124.         int store = left;
  125.         for (int i = left; i<right; i++)
  126.         {
  127.             if (comparisonBaseConv(S[i],pivotVal)==1)
  128.             {
  129.                 String temp2 = S[i];
  130.                 S[i] = S[store];
  131.                 S[store] = temp2;
  132.                 store++;
  133.             }
  134.         }  
  135.         String temp1 = S[store];
  136.         S[store] = S[right];
  137.         S[right] = temp1;
  138.         return store;
  139.     }
  140.  
  141.     public static void quickSortBaseConv (String[] S, int left, int right)
  142.     {
  143.         if(left<right)
  144.         {
  145.             int pivotIndex = (int) (left+Math.random()*(right-left));
  146.             int pivotNewIndex = partitionBaseConv(S,left,right,pivotIndex);
  147.             quickSortBaseConv(S,left,pivotNewIndex - 1);
  148.             quickSortBaseConv(S,pivotNewIndex + 1, right);
  149.         }
  150.     }
  151.  
  152. }
  153.  
  154.  
  155.  
  156.  
  157. import java.util.Random;
  158. public class SpecialSortingRadix
  159. {
  160.     private static String digitList = "0123456789abcdefghijklmnopqrstuvwxyz";
  161.        
  162.         public SpecialSortingRadix()
  163.         {
  164.            
  165.         }
  166.    
  167.     public static int comparisonRadix(String a, String b)
  168.     {
  169.         boolean ifEqual = true;
  170.         int max = 0;
  171.        
  172.         if (a.length()>b.length())
  173.             max = 1;
  174.         if (a.length()<b.length())
  175.             max = 2;
  176.        
  177.         if (a.length()==b.length())
  178.         {
  179.             int cConsider = 0;
  180.             while ((cConsider<a.length())&&(ifEqual == true))
  181.             {
  182.                 if (digitList.indexOf(a.charAt(cConsider)) == digitList.indexOf(b.charAt(cConsider)))
  183.                 {
  184.                     cConsider++;
  185.                 }
  186.                 else
  187.                 {
  188.                     if (digitList.indexOf(a.charAt(cConsider)) > digitList.indexOf(b.charAt(cConsider)))
  189.                     {
  190.                         ifEqual = false;
  191.                         max = 1;
  192.                     }
  193.                     else
  194.                     {
  195.                         ifEqual = false;
  196.                         max = 2;
  197.                     }
  198.                 }
  199.             }
  200.            
  201.             if (ifEqual == true)
  202.                 max = 1;
  203.         }
  204.         return max;
  205.     }
  206.    
  207.     public static int partitionRadix(String[] S, int left, int right, int pivotIndex)
  208.     {
  209.         String pivotVal = S[pivotIndex];
  210.        
  211.         String temp = S[pivotIndex];
  212.         S[pivotIndex] = S[right];
  213.         S[right] = temp;
  214.        
  215.         int store = left;
  216.         for (int i = left; i<right; i++)
  217.         {
  218.             if (comparisonRadix(S[i],pivotVal)==1)
  219.             {
  220.                 String temp2 = S[i];
  221.                 S[i] = S[store];
  222.                 S[store] = temp2;
  223.                 store++;
  224.             }
  225.         }  
  226.         String temp1 = S[store];
  227.         S[store] = S[right];
  228.         S[right] = temp1;
  229.         return store;
  230.     }
  231.  
  232.     public static void quickSortRadix (String[] S, int left, int right)
  233.     {
  234.         if(left<right)
  235.         {
  236.             int pivotIndex = (int) (left+Math.random()*(right-left));
  237.             int pivotNewIndex = partitionRadix(S,left,right,pivotIndex);
  238.             quickSortRadix(S,left,pivotNewIndex - 1);
  239.             quickSortRadix(S,pivotNewIndex + 1, right);
  240.         }
  241.     }
  242.    
  243. }
Add Comment
Please, Sign In to add comment