Exemple #1
0
def selectionSort(array):
    for i in range(len(array)):
        min = i
        for j in range(i + 1, len(array)):
            if array[j] < array[min]:
                min = j
        swap(array, i, min)
Exemple #2
0
def mergeDown(array, start, end, falseBottom=None):
    if end - start <= 1:
        return
    if falseBottom and falseBottom >= end:
        return
    gap = int(2**(math.ceil(math.log(end - start, 2)) - 1))
    if falseBottom and end - (gap * 2) > falseBottom:
        falseBottom = None
    missingNumbers = (gap * 2) - (end - start)
    end = min(len(array), end)
    middle = start + gap
    if missingNumbers > 0 and falseBottom == None:
        holding = [
            array[index] for index in range(start, start + missingNumbers)
        ]
        for i in range(gap - missingNumbers):
            left = middle - gap + missingNumbers + i
            right = middle + i
            minVal = min(array[left], array[right])
            maxVal = max(array[left], array[right])
            array[start + i] = maxVal
            array[middle + i] = minVal
        middle = start + gap - missingNumbers
        for i in range(missingNumbers):
            array[middle + i] = holding[i]
        mergeDown(array, middle - gap, middle, falseBottom=start)
        mergeDown(array, middle, end)
    else:
        for left in range(start, middle):
            if not falseBottom or left >= falseBottom:
                right = left + gap
                if array[left] < array[right]:
                    swap(array, left, right)
        mergeDown(array, start, middle, falseBottom=falseBottom)
        mergeDown(array, middle, end, falseBottom=falseBottom)
Exemple #3
0
def insertionSort(array, start=0, end=None, gap=1):
    if not end:
        end = len(array)
    for i in range(start + gap, end):
        j = i
        while j >= start + gap and array[j] < array[j - gap]:
            swap(array, j, j - gap)
            j -= gap
Exemple #4
0
def bubbleSort(array):
    for i in range(1, len(array)):
        sorted = True
        for j in range(len(array) - i):
            if array[j] > array[j + 1]:
                sorted = False
                swap(array, j, j + 1)
        if sorted:
            return
Exemple #5
0
def gnomeSort(array):
    index = 1
    while index < len(array):
        if index == 0:
            index = 1
        if array[index - 1] > array[index]:
            swap(array, index - 1, index)
            index -= 1
        else:
            index += 1
Exemple #6
0
def combSort(array):
    gap = math.ceil((len(array) - 1) / 2)
    changed = False
    while gap > 1 or changed:
        changed = False
        for i in range(len(array) - gap):
            if array[i] > array[i + gap]:
                changed = True
                swap(array, i, i + gap)
        gap = max(1, min(math.ceil(gap / 2), gap - 1))
Exemple #7
0
def heapDown(array, position, length):
    child = (position + 1) * 2 - 1
    if child >= length:
        return
    if child + 1 < length:
        if (array[child] < array[child + 1]):
            child += 1
    if array[position] < array[child]:
        swap(array, position, child)
        heapDown(array, child, length)
        return
Exemple #8
0
def stoogeSort(array, start=0, end=None):
  if end == None:
    end = len(array)
  if array[start] > array[end - 1]:
    swap(array, start, end - 1)
  if start + 2 >= end:
    return
  increment = ceil((2/3) * (end - start))
  stoogeSort(array, start, start + increment)
  stoogeSort(array, end - increment, end)
  stoogeSort(array, start, start + increment)
  
Exemple #9
0
def cocktailSort(array):
    start = 0
    end = len(array) - 1
    changed = True
    while changed:
        changed = False
        for i in range(start, end):
            if array[i] > array[i + 1]:
                swap(array, i, i + 1)
                changed = True
        end -= 1
        for i in range(end, start - 1, -1):
            if array[i] > array[i + 1]:
                swap(array, i, i + 1)
                changed = True
        start += 1
Exemple #10
0
def mergeUp(array, start, end):
    if end - start <= 1:
        return
    gap = int(2**(math.ceil(math.log(end - start, 2)) - 1))
    middle = start + gap
    missingNumbers = (gap * 2) - (end - start)
    if missingNumbers > 0:
        if array[start] > array[middle - 1]:
            for left in range(start, start + gap - missingNumbers):
                right = left + gap
                if array[left] > array[right]:
                    swap(array, left, right)
        else:
            for left in range(start + missingNumbers, middle):
                right = left + gap - missingNumbers
                if array[left] > array[right]:
                    swap(array, left, right)
        mergeUp(array, start, middle)
        mergeUp(array, middle, end)
    else:
        for left in range(start, middle):
            right = left + gap
            if right < end:
                if array[left] > array[right]:
                    swap(array, left, right)
        mergeUp(array, start, middle)
        mergeUp(array, middle, end)
Exemple #11
0
def quickSort(array, start=0, end=None):
    if not end:
        end = len(array)
    if end - start <= 1:
        return

    swap(array, start, random.randint(start, end - 1))
    leftIndex = start + 1
    rightIndex = end - 1
    while leftIndex < rightIndex:
        while leftIndex < rightIndex and array[rightIndex] > array[start]:
            rightIndex -= 1
        while leftIndex < rightIndex and array[leftIndex] < array[start]:
            leftIndex += 1
        if leftIndex != rightIndex:
            swap(array, leftIndex, rightIndex)
    if array[start] > array[rightIndex]:
        swap(array, start, rightIndex)
    else:
        start += 1
    quickSort(array, start=start, end=rightIndex)
    quickSort(array, start=rightIndex, end=end)
Exemple #12
0
def heapSort(array):
    for i in range(1, len(array)):
        heapUp(array, i)
    for i in range(len(array) - 1, 0, -1):
        swap(array, 0, i)
        heapDown(array, 0, i)
Exemple #13
0
def heapUp(array, position):
    parent = int((position - 1) / 2)
    if array[position] > array[parent]:
        swap(array, position, parent)
        heapUp(array, parent)