-2

Как должна выглядеть функция сортировки массива алгоритмом слияния через yield.

1
  • 3
    должна выглядеть хорошо. 29 мая 2017 в 6:24

1 ответ 1

1

Что-то типа:

def merge(left, right):
    left = list(left)
    right = list(right)
    while left and right:
        if left[0] < right[0]:
            yield left.pop(0)
        else:
            yield right.pop(0)
    yield from left
    yield from right


def sort(l):
    t = next(l, None), next(l, None)
    if t[0] is None:
        return None
    if t[1] is None:
        yield t[0]
        return None
    l = list(t) + list(l)
    length = len(l)
    if length >= 2:
        mid = int(length / 2)
        lst = merge(sort(iter(l[:mid])), sort(iter(l[mid:])))
    yield from lst


L = [54,26,93,17,77,31,44,55,20]
IT_L = sort(iter(L))
print(IT_L, list(IT_L))

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.