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

Envío 6209

Problema 0xde - Ordenar un arreglo grande

  • Autor: rpedrazacoello
  • Fecha: 2022-05-26 00:39:37 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.102 s 17 KBi
#2
Correcto
0.094 s 17 KBi
#3
Correcto
0.116 s 17 KBi
#4
Correcto
0.093 s 17 KBi
#5
Correcto
0.093 s 17 KBi
#6
Correcto
0.1 s 17 KBi
#7
Correcto
0.925 s 100 KBi
#8
Correcto
0.949 s 112 KBi
#9
Correcto
0.704 s 95 KBi
#10
Tiempo límite excedido
1.047 s 114 KBi
#11
Correcto
0.786 s 106 KBi
#12
Correcto
0.78 s 104 KBi
#13
Tiempo límite excedido
1.093 s 91 KBi
#14
Correcto
0.918 s 103 KBi
#15
Correcto
0.772 s 101 KBi
#16
Correcto
0.814 s 101 KBi
#17
Correcto
0.843 s 103 KBi
#18
Correcto
0.823 s 102 KBi
#19
Correcto
0.886 s 102 KBi
#20
Correcto
0.874 s 102 KBi
#21
Correcto
0.825 s 101 KBi
#22
Tiempo límite excedido
1.015 s 108 KBi
#23
Correcto
0.803 s 101 KBi
#24
Correcto
0.934 s 100 KBi
#25
Correcto
0.881 s 101 KBi
#26
Correcto
0.865 s 99 KBi
#27
Correcto
0.959 s 105 KBi
Puntos totales: 89 / 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;
    }
}