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

Envío 6208

Problema 0xdd - Ordenar un arreglo pequeño

  • Autor: rpedrazacoello
  • Fecha: 2022-05-26 00:39:21 UTC (Hace más de 2 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.139 s 17 KBi
#2
Correcto
0.101 s 16 KBi
#3
Correcto
0.102 s 16 KBi
#4
Correcto
0.103 s 17 KBi
#5
Correcto
0.094 s 16 KBi
#6
Correcto
0.109 s 16 KBi
#7
Correcto
0.098 s 16 KBi
#8
Correcto
0.11 s 17 KBi
#9
Correcto
0.103 s 16 KBi
#10
Correcto
0.104 s 17 KBi
#11
Correcto
0.182 s 22 KBi
#12
Correcto
0.171 s 21 KBi
#13
Correcto
0.18 s 22 KBi
#14
Correcto
0.197 s 25 KBi
#15
Correcto
0.193 s 23 KBi
#16
Correcto
0.175 s 21 KBi
#17
Correcto
0.147 s 21 KBi
#18
Correcto
0.179 s 21 KBi
#19
Correcto
0.2 s 22 KBi
#20
Correcto
0.182 s 24 KBi
Puntos totales: 100 / 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;
    }
}