Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ejercicios {
- private int[] arreglo = {3,4,56,99,1,3,4,56,99,1,3,4,56,99,1,100};
- private static int[] mergeSort(int[] A)
- {
- int half = A.length / 2;
- int[] partA = new int[half];
- int[] partB = new int[A.length - half];
- if (A.length <= 1)
- {
- return A;
- }
- else
- {
- System.arraycopy(A, 0, partA, 0, half);
- System.arraycopy(A, half, partB, 0, A.length - half);
- }
- return merge(mergeSort(partA),mergeSort(partB));
- }
- private static int[] merge(int[] A, int[] B)
- {
- int i = 0;
- int j = 0;
- int[] res = new int[A.length + B.length];
- for (int k = 0; k < res.length; k++)
- {
- if (A.length - i < 1)
- {
- res[k] = B[j];
- j++;
- }
- else
- {
- if (B.length - j < 1)
- {
- res[k] = A[i];
- i++;
- }
- else
- {
- if (A[i] < B[j])
- {
- res[k] = A[i];
- i++;
- }
- else
- {
- res[k] = B[j];
- j++;
- }
- }
- }
- }
- return res;
- }
- public static void main(String[] args) {
- ejercicios res = new ejercicios();
- System.out.print(Arrays.toString(mergeSort(res.arreglo)));
- }
- }
Add Comment
Please, Sign In to add comment