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

Envío 1329

Problema 0xdd - Ordenar un arreglo pequeño

  • Autor: abatesins
  • Fecha: 2020-10-20 09:44:25 UTC (Hace alrededor de 4 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.027 s 3 KBi
#2
Correcto
0.021 s 3 KBi
#3
Correcto
0.024 s 3 KBi
#4
Correcto
0.027 s 3 KBi
#5
Correcto
0.028 s 3 KBi
#6
Correcto
0.027 s 3 KBi
#7
Correcto
0.018 s 3 KBi
#8
Correcto
0.028 s 3 KBi
#9
Correcto
0.03 s 3 KBi
#10
Correcto
0.03 s 3 KBi
#11
Correcto
0.195 s 3 KBi
#12
Correcto
0.118 s 3 KBi
#13
Correcto
0.134 s 3 KBi
#14
Correcto
0.146 s 3 KBi
#15
Correcto
0.14 s 3 KBi
#16
Correcto
0.161 s 3 KBi
#17
Correcto
0.16 s 3 KBi
#18
Correcto
0.109 s 3 KBi
#19
Correcto
0.119 s 3 KBi
#20
Correcto
0.153 s 3 KBi
Puntos totales: 100 / 100

Código

import time

def bubblesort(arr, num_elements):
  for i in range(num_elements-1):
    for ii in range(0, num_elements - i - 1):
      if (x0 := arr[ii]) > (x1 := arr[ii+1]):
        arr[ii:ii+2] = x1, x0
  return arr

def insertionsort(arr, num_elements):
  for i, x0 in enumerate(arr):
    for ii, x1 in enumerate(arr[:i]):
      if x0 < x1:
        arr[ii+1:i+1] = arr[ii:i]
        arr[ii] = x0
        break
  return arr

def selectionsort(arr, num_elements):
  for i, x0 in enumerate(arr):
    x_min, argmin = x0, i
    for ii, x1 in enumerate(arr[i:], start=i):
      x_min, argmin = (x_min, argmin) if x1 >= x_min else (x1, ii)
    arr[i], arr[argmin] = x_min, x0
  return arr

def mergesort(arr, num_elements):
  if num_elements > 1:
    pivot = num_elements // 2
    left = mergesort(arr[:pivot], pivot)
    right = mergesort(arr[pivot:], num_elements - pivot)
    merged = []
    while left and right:
      lowest = left.pop(0) if left[0] < right[0] else right.pop(0)
      merged.append(lowest)
    merged.extend(left)
    merged.extend(right)
    return merged
  else:
    return arr

if __name__ == "__main__":
  num_elements = int(input())
  arr = list(map(int, input().split(' ')))
  
  # Use a different sort every minute
  sorts = [bubblesort, insertionsort,
           selectionsort, mergesort]
  sort_idx = int(time.time() / 60) % len(sorts)
  sorted_arr = sorts[sort_idx](arr, num_elements)
  print(' '.join(map(str, sorted_arr)))