Guest User

Untitled

a guest
Jul 21st, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. public class ejercicios {
  2.  
  3.     private int[] arreglo = {3,4,56,99,1,3,4,56,99,1,3,4,56,99,1,100};
  4.    
  5.     private static int[]  mergeSort(int[] A)
  6.     {
  7.         int half = A.length / 2;
  8.         int[] partA = new int[half];
  9.         int[] partB = new int[A.length - half];
  10.        
  11.         if (A.length <= 1)
  12.         {
  13.             return A;
  14.         }
  15.         else
  16.         {                      
  17.             System.arraycopy(A, 0, partA, 0, half);
  18.             System.arraycopy(A, half, partB, 0, A.length - half);
  19.            
  20.         }
  21.        
  22.         return merge(mergeSort(partA),mergeSort(partB));
  23.     }
  24.    
  25.     private static int[]  merge(int[] A, int[] B)
  26.     {
  27.         int i = 0;
  28.         int j = 0;
  29.         int[] res = new int[A.length + B.length];
  30.        
  31.         for (int k = 0; k < res.length; k++)
  32.         {
  33.             if (A.length - i < 1)
  34.             {
  35.                 res[k] = B[j];
  36.                 j++;
  37.             }
  38.             else
  39.             {
  40.                 if (B.length - j < 1)
  41.                 {
  42.                     res[k] = A[i];
  43.                     i++;
  44.                 }
  45.                 else
  46.                 {
  47.                     if (A[i] < B[j])
  48.                     {
  49.                         res[k] = A[i];
  50.                         i++;
  51.                     }
  52.                     else
  53.                     {
  54.                         res[k] = B[j];
  55.                         j++;
  56.                     }
  57.                 }
  58.             }
  59.         }
  60.        
  61.         return res;
  62.     }
  63.    
  64.    
  65.    
  66.    
  67.    
  68.     public static void main(String[] args) {
  69.        
  70.         ejercicios res = new ejercicios();
  71.        
  72.         System.out.print(Arrays.toString(mergeSort(res.arreglo)));
  73.        
  74.        
  75.        
  76.  
  77.     }
  78.  
  79. }
Add Comment
Please, Sign In to add comment