Advertisement
urksiful

Radix Sort 2.0

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