Ejemplo n.º 1
0
def ksa_sst(key: str, n: int, t: int):
    num_key = str2num(key)
    key_length = len(num_key)
    s = list(range(n))

    marked_lst = [False for _ in range(n)]
    marked_lst[-1] = True
    marked_num = 1
    j = n
    i = 0
    while marked_num < n:
        i = i % n
        j = (j + s[i % n] + num_key[i % key_length]) % n
        swap(s, i, j)
        if marked_num < n / 2:
            if not marked_lst[j] and not marked_lst[i]:
                marked_lst[j] = True
                marked_num += 1
        else:
            if (not marked_lst[j] and marked_lst[i]) or (not marked_lst[j]
                                                         and i == j):
                marked_lst[j] = True
                marked_num += 1
        swap(marked_lst, i, j)
        i += 1
    return s
Ejemplo n.º 2
0
def bubbleSort(a):
    n = len(a)
    if n < 2:
        return a
    for i in range(n - 1):
        for j in range(i + 1, n):
            if a[i] > a[j]:
                swap(a, i, j)
def ascending(array):
    sorted = True
    for i in range(len(array) - 1):
        for j in range(len(array) - i - 1):
            if (array[j] > array[j + 1]):
                swap(array, j, j + 1)
                sorted = False
        if (sorted):
            break
    return array
Ejemplo n.º 4
0
def selectionSort(a):
    n = len(a)
    if n < 2:
        return a
    for i in range(n - 1):
        mi = i
        for j in range(i + 1, n):
            if a[j] < a[mi]:
                mi = j
        swap(a, i, mi)
Ejemplo n.º 5
0
def insertionSort1(a):
    # 低效、错误的代码
    n = len(a)
    if n < 2:
        return a
    for i in range(1, n):
        for j in reversed(range(i)):
            if a[j] <= a[i]:
                break
            swap(a, i, j)
            i = j  # 记得更新 i,为的是把 i 插入正确的位置
def heapify(A, idx, max):
    l = 2 * idx + 1
    r = 2 * idx + 2
    if l < max and A[l] > A[idx]:
        largest = l
    else:
        largest = idx
    if r < max and A[r] > A[largest]:
        largest = r
    if largest != idx:
        swap(A, idx, largest)
        heapify(A, largest, max)
def heapify(A,idx,max):
	l = 2*idx+1
	r = 2*idx+2
	if l < max and A[l] > A[idx] :
		largest = l
	else:
		largest = idx
	if r < max and A[r] > A[largest] :
		largest = r
	if largest != idx :
		swap( A, idx, largest )
		heapify( A, largest, max)
Ejemplo n.º 8
0
 def partition(lb, ub):
     # initialize the pivot, and lower/upper bound
     pivot, start, end = A[lb], lb, ub
     while (start < end):
         while ((A[start] <= pivot) and (start < ub)):
             start += 1
         while ((A[end] > pivot) and (end > lb)):
             end -= 1
         if (start < end):
             A[start], A[end] = c.swap(A[start], A[end])
         if (verbose == 2): print("  sub:", pivot, start, end, " :: ", A)
     A[lb], A[end] = c.swap(A[lb], A[end])
     return (end)
Ejemplo n.º 9
0
def heap_sort(array):
    queue = deque()
    array_copy = list(array)
    array_copy = build_max_heap(array_copy)
    for i in range(len(array_copy)):
        array_copy = swap(array_copy, 0, len(array_copy) - 1)
        queue.appendleft(array_copy[len(array_copy) - 1])
        array_copy.pop()
        array_copy = heapify(array_copy, 0)
    return queue
Ejemplo n.º 10
0
def partition(a, left, right):
    # pivot = a[left]
    # 数组 a 从索引 left+1 到 right 中
    # 把比 pivot 大的都放右边,比 pivot 小的都放左边
    # 最后返回 pivot 的索引

    # 可以记录比 pivot 小的数的最大索引,然后和 pivot 交换
    # 举例:[3, 2, 5, 1, 6]--> [3, 2, 1, 5, 6] --> [1, 2, 3, 5, 6]
    # 首先分成 2 1 和 5 6,比 pivot 小的最大位置是 1 ,和 3 交换即可,最后返回 3 的索引
    # p2 用来遍历,p1-1 标记比 pivot 小的数的最大位置
    pivot = a[left]
    p1 = p2 = left + 1
    while p2 <= right:
        if a[p2] < pivot:
            swap(a, p1, p2)
            p1 += 1
        p2 += 1
    swap(a, p1 - 1, left)
    return p1 - 1
Ejemplo n.º 11
0
def heapify(array, i):

    if (0 <= 2 * i + 1 < len(array) and 0 <= 2 * i + 2 < len(array)):
        if (array[i] <= array[2 * i + 1] and array[i] <= array[2 * i + 2]):
            if (array[2 * i + 1] > array[2 * i + 2]):
                heapify(swap(array, i, 2 * i + 1), 2 * i + 1)
            else:
                heapify(swap(array, i, 2 * i + 2), 2 * i + 2)

        elif (array[i] <= array[2 * i + 1]):
            heapify(swap(array, i, 2 * i + 1), 2 * i + 1)
        elif (array[i] <= array[2 * i + 2]):
            heapify(swap(array, i, 2 * i + 2), 2 * i + 2)

    elif (0 <= 2 * i + 1 < len(array)):
        if (array[i] <= array[2 * i + 1]):
            heapify(swap(array, i, 2 * i + 1), 2 * i + 1)

    return array
