Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //class Random is used to generate randomized input
- import java.util.Date;
- import java.util.Random;
- public class BaseConvTest
- {
- private static String digitList = "0123456789abcdefghijklmnopqrstuvwxyz";
- public static void main(String[] args)
- {
- Random x = new Random();
- String[] S = new String[1000];
- SpecialSortingBaseConv baseConv = new SpecialSortingBaseConv();
- for (int j = 0; j<S.length; j++)
- {
- S[j] = "";
- for (int i = 0; i<(x.nextInt(5)+1); i++)
- {
- S[j] += digitList.charAt(x.nextInt(36));
- }
- }
- System.out.println("Before sorting: ");
- for(String t:S)
- {
- System.out.print(t+" ");
- }
- long timebuf1 = (new Date()).getTime();
- baseConv.quickSortBaseConv(S, 0, S.length-1);
- long smallTime1 = (new Date()).getTime() - timebuf1;
- System.out.println("\nAfter sorting: ");
- for(String s:S)
- {
- System.out.print(s+" ");
- }
- System.out.println("\nTime taken for quicksort: " + smallTime1 + " ms");
- }
- }
- import java.util.Date;
- import java.util.Random;
- public class RadixTest
- {
- private static String digitList = "0123456789abcdefghijklmnopqrstuvwxyz";
- public static void main(String[] args)
- {
- Random x = new Random();
- String[] S = new String[1000];
- SpecialSortingRadix radix = new SpecialSortingRadix();
- for (int j = 0; j < S.length; j++)
- {
- S[j] = "";
- for (int i = 0; i < (x.nextInt(5) + 1); i++)
- {
- S[j] += digitList.charAt(x.nextInt(36));
- }
- }
- System.out.println("Before sorting: ");
- for (String t : S)
- {
- System.out.print(t + " ");
- }
- long timebuf1 = (new Date()).getTime();
- radix.quickSortRadix(S, 0, S.length - 1);
- long smallTime1 = (new Date()).getTime() - timebuf1;
- System.out.println("\nAfter sorting: ");
- for (String s : S) {
- System.out.print(s + " ");
- }
- System.out.println("\nTime taken for quicksort: " + smallTime1 + " ms");
- }
- }
- public class SpecialSortingBaseConv
- {
- private static String digitList = "0123456789abcdefghijklmnopqrstuvwxyz";
- public SpecialSortingBaseConv()
- {
- }
- public static long baseConv(String S)
- {
- long result = 0;
- for (int i = 0; i < S.length(); i++)
- {
- result += digitList.indexOf(S.charAt(i)) * (long) Math.pow(36, S.length() - i);
- }
- return result;
- }
- public static int comparisonBaseConv (String a, String b)
- {
- int max = 0;
- long aConv = baseConv(a);
- long bConv = baseConv(b);
- if (aConv>=bConv)
- max = 1;
- else
- max = 2;
- return max;
- }
- public static int partitionBaseConv(String[] S, int left, int right, int pivotIndex)
- {
- String pivotVal = S[pivotIndex];
- String temp = S[pivotIndex];
- S[pivotIndex] = S[right];
- S[right] = temp;
- int store = left;
- for (int i = left; i<right; i++)
- {
- if (comparisonBaseConv(S[i],pivotVal)==1)
- {
- String temp2 = S[i];
- S[i] = S[store];
- S[store] = temp2;
- store++;
- }
- }
- String temp1 = S[store];
- S[store] = S[right];
- S[right] = temp1;
- return store;
- }
- public static void quickSortBaseConv (String[] S, int left, int right)
- {
- if(left<right)
- {
- int pivotIndex = (int) (left+Math.random()*(right-left));
- int pivotNewIndex = partitionBaseConv(S,left,right,pivotIndex);
- quickSortBaseConv(S,left,pivotNewIndex - 1);
- quickSortBaseConv(S,pivotNewIndex + 1, right);
- }
- }
- }
- import java.util.Random;
- public class SpecialSortingRadix
- {
- private static String digitList = "0123456789abcdefghijklmnopqrstuvwxyz";
- public SpecialSortingRadix()
- {
- }
- public static int comparisonRadix(String a, String b)
- {
- boolean ifEqual = true;
- int max = 0;
- if (a.length()>b.length())
- max = 1;
- if (a.length()<b.length())
- max = 2;
- if (a.length()==b.length())
- {
- int cConsider = 0;
- while ((cConsider<a.length())&&(ifEqual == true))
- {
- if (digitList.indexOf(a.charAt(cConsider)) == digitList.indexOf(b.charAt(cConsider)))
- {
- cConsider++;
- }
- else
- {
- if (digitList.indexOf(a.charAt(cConsider)) > digitList.indexOf(b.charAt(cConsider)))
- {
- ifEqual = false;
- max = 1;
- }
- else
- {
- ifEqual = false;
- max = 2;
- }
- }
- }
- if (ifEqual == true)
- max = 1;
- }
- return max;
- }
- public static int partitionRadix(String[] S, int left, int right, int pivotIndex)
- {
- String pivotVal = S[pivotIndex];
- String temp = S[pivotIndex];
- S[pivotIndex] = S[right];
- S[right] = temp;
- int store = left;
- for (int i = left; i<right; i++)
- {
- if (comparisonRadix(S[i],pivotVal)==1)
- {
- String temp2 = S[i];
- S[i] = S[store];
- S[store] = temp2;
- store++;
- }
- }
- String temp1 = S[store];
- S[store] = S[right];
- S[right] = temp1;
- return store;
- }
- public static void quickSortRadix (String[] S, int left, int right)
- {
- if(left<right)
- {
- int pivotIndex = (int) (left+Math.random()*(right-left));
- int pivotNewIndex = partitionRadix(S,left,right,pivotIndex);
- quickSortRadix(S,left,pivotNewIndex - 1);
- quickSortRadix(S,pivotNewIndex + 1, right);
- }
- }
- }
Add Comment
Please, Sign In to add comment