Pseudo Code
Lição 4 de 9 do curso Merge Sort - Série DSA da 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 resultDuas funções trabalham juntas:
- mergeSort continua dividindo o array até que as partes estejam trivialmente ordenadas, então conta com o merge para combiná-las.
- merge percorre duas listas ordenadas simultaneamente, sempre pegando o menor elemento frontal, para que a lista combinada permaneça ordenada.
Experimente você mesmo
Esta lição não inclui um desafio de código.
Esta lição inclui um quiz rápido. Comece a lição para respondê-lo e acompanhar seu progresso.
Todas as lições de Merge Sort - Série DSA
2The Algorithm
How it works?Pseudo CodeImplementation (Part 1)Implementation (Part 2)