SHARE
TWEET

Untitled

a guest Dec 2nd, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     import java.io.*;
  2.     import java.util.*;
  3.      
  4.     class RadixSort {
  5.       public static void main (String[] args) {
  6.         String[] arr = {"apple", "australia", "algorithm","sell", "olympic","jack","sleep"};
  7.         radixSort(arr,'a','z');
  8.         for(int i=0;i<arr.length;i++){
  9.           System.out.println(arr[i]);
  10.         }
  11.       }
  12.      
  13.       public static void countingSort(String[] arr,int index,char lower,char upper){
  14.         int[] countArray = new int[(upper-lower)+2];
  15.         String[] tempArray = new String[arr.length];
  16.         Arrays.fill(countArray,0);
  17.        
  18.         //increase count for char at index
  19.         for(int i=0;i<arr.length;i++){
  20.           int charIndex = (arr[i].length()-1 < index) ? 0 : ((arr[i].charAt(index) - lower)+1);
  21.           countArray[charIndex]++;
  22.         }
  23.        
  24.         //sum up countArray;countArray will hold last index for the char at each strings index
  25.         for(int i=1;i<countArray.length;i++){
  26.           countArray[i] += countArray[i-1];
  27.         }
  28.        
  29.         for(int i=arr.length-1;i>=0;i--){
  30.           int charIndex = (arr[i].length()-1 < index) ? 0 : (arr[i].charAt(index) - lower)+1;
  31.           tempArray[countArray[charIndex]-1] = arr[i];
  32.           countArray[charIndex]--;
  33.         }
  34.        
  35.         for(int i=0;i<tempArray.length;i++){
  36.           arr[i] = tempArray[i];  
  37.         }
  38.       }
  39.      
  40.       public static void radixSort(String[] arr,char lower,char upper){
  41.         int maxIndex = 0;
  42.         for(int i=0;i<arr.length;i++){
  43.           if(arr[i].length()-1 > maxIndex){
  44.             maxIndex = arr[i].length()-1;
  45.           }
  46.         }
  47.        
  48.         for(int i=maxIndex;i>=0;i--){
  49.           countingSort(arr,i,lower,upper);
  50.         }
  51.       }
  52.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top