Advertisement
urksiful

Radix Sort

Nov 11th, 2015
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.92 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3.  
  4. /**
  5.  *
  6.  * @author urksiful
  7.  */
  8. public class Radix {
  9. static int comp=0, mov=0;
  10.     public static void main(String[] args) {
  11.         ArrayList<Integer> lista = new ArrayList<Integer>();
  12.         int[] tmp = new int[100];
  13.        
  14.         for (int i = 0; i < 100; i++) {
  15.             tmp[i] = (int) (Math.random() * 998) + 1;
  16.             System.out.print(tmp[i] + " ");
  17.         }
  18.         System.out.println("");
  19.         Ordena(tmp);
  20.         for (int i = 0; i < 100; i++) {
  21.             lista.add(tmp[i]);
  22.             System.out.print(lista.get(i) + " ");
  23.         }
  24.         System.out.println("\nComparaciones: "+comp+"\nMovimientos: "+mov);
  25.     }
  26.  
  27.     public static void Ordena(int[] n) {
  28.         if (n.length == 0) {
  29.             return;
  30.         }
  31.         int[][] np = new int[n.length][2];
  32.         int[] q = new int[0x100];
  33.         int i, j, k, l, f = 0;
  34.         for (k = 0; k < 4; k++) {
  35.             for (i = 0; i < (np.length - 1); i++) {
  36.                 np[i][1] = i + 1;
  37.             }
  38.             np[i][1] = -1;
  39.             for (i = 0; i < q.length; i++) {
  40.                 q[i] = -1;
  41.             }
  42.             for (f = i = 0; i < n.length; i++) {
  43.                 j = ((0xFF << (k << 3)) & n[i]) >> (k << 3);
  44.                 if (q[j] == -1) {
  45.                     l = q[j] = f;
  46.                 } else {
  47.                     l = q[j];
  48.                     while (np[l][1] != -1) {
  49.                         l = np[l][1];
  50.                     }
  51.                     np[l][1] = f;
  52.                     l = np[l][1];
  53.                 }
  54.                 f = np[f][1];
  55.                 np[l][0] = n[i];
  56.                 np[l][1] = -1;
  57.             }
  58.             comp++;
  59.             for (l = q[i = j = 0]; i < 0x100; i++) {
  60.                 for (l = q[i]; l != -1; l = np[l][1]) {
  61.                     n[j++] = np[l][0];
  62.                     mov++;
  63.                 }
  64.             }
  65.         }
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement