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

Envío 6214

Problema 0xde - Ordenar un arreglo grande

  • Autor: rpedrazacoello
  • Fecha: 2022-05-26 00:47:44 UTC (Hace casi 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.117 s 17 KBi
#2
Correcto
0.1 s 16 KBi
#3
Correcto
0.096 s 17 KBi
#4
Correcto
0.099 s 17 KBi
#5
Correcto
0.113 s 17 KBi
#6
Correcto
0.107 s 16 KBi
#7
Tiempo límite excedido
1.023 s 112 KBi
#8
Tiempo límite excedido
1.144 s 95 KBi
#9
Correcto
0.735 s 100 KBi
#10
Correcto
0.926 s 104 KBi
#11
Correcto
0.779 s 107 KBi
#12
Correcto
0.848 s 106 KBi
#13
Correcto
0.779 s 97 KBi
#14
Tiempo límite excedido
1.033 s 93 KBi
#15
Correcto
0.861 s 105 KBi
#16
Correcto
0.808 s 106 KBi
#17
Correcto
0.868 s 105 KBi
#18
Tiempo límite excedido
1.017 s 95 KBi
#19
Correcto
0.681 s 100 KBi
#20
Correcto
0.684 s 99 KBi
#21
Tiempo límite excedido
1.055 s 95 KBi
#22
Correcto
0.747 s 103 KBi
#23
Correcto
0.753 s 96 KBi
#24
Tiempo límite excedido
1.002 s 105 KBi
#25
Correcto
0.879 s 101 KBi
#26
Correcto
0.885 s 105 KBi
#27
Tiempo límite excedido
1.053 s 95 KBi
Puntos totales: 75 / 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;
    }
}