Esempio n. 1
0
File: sorts.py Progetto: Qoter/sorts
def insertion_sort(data, key=default_key, reverse=False):
    for i in range(len(data)):
        insert_elem = data[i]
        j = i
        while (j > 0) and _cmp(key(insert_elem), key(a[j - 1]), reverse) < 0:
            swap(data, j, j - 1)
            j -= 1
    return data
Esempio n. 2
0
File: sorts.py Progetto: Qoter/sorts
def select_sort(data, key=default_key, reverse=False):
    for i in range(len(data)):
        minimum = i
        for j in range(i, len(data)):
            if _cmp(key(data[j]), key(data[minimum]), reverse) < 0:
                minimum = j
        swap(data, minimum, i)
    return data
Esempio n. 3
0
File: sorts.py Progetto: Qoter/sorts
def _shell_sort(data, step_table, key=default_key, reverse=False):
    if len(data) == 0:
        return data
    for increment in step_table:
        for i in range(increment, len(data)):
            for j in range(i, increment - 1, -increment):
                if _cmp(key(data[j - increment]), key(data[j]), reverse) < 0:
                    break
                swap(data, j, j - increment)
    return data
Esempio n. 4
0
File: sorts.py Progetto: Qoter/sorts
def _scatter(data, key, left, right, reverse):
    """function scatters elements on two parts (used _qsort)"""
    pivotIndex = (left + right) // 2
    pivot = data[pivotIndex]
    swap(data, pivotIndex, right)
    store_index = left
    for i in range(left, right):
        if _cmp(key(data[i]), key(pivot), reverse) < 0:
            swap(data, i, store_index)
            store_index += 1
    swap(data, store_index, right)
    return store_index
Esempio n. 5
0
File: sorts.py Progetto: Qoter/sorts
def bubble_sport(data, key=default_key, reverse=False):
    for i in range(len(data)):
        for j in range(len(data) - i - 1):
            if _cmp(key(data[j + 1]), key(data[j]), reverse) < 0:
                swap(data, j, j + 1)
    return data
Esempio n. 6
0
File: sorts.py Progetto: Qoter/sorts
 def switch(parent_i, child_i):
     if _cmp(key(data[parent_i]), key(data[child_i]), reverse) < 0:
         swap(data, parent_i, child_i)
         return True