█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 6212

Problema 0xde - Ordenar un arreglo grande

  • Autor: rpedrazacoello
  • Fecha: 2022-05-26 00:47:02 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.12 s 16 KBi
#2
Correcto
0.09 s 17 KBi
#3
Correcto
0.164 s 14 KBi
#4
Correcto
0.116 s 16 KBi
#5
Correcto
0.098 s 17 KBi
#6
Correcto
0.116 s 17 KBi
#7
Correcto
0.897 s 104 KBi
#8
Correcto
0.761 s 99 KBi
#9
Correcto
0.817 s 100 KBi
#10
Correcto
0.885 s 99 KBi
#11
Correcto
0.811 s 105 KBi
#12
Correcto
0.792 s 106 KBi
#13
Correcto
0.771 s 102 KBi
#14
Correcto
0.808 s 106 KBi
#15
Correcto
0.879 s 101 KBi
#16
Correcto
0.769 s 106 KBi
#17
Correcto
0.889 s 111 KBi
#18
Correcto
0.804 s 104 KBi
#19
Correcto
0.831 s 100 KBi
#20
Correcto
0.826 s 101 KBi
#21
Correcto
0.876 s 101 KBi
#22
Correcto
0.949 s 103 KBi
#23
Correcto
0.835 s 102 KBi
#24
Tiempo límite excedido
1.039 s 102 KBi
#25
Correcto
0.895 s 103 KBi
#26
Correcto
0.857 s 99 KBi
#27
Correcto
0.995 s 105 KBi
Puntos totales: 97 / 100

Código

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        int[] array = new int[n];
        for (int i=0; i<n; i++){
            array[i] = scanner.nextInt();
        }

        int[] orderedArray = mergeSort(array, 0, n-1);

        for(int i=0; i<n; i++){
            System.out.print(orderedArray[i] + " ");
        }
    }

    private static int[] mergeSort(int[] array, int start, int end){
        if(start == end){
            return new int[]{array[start]};
        }

        int middle = (end+start)/2;
        int startLeft = start;
        int endLeft = middle;
        int startRight = middle+1;
        int endRight = end;

        int[] orderedLeft = mergeSort(array, startLeft, endLeft);
        int[] orderedRight = mergeSort(array, startRight, endRight);

        int leftPointer = 0;
        int rightPointer = 0;

        int n = end - start + 1;
        int[] orderedArray = new int[n];
        int orderedArrayPointer = 0;
        while (orderedArrayPointer < n){

            int leftValue = leftPointer<orderedLeft.length ? orderedLeft[leftPointer] : Integer.MAX_VALUE;
            int rightValue = rightPointer<orderedRight.length ? orderedRight[rightPointer] : Integer.MAX_VALUE;

            if(leftValue<rightValue){
                orderedArray[orderedArrayPointer] = leftValue;
                leftPointer++;
            } else {
                orderedArray[orderedArrayPointer] = rightValue;
                rightPointer++;
            }
            orderedArrayPointer++;
        }

        return orderedArray;
    }
}