Beispiel #1
0
def Merge(A, p, q, r):
    i = q - 1
    j = q
    t = 0
    temp = [range(len(A))]
    count = 0
    while i < q and j < r:
        if A[i] < A[j]:
            temp[t] = A[i]
            t += 1
            i += 1
        else:
            temp[t] = A[j]
            t += 1
            j += 1

    while i < q:
        temp[t] = A[i]
        t += 1
        i += 1

    while j < r:
        temp[t] = A[j]
        t += 1
        j += 1

    i = p - 1
    t = 0
    while i < r:
        A[i] = temp[t]
        t += 1
        i += 1

    count += 1
    printArr.printArr(A, count)
Beispiel #2
0
def CountingSort(A, n):
    oldA = A[0]
    if type(oldA) == type('a'):
        A = [ord(a) for a in A]
    Min = Max = A[0]

    for i in range(n):
        if A[i] < Min: Min = A[i]
        if A[i] > Max: Max = A[i]

    count = [0 for i in range(Max + 1)]

    for i in A:
        count[i] += 1

    # 출현 횟수 누적값 계산
    for i in range(Min + 1, Max + 1):
        count[i] = count[i] + count[i - 1]

    B = [0 for i in range(n)]
    for i in range(n - 1, -1, -1):
        B[count[A[i]] - 1] = A[i]
        count[A[i]] -= 1

    if type(B[0]) != type(oldA): B = [chr(b) for b in B]
    printArr.printArr(B)
Beispiel #3
0
def SelectionSort(A, n):
    count = 0
    for last in range(n - 1, 0, -1):
        Max = last  # 가장 마지막 원소를 최댓값으로 설정함.
        for k in range(last):
            if A[k] > A[Max]:
                Max = k  # 앞의 미정렬 원소 중에서 최댓값을 구함.
        A[Max], A[last] = A[last], A[Max]  # 최댓값을 뒤로 보내 정렬함.
        count += 1
        printArr.printArr(A, count)
Beispiel #4
0
def BubbleSort(A, n):
    count = 0
    flag = True
    for last in range(n - 1, 0, -1):
        for i in range(0, last):
            if A[i] > A[i + 1]:
                A[i], A[i + 1] = A[i + 1], A[i]
                flag = False
        if flag: break
        count += 1
        printArr.printArr(A, count)
Beispiel #5
0
def InsertSort(A, n):
    count = 0
    for i in range(1, n):
        loc = i - 1
        newItem = A[i]
        while loc >= 0 and newItem < A[loc]:
            A[loc + 1] = A[loc]
            loc -= 1
        if A[loc + 1] != newItem:
            A[loc + 1] = newItem
            count += 1
            printArr.printArr(A, count)