Exemple #1
0
def radixSort(arr):
    i = 0  # 初始为个位排序
    n = 1  # 最小的位数置为1(包含0)

    max_num = max(arr)
    # 判断得到最大数是几位数
    while max_num > 10**n:
        n += 1

    while i < n:
        # 用字典构建桶
        bucket = {}
        # 将每个桶置空
        for x in range(10):
            bucket.setdefault(x, [])
        for x in arr:
            radix = int((x / (10**i)) % 10)
            # 将对应的数组元素加入到相应位基数的桶中
            bucket[radix].append(x)
        j = 0
        for k in range(10):
            # 若桶不为空
            if len(bucket[k]) != 0:
                for y in bucket[k]:
                    arr[j] = y
                    j += 1
        i += 1

        draw(arr, "Radix-Sort")
Exemple #2
0
def quickSort(arr, start, end):
    # 建立递归终止条件
    if start >= end:
        return

    # low为序列左边要移动的游标 ,last为序列右边要移动的游标
    low = start
    last = end

    # 将起始元素设为要寻找位置的基准元素
    mid_num = arr[start]

    while low < last:
        # 当low与last未重合,并且比基准元素要大,就将游标向左移动
        while low < last and arr[last] >= mid_num:
            last -= 1
        # 如果比基准元素小,就跳出循环,并且把其放在基准元素左边
        arr[low] = arr[last]

        # 当low与last未重合,并且比基准元素要小,就将游标向右移动
        while low < last and arr[low] < mid_num:
            low += 1
        # 如果比基准元素大,就跳出循环,并且把其放在基准元素右边
        arr[last] = arr[low]

    # 当low与last相等,就是mid_num的排序位置
    arr[low] = mid_num

    draw(arr, "Quick-Sort")

    # 然后对排序好的元素左右两边的序列进行递归
    quickSort(arr, start, low - 1)  # 对左边的序列进行递归
    quickSort(arr, low + 1, end)  # 对右边的序列进行递归
Exemple #3
0
def insertSort(arr, length):
    for i in range(1, length):
        if arr[i - 1] > arr[i]:
            tmp = arr[i]
            j = i - 1
            # 移位操作
            while arr[j] > tmp and j >= 0:
                arr[j + 1] = arr[j]
                j -= 1
            arr[j + 1] = tmp

            draw(arr, "Insert-Sort")
Exemple #4
0
def directSelectSort(arr, length):
    for i in range(0, length - 1):
        k = i
        j = i + 1
        # 在当前无序区寻找最小值
        for j in range(j, length):
            if arr[j] < arr[k]:
                k = j
        if k != i:
            arr[i], arr[k] = arr[k], arr[i]

        draw(arr, "Direct-Select-Sort")
Exemple #5
0
def shellSort(arr, length):
    gap = length // 2  # 增量设置初值
    while gap > 0:
        for i in range(gap, length):
            tmp = arr[i]
            j = i - gap
            # 对间隔为gap的元素组进行排序
            while j >= 0 and tmp < arr[j]:
                arr[j + gap] = arr[j]
                j -= gap
            arr[j + gap] = tmp
        gap = gap // 2
        draw(arr, "Shell-Sort")
Exemple #6
0
def bubbleSort(arr, length):
    for i in range(0, length - 1):
        is_sort = True  # 标志判断
        j = length - 1
        while j > i:
            if arr[j] < arr[j - 1]:
                arr[j], arr[j - 1] = arr[j - 1], arr[j]
                is_sort = False
            j -= 1
        # 如果没有发生交换,排序结束
        if is_sort:
            return

        draw(arr, "Bubble-Sort")
Exemple #7
0
def binaryInsertSort(arr, length):
    for i in range(1, length):
        tmp = arr[i]
        low = 0
        high = i - 1
        # 折半查找
        while low <= high:
            mid = (low + high) // 2
            if arr[i] < arr[mid]:
                high = mid - 1
            else:
                low = mid + 1
        # 移位操作
        j = i - 1
        while j >= low:
            arr[j + 1] = arr[j]
            j -= 1
        arr[high + 1] = tmp

        draw(arr, "Binary-Insert-Sort")
Exemple #8
0
def fetch_nutrient(user_id,
                   nutrients=['Index'],
                   date=None,
                   limit=None,
                   printing=False):
    # returns health index by default
    # foods with fields Nutrients must be in a list, that can include NDBNO
    # limit is unlimited by default,

    if limit == None:
        limit = ""
    else:
        limit = 'LIMIT {}'.format(limit)

    if date == None:
        date == ""

    select_list = []
    select_string = ""
    for nutrient in nutrients:
        select_string += "f.'{}',".format(nutrient)

    password_q = ''' 
        SELECT {}, date
        FROM Logs as l
        JOIN Foods as f
            ON l.ndbno = f.ndbno
        WHERE UserId = "{}"
        ORDER BY l.Date DESC
        {}
        '''.format(select_string[:-1], user_id, limit)

    conn = connect_db()
    cur = conn.execute(password_q)
    tuples = conn.execute(password_q).fetchall()

    if printing:
        table.draw(cur)

    conn.close()
    return tuples
Exemple #9
0
def heapSort(arr, length):

    #调整parent结点为大根堆
    def heapAdjust(arr, parent, length):

        temp = arr[parent]
        child = 2 * parent + 1

        while child < length:
            if child + 1 < length and arr[child] < arr[child + 1]:
                child += 1

            if temp > arr[child]:
                break
            arr[parent] = arr[child]
            parent = child
            child = 2 * child + 1
        arr[parent] = temp

    if arr == []:
        return []
    sorted_list = arr
    #最后一个结点的下标为length//2-1
    #建立初始大根堆
    for i in range(0, length // 2)[::-1]:
        heapAdjust(sorted_list, i, length)

    for j in range(1, length)[::-1]:
        #把堆顶元素即第一大的元素与最后一个元素互换位置
        temp = sorted_list[j]
        sorted_list[j] = sorted_list[0]
        sorted_list[0] = temp
        #换完位置之后将剩余的元素重新调整成大根堆
        heapAdjust(sorted_list, 0, j)

        draw(sorted_list, "Heap-Sort")
    return sorted_list