Menu
Coddy logo textTech

Pseudo Code

Lección 4 de 9 del curso Merge Sort - Serie de DSA de Coddy.

mergeSort(array):
   if length(array) <= 1:
      return array
   mid = length(array) / 2
   left = mergeSort(array[0..mid])
   right = mergeSort(array[mid..end])
   return merge(left, right)

merge(left, right):
   result = []
   while left and right both still have elements:
      if left[0] <= right[0]:
         move the front of left into result
      else:
         move the front of right into result
   append whatever remains of left
   append whatever remains of right
   return result

Dos funciones trabajan juntas:

  • mergeSort continúa dividiendo el arreglo hasta que las piezas están trivialmente ordenadas, luego confía en merge para combinarlas.
  • merge recorre dos listas ordenadas a la vez, tomando siempre el elemento frontal más pequeño, de modo que la lista combinada se mantenga ordenada.

Pruébalo tú mismo

Esta lección no incluye un desafío de código.

quiz iconPonte a prueba

Esta lección incluye un breve cuestionario. Empieza la lección para responderlo y registrar tu progreso.

Todas las lecciones de Merge Sort - Serie de DSA