Example #1
0
def buildGraphFrom1():
    desc = readList()
    N = desc[0]
    M = desc[1]
    # 新建图
    G = GNode(N)
    G.Ne = M
    while M:
        eList = readList()
        G.insertEdge(Edge(eList[0] - 1, eList[1] - 1, eList[2]))
        M -= 1
    return G
Example #2
0
def buildGraphFrom0():
    desc = readList()
    N = desc[0]
    M = desc[1]
    # 新建图
    G = GNode(N)
    G.Ne = M
    while M:
        eList = readList()
        # 有向图
        G.insertEdgeDirect(Edge(eList[0], eList[1], eList[2]))
        M -= 1
    return G
def readData():
    desc = readList()
    N = desc[0]
    K = desc[1]
    M = desc[2]

    # 初始化
    users = [['-', '-', '-', '-'] for i in range(N)]

    perfectScore = readList()

    # 读入数据
    while M:
        submission = readList()
        submission[0] -= 1
        submission[1] -= 1
        if users[submission[0]][submission[1]] == '-' or users[submission[0]][
                submission[1]] < submission[2]:
            users[submission[0]][submission[1]] = submission[2]
        M -= 1

    return users, N, K, perfectScore
Example #4
0
            if G[pos][i] > 0:  # 邻接点
                degrees[i] -= 1
                if degrees[i] == 0:
                    heap.insert(-list[i])

    return result


'''
11
33 1 13 12 34 38 27 22 32 -1 21

------------
1 13 12 21 33 34 38 27 22 32

'''


def printList(list):
    for i in list:
        print - i,


from utils.readList import readList
if __name__ == '__main__':
    N = input()
    list = readList()
    G = buildGraph(list, N)
    listSorted = topSort(G, list, N)
    printList(listSorted)
    percDown(list, 0, i)


def insertionNext(list, N):
    # 找到合适的位置
    for i in range(N - 1):
        if list[i] > list[i + 1]:
            break
    # 进行一次插入
    if i < N - 1:  # 尚未排序完成
        temp = list[i + 1]
        j = i + 1
        # python中没有那种两个分号的while循环,用while来代替
        while j >= 1 and temp < list[j - 1]:
            list[j] = list[j - 1]
            j -= 1  # 有交换时才进行移位
        list[j] = temp


if __name__ == '__main__':
    N = input()
    list_raw = readList()
    list = readList()
    if InsertionOrHeap(list_raw, list, N):
        print 'Insertion Sort'
        insertionNext(list, N)
    else:
        print 'Heap Sort'
        heapNext(list, N)

    print list