Ejemplo n.º 12
0
    def insertion_sort(gap, i=0):
        j, n = (gap + i), len(A)
        # index should be within range of the list
        if ((i < 0) or (j >= len(A))): return ()

        if(verbose == 2): print("  sub:", i, "-", j, " :: ", A)

        if (A[i] > A[j]):  # push larger numbers to the right
            A[i], A[j] = c.swap(A[i], A[j])
            insertion_sort(gap, i - gap)  # check previous set
        # note: when the above condition is true, i is pushed back to i - gap.
        # the cycle picks up again from this point, thus doing a few extra iterations.
        insertion_sort(gap, i + 1)  # increment i and j, recurse
Ejemplo n.º 13
0
 def heapify(i, n):
     parent = i  # pointer of the parent node, that needs to be heap sorted
     ch_1 = i * 2 + 1  # child #1
     ch_2 = ch_1 + 1  # child #2
     # check if index of child is within active range, and if it has lower/higher value
     while ((ch_1 < n) and (A[ch_1] > A[parent])):
         parent = ch_1
     while ((ch_2 < n) and (A[ch_2] > A[parent])):
         parent = ch_2
     if (verbose == 2): print("  sub:", i, n, " :: ", A)
     if (i != parent):
         A[i], A[parent] = c.swap(A[i], A[parent])
         heapify(parent, n - 1)
Ejemplo n.º 14
0
def bubble_sort(A, verbose=0, desc=0):
    import common as c
    for i in range(len(A)):
        swapped = 0
        for j in range(len(A) - 1 -
                       i):  # -i :: keep ignoring items already sorted
            if (A[j] > A[j + 1]):  # only worry about ascending order
                (A[j], A[j + 1]) = c.swap(A[j], A[j + 1])
                swapped = 1
            if (verbose == 2): print("  sub:", j, " :: ", A)
        if (verbose): print("iter #", i, " :: ", A)
        if (not swapped): break  # early exit if already sorted.
    if (desc): A = A[::-1]  # if descending read list from right to left
    return (A)
Ejemplo n.º 15
0
 def h_sort():
     n = len(A)
     # building the heap  -- parent nodes = child nodes -1 (since each parent has 2 childs)
     # for n=7, there are 3 parent nodes (0, 1, 2) and 4 child nodes.
     for i in range((n // 2) - 1, -1,
                    -1):  # just the parent nodes; 0 is a parent node
         heapify(i, n)
         if (verbose): print("iter #", i, " build  :: ", A)
     # deleting the root node. save the root node value at the end
     # build sorted list by decrementing the size of the list ...
     # sorted values beyond the end of the (active) list
     for i in range(n - 1, 0, -1):
         A[0], A[i] = c.swap(A[0], A[i])
         heapify(0, i)
         if (verbose): print("iter #", i, " delete :: ", A)
def partition(A, l, r):
    p = selectPivotIndex(A, l, r)
    swap(A, p, r)
    store = l
    for i in range(l, r):
        if cmp(A[i], A[r]) <= 0:
            swap(A, i, store)
            store += 1
    swap(A, store, r)
    return store
def partition(A,l,r):
	p = selectPivotIndex(A,l,r)
	swap(A,p,r)
	store = l
	for i in range(l,r):
		if cmp(A[i], A[r]) <= 0:
			swap(A,i,store)
			store += 1
	swap(A,store,r)
	return store
def partition(A, l, r, pidx):

    idx, store, pivot = 0, l, A[pidx]

    # move pivot to end of the array
    swap(A, r, pidx)

    # all values <= pivot moved to front of array, and pivot inserted just after
    for i in range(l, r):
        if cmp(A[i], pivot) <= 0:
            swap(A, i, store)
            store += 1
    swap(A, r, store)
    return store
def partition( A, l, r, pidx):

	idx, store, pivot = 0, l, A[pidx]

	# move pivot to end of the array
	swap(A,r,pidx)

	# all values <= pivot moved to front of array, and pivot inserted just after
	for i in range(l,r):
		if cmp(A[i],pivot) <= 0:
			swap(A, i, store)
			store += 1
	swap(A,r,store)
	return store
Ejemplo n.º 20
0
def ascending(array):
    for i in range(len(array) - 1):
        for j in range(len(array) - i - 1):
            if (array[j] > array[j + 1]):
                swap(array, j, j + 1)
    return array
Ejemplo n.º 21
0
def descending(array):
    for i in range(len(array) - 1):
        for j in range(len(array) - 1 - i):
            if (array[j] < array[j + 1]):
                swap(array, j, j + 1)
    return array
def heapsort(A, l, r): # note l and r not used here
	buildHeap(A)
	for i in range(len(A)-1,0,-1): # range is [ n-1 ... 0 ]
		swap(A,0,i)
		heapify(A,0,i)
def heapsort(A, l, r):  # note l and r not used here
    buildHeap(A)
    for i in range(len(A) - 1, 0, -1):  # range is [ n-1 ... 0 ]
        swap(A, 0, i)
        heapify(A, 0, i)