Answers for "comb sort"

0

comb sort

def combsort_inplace(data):
    length = len(data)
    shrink = 1.3
    _gap = length
    sorted = False
    while not sorted:
        # Python has no builtin 'floor' function, so we/I just have one variable (_gap) to be shrunk,
        # and an integer variable (gap) to store the truncation (of the other variable) in and
        # to use for stuff pertaining to indexing
        _gap /= shrink
        # gap = np.floor(_gap)
        gap = int(_gap)
        if gap <= 1:
            sorted = True
            gap = 1
        # equivalent to `i = 0; while (i + gap) < length: ...{loop body}... i += 1`
        for i in range(length - gap):
            sm = gap + i
            if data[i] > data[sm]:
                # because Python is very nice, this accomplishes the swap
                data[i], data[sm] = data[sm], data[i]
                sorted = False


def combsort(data):
    length = len(data)
    shrink = 1.3
    _gap = length
    out = list(data)
    is_sorted = False
    while not is_sorted:
        _gap /= shrink
        gap = int(_gap)
        if gap <= 1:
            is_sorted = True
            gap = 1
        for i in range(length - gap):
            sm = gap + i
            if out[i] > out[sm]:
                out[i], out[sm] = out[sm], out[i]
                is_sorted = False
    return out
Posted by: Guest on April-13-2021

Python Answers by Framework

Browse Popular Code Answers by Language