Beispiel #1
0
def shell_sort(item, begin, end, visualise=None, condition=None, gap='std'):
    size = end-begin+1
    esc=None
    if condition is None:
        condition=lambda a, b: a<b
    if visualise:
        warnings.warn('Visualise, required extra Modules such as numpy and matplotlib and OpenCV')
        
    if gap is 'std':
        next_gap = lambda curr_gap: curr_gap>>1
        esc=next_gap(size)
    elif gap is 'hibbard':
        import math as mt
        exp = mt.ceil(mt.log2(size))
        temp=2**exp-1
        next_gap = lambda curr_gap: int((curr_gap / 2) - .5)
        esc=next_gap(temp)
    while esc > 0:
        for i in range( begin+esc, end+1 ):
            temp = item[i]
            j=i
            while j>=esc and not condition(item[j-esc], temp):
                item[j]=item[j-esc]                           
                j-=esc
            item[j]=temp
            if visualise:
                vis(item, 'Shell Sort' , j)
        esc = next_gap(esc)
    if visualise:
        vis(item, 'Shell Sort')
Beispiel #2
0
def quick_sort(item, begin, end, visualise=None, condition=None):
    if visualise:
        warnings.warn(
            'Visualise, required extra Modules such as numpy and matplotlib and OpenCV'
        )

    def partition(begin, end):
        pivot = end
        i = begin
        j = begin
        while i <= end - 1:
            if not condition(item[pivot], item[i]):
                item[i], item[j] = item[j], item[i]
                j += 1
            i += 1
        item[j], item[pivot] = item[pivot], item[j]
        if visualise:
            vis(item, 'Quick Sort', pivot)
        return j

    def sort(begin, end):
        if begin < end:
            p = partition(begin, end)
            sort(begin, p - 1)
            sort(p + 1, end)

    if condition is None:
        condition = lambda first, second: first < second

    sort(begin, end)

    if visualise:
        vis(item, 'Quick Sort')
Beispiel #3
0
 def merge(l, m, r):
     n1 = m-l+1
     n2 = r-m
     left, right = [], []
     for i in range(l, m+1):
         left.append(item[i])
     for i in range(m+1, r+1):
         right.append(item[i])
     i, j = 0, 0
     k=l
     while i<n1 and j<n2:
         if condition(left[i], right[j]):
             item[k] = left[i]
             i+=1
         else:
             item[k] = right[j]
             j+=1
         k+=1
     while i<n1:
         item[k] = left[i]
         i, k = i+1, k+1
     while j<n2:
         item[k]=right[j]
         j, k =j+1, k+1
     if visualise:
         vis(item, 'Merge Sort', m)
Beispiel #4
0
 def partition(begin, end):
     pivot = end
     i = begin
     j = begin
     while i <= end - 1:
         if not condition(item[pivot], item[i]):
             item[i], item[j] = item[j], item[i]
             j += 1
         i += 1
     item[j], item[pivot] = item[pivot], item[j]
     if visualise:
         vis(item, 'Quick Sort', pivot)
     return j
Beispiel #5
0
def bubble_sort(item, begin, end, visualise=None, condition=None):
    if condition is None:
        condition = lambda a, b: a<=b
    if visualise:
        warnings.warn('Visualise, required extra Modules such as numpy and matplotlib and OpenCV')
        if not isinstance(item[begin], (int, float, str)):
            raise ValueError("visualise is valid only for 'int', 'float', 'str' not for {}".format(type(item[begin])))
        
    for i in range(end-begin+1):
        for j in range(begin, end-i):
            if not condition(item[j], item[j+1]):
                item[j], item[j+1] = item[j+1], item[j]

        if visualise:
            vis(item, 'Bubble Sort', j)
    # final-sorted
    if visualise:
            vis(item, 'Bubble Sort')
Beispiel #6
0
def merge_sort(item, begin, end, visualise=None, condition=None):
    def merge(l, m, r):
        n1 = m-l+1
        n2 = r-m
        left, right = [], []
        for i in range(l, m+1):
            left.append(item[i])
        for i in range(m+1, r+1):
            right.append(item[i])
        i, j = 0, 0
        k=l
        while i<n1 and j<n2:
            if condition(left[i], right[j]):
                item[k] = left[i]
                i+=1
            else:
                item[k] = right[j]
                j+=1
            k+=1
        while i<n1:
            item[k] = left[i]
            i, k = i+1, k+1
        while j<n2:
            item[k]=right[j]
            j, k =j+1, k+1
        if visualise:
            vis(item, 'Merge Sort', m)
            
    def sort(l, r):
        if l<r:
            m = (l+r)//2
            sort(l, m)
            sort(m+1, r)
            merge(l, m, r)
            
    if condition is None:
        condition=lambda first, second: first <= second # equality must be checked other wise un-stablity happens
    if visualise:
        warnings.warn('Visualise, required extra Modules such as numpy and matplotlib and OpenCV')
        if not isinstance(item[begin], (int, float, str)):
            raise ValueError("visualise is valid only for 'int', 'float', 'str' not for {}".format(type(item[begin])))
    sort(begin, end)
    if visualise:
            vis(item, 'Merge Sort')
Beispiel #7
0
def quick_sort(item, begin, end, visualise=None, condition=None):

    if condition is None:
        condition = lambda first, second: first <= second  # equality must be checked other wise un-stablity happens
    if visualise:
        warnings.warn(
            'Visualise, required extra Modules such as numpy and matplotlib and OpenCV'
        )
        if not isinstance(item[begin], (int, float, str)):
            raise ValueError(
                "visualise is valid only for 'int', 'float', 'str' not for {}".
                format(type(item[begin])))

    def partition(begin, end):
        pivot = end
        i = begin
        j = begin
        while i <= end - 1:
            if not condition(item[pivot], item[i]):
                item[i], item[j] = item[j], item[i]
                j += 1
            i += 1
        item[j], item[pivot] = item[pivot], item[j]
        if visualise:
            vis(item, 'Quick Sort', pivot)
        return j

    s = Stack()
    s.push((begin, end))

    while not s.empty():  # <= log(end-begin+1)
        l, h = s.pop()
        p = partition(l, h)
        if p + 1 < h:
            s.push((p + 1, h))
        if l < p - 1:
            s.push((l, p - 1))
    if visualise:
        vis(item, 'Quick Sort')
Beispiel #8
0
def insertion_sort(item, begin, end, visualise=None, condition=None):
    if condition is None:
        condition=lambda first, second: first <= second
    if visualise:
        warnings.warn('Visualise, required extra Modules such as numpy and matplotlib and OpenCV')
        if not isinstance(item[begin], (int, float, str)):
            raise ValueError("visualise is valid only for 'int', 'float', 'str' not for {}".format(type(item[begin])))
        
    i=j=begin
    while i <= end:
        j=i
        k=item[i]
        while j > begin and not condition( item[j-1], k ):
            item[j] = item[j-1]
            j -= 1
        item[j] = k
        if visualise:
            vis(item, 'Insertion Sort', j)
        i+=1
        
    # final-sorted
    if visualise:
            vis(item, 'Insertion Sort')
Beispiel #9
0
def selection_sort(item, begin, end, visualise=None, condition=None):
    if condition is None:
        condition = lambda a, b: a < b
    if visualise:
        warnings.warn(
            'Visualise, required extra Modules such as numpy and matplotlib and OpenCV'
        )
        if not isinstance(item[begin], (int, float, str)):
            raise ValueError(
                "visualise is valid only for 'int', 'float', 'str' not for {}".
                format(type(item[begin])))

    for i in range(begin, end):
        min_idx = i
        for j in range(i + 1, end + 1):
            if not condition(item[min_idx], item[j]):
                min_idx = j
        item[i], item[min_idx] = item[min_idx], item[i]
        if visualise:
            vis(item, 'Selection Sort', min_idx)
    # final-sorted
    if visualise:
        vis(item, 'Selection Sort')