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

Envío 5083

Problema 0xde - Ordenar un arreglo grande

  • Autor: cams2692
  • Fecha: 2021-10-12 14:38:48 UTC (Hace alrededor de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.004 s 3 KBi
#2
Correcto
0.006 s 53 KBi
#3
Correcto
0.005 s 18 KBi
#4
Correcto
0.005 s 53 KBi
#5
Correcto
0.006 s 3 KBi
#6
Correcto
0.004 s 10 KBi
#7
Correcto
0.314 s 16 KBi
#8
Correcto
0.379 s 32 KBi
#9
Correcto
0.31 s 19 KBi
#10
Correcto
0.431 s 50 KBi
#11
Correcto
0.336 s 19 KBi
#12
Correcto
0.37 s 21 KBi
#13
Correcto
0.313 s 20 KBi
#14
Correcto
0.319 s 18 KBi
#15
Correcto
0.307 s 18 KBi
#16
Correcto
0.464 s 20 KBi
#17
Correcto
0.461 s 20 KBi
#18
Correcto
0.337 s 16 KBi
#19
Correcto
0.331 s 17 KBi
#20
Correcto
0.324 s 22 KBi
#21
Correcto
0.348 s 18 KBi
#22
Correcto
0.329 s 18 KBi
#23
Correcto
0.331 s 19 KBi
#24
Correcto
0.324 s 18 KBi
#25
Correcto
0.324 s 20 KBi
#26
Correcto
0.287 s 17 KBi
#27
Correcto
0.355 s 18 KBi
Puntos totales: 100 / 100

Código

package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)

	n, _ := reader.ReadString('\n')
	n = strings.ReplaceAll(n, "\n", "")

	numbersS, _ := reader.ReadString('\n')
	numbersS = strings.ReplaceAll(numbersS, "\n", "")
	numbers := strings.Split(numbersS, " ")

	fmt.Println(strings.Join(mergeSort(numbers), " "))
}

func mergeSort(array []string) []string {
	if len(array) == 1 {
		return array
	}

	lenArrayMid := int(len(array) / 2)

	array1 := mergeSort(array[:lenArrayMid])
	array2 := mergeSort(array[lenArrayMid:])

	mergeArray := make([]string, 0)
	i := 0
	j := 0
	for i < len(array1) && j < len(array2) {
		numberOne, _ := strconv.ParseInt(array1[i], 10, 64)
		numberTwo, _ := strconv.ParseInt(array2[j], 10, 64)
		if numberOne < numberTwo {
			mergeArray = append(mergeArray, array1[i])
			i++
		} else {
			mergeArray = append(mergeArray, array2[j])
			j++
		}
	}

	if i < len(array1) {
		mergeArray = append(mergeArray, array1[i:]...)
	} else if j < len(array2) {
		mergeArray = append(mergeArray, array2[j:]...)
	}

	return mergeArray
}