import java.util.ArrayList; import java.util.LinkedList; import java.util.Queue; public class RadixSort{ public static void radixSort(int arr[], int maxDigits){ int exp = 1;//10^0; for(int i =0; i < maxDigits; i++){ ArrayList bucketList[] = new ArrayList[10]; for(int k=0; k < 10; k++){ bucketList[k] = new ArrayList(); } for(int j =0; j < arr.length; j++){ int number = (arr[j]/exp)%10; bucketList[number].add(arr[j]); } exp *= 10; int index =0; for(int k=0; k < 10; k++){ for(int num: (ArrayList<Integer>)bucketList[k]){ arr[index] = num; index++; } } } System.out.println("Sorted numbers"); for(int i =0; i < arr.length; i++){ System.out.print(arr[i] +", "); } } public static void radixSort1( int[] a) { int i, m = a[0], exp = 1, n = a.length; int[] b = new int[10]; for (i = 1; i < n; i++) if (a[i] > m) m = a[i]; while (m / exp > 0) { int[] bucket = new int[10]; for (i = 0; i < n; i++) bucket[(a[i] / exp) % 10]++; for (i = 1; i < 10; i++) bucket[i] += bucket[i - 1]; for (i = n - 1; i >= 0; i--) b[--bucket[(a[i] / exp) % 10]] = a[i]; for (i = 0; i < n; i++) a[i] = b[i]; exp *= 10; } } public static void radixSort(int[] arr){ if(arr.length == 0) return; int[][] np = new int[arr.length][2]; int[] q = new int[0x100]; int i,j,k,l,f = 0; for(k=0;k<4;k++){ for(i=0;i<(np.length-1);i++) np[i][1] = i+1; np[i][1] = -1; for(i=0;i<q.length;i++) q[i] = -1; for(f=i=0;i<arr.length;i++){ j = ((0xFF<<(k<<3))&arr[i])>>(k<<3); if(q[j] == -1) l = q[j] = f; else{ l = q[j]; while(np[l][1] != -1) l = np[l][1]; np[l][1] = f; l = np[l][1]; } f = np[f][1]; np[l][0] = arr[i]; np[l][1] = -1; } for(l=q[i=j=0];i<0x100;i++) for(l=q[i];l!=-1;l=np[l][1]) arr[j++] = np[l][0]; } } public static void radixSort4(int A[], int B[], int n, int b, int r, int C[]){ int j ; int m = b/r; int radix2 = 1; for(int i = 0; i <r; i++){ radix2 *= 2; } for(int i = 0, t = 1; i < m; i++, t*=radix2){ for(j = 0; j < radix2; j++){ C[j] = 0; } for(j = 0; j < n; j++){ C[(A[j]/t)%radix2]++; } for(j = 1; j < radix2; j++){ C[j] = C[j - 1] + C[j]; } for(j = n -1 ; j >= 0; j--){ B[--C[(A[j]/t)%radix2]] = A[j]; } for(j = 0; j < n; j++){ A[j] = B[j]; } } } /** * place elements in data into order * * @param data array of positive integer values */ public static void radixSort2(int [ ] data) { boolean flag = true; int divisor = 1; Queue [ ] buckets = new Queue[10]; for (int i = 0; i < 10; i++) buckets[i] = new LinkedList<Integer>(); while (flag) { flag = false; // first copy the values into buckets for (int i = 0; i < data.length; i++) { int hashIndex = (data[i] / divisor) % 10; if (hashIndex > 0) flag = true; ((LinkedList<Integer>)buckets[hashIndex]).addLast(new Integer(data[i])); } // then copy the values back into vector divisor *= 10; int i = 0; for (int j = 0; j < 10; j++) { while (! buckets[j].isEmpty()) { Integer ival = (Integer) ((LinkedList<Integer>)buckets[j]).getFirst(); ((LinkedList<Integer>)buckets[j]).removeFirst(); data[i++] = ival.intValue(); } } } } // Sort the numbers beginning with least-significant digit public static int[] radixSort3(int[] input){ // Largest place for a 32-bit int is the 1 billion's place for(int place=1; place <= 1000000000; place *= 10){ // Use counting sort at each digit's place input = countingSort(input, place); } return input; } private static int[] countingSort(int[] input, int place){ int[] out = new int[input.length]; int[] count = new int[10]; for(int i=0; i < input.length; i++){ int digit = getDigit(input[i], place); count[digit] += 1; } for(int i=1; i < count.length; i++){ count[i] += count[i-1]; } for(int i = input.length-1; i >= 0; i--){ int digit = getDigit(input[i], place); out[count[digit]-1] = input[i]; count[digit]--; } return out; } private static int getDigit(int value, int digitPlace){ return ((value/digitPlace ) % 10); } public static void main(String[] args){ int i; int[] arr = new int[10]; int[] arr1 = new int[10]; int[] arr2 = new int[256]; System.out.print("original: "); for(i=0;i<arr.length;i++){ arr[i] = (int)(Math.random() * 1024); System.out.print(arr[i] + " "); } //radixSort(arr); //radixSort(arr, 4); //radixSort2(arr); radixSort4(arr, arr1, 10, 32, 8, arr2); System.out.print("\nsorted: "); for(i=0;i<arr.length;i++) System.out.print(arr[i] + " "); System.out.println("\nDone ;-)"); } }