0
Python-3.xでMergeSortに問題があります。これは、アルゴリズムがソートを行わないということです。私の間違いは何ですか?手伝って頂けますか?Python-3でのMergeSortの問題
コードは次のとおりです。
def mergeSort(lista):
\t #Precondición: elem comparables
\t #Postcondición: lista ordenada
\t if len(lista) < 2:
\t \t return lista
\t medio = len(lista) // 2
\t izq = mergeSort(lista[:medio])
\t der = mergeSort(lista[medio:])
\t return merge(izq,der)
def merge(lista1,lista2):
\t #Precondición: listas ordenadas
\t #Postcondición: retorna lista con elem de lista1 y lista2
\t i,j = 0,0
\t resultado = []
\t while i<len(lista1) and j<len(lista2):
\t \t if (lista1[i] < lista2[j]):
\t \t \t resultado.append(lista1[i])
\t \t \t i += 1
\t \t else:
\t \t \t resultado.append(lista2[j])
\t \t \t j += 1
\t resultado += lista1[i:]
\t resultado += lista2[j:]
\t return resultado
L = [6,7,-1,0,5,2,3,8]
mergeSort(L)
print(L)
、結果は次のとおりです。
[6, 7, -1, 0, 5, 2, 3, 8]
[Finished in 0.2s]
(https://stackoverflow.com/questions/18761766/mergesort-python)をご覧ください。 –
[Mergesort Python]の可能な複製(https://stackoverflow.com/questions/18761766/mergesort-python) –