コード例 #1
0
ファイル: insertion.py プロジェクト: masonmei/studing-python
def direct_insertion(list_to_sort):
    length = len(list_to_sort)
    index = 1
    while index < length:
        inner_index = index
        while inner_index >= 1:
            if list_to_sort[inner_index] < list_to_sort[inner_index - 1]:
                swap_element(list_to_sort, inner_index, inner_index - 1)
            else:
                break
            inner_index -= 1
        index += 1
コード例 #2
0
ファイル: bubble.py プロジェクト: masonmei/studing-python
def bubble(list_to_sort, sort):
    length = len(list_to_sort)

    index = length - 1
    while index > 0:
        for i in range(index):
            if list_to_sort[index] < list_to_sort[i]:
                swap_element(list_to_sort, i, index)
        index -= 1

    if not sort:
        list_to_sort.reverse()
コード例 #3
0
ファイル: shell_sort.py プロジェクト: masonmei/studing-python
def shell_sort(list_to_sort):
    length = len(list_to_sort)

    dist = length / 2

    while dist > 0:
        for i in range(dist, length):
            j = i
            while j >= dist and list_to_sort[j] <= list_to_sort[j - dist]:
                swap_element(list_to_sort, j, j - dist)
                j -= dist

        dist /= 2
コード例 #4
0
ファイル: quick.py プロジェクト: masonmei/studing-python
def get_partition(list_to_sort, start, end):
    if start >= end:
        raise Exception("start must smaller than end")
    else:
        pivot_index = start
        left = start + 1
        right = end

        if left == right:
            if list_to_sort[pivot_index] > list_to_sort[left]:
                swap_element(list_to_sort, pivot_index, left)
                pivot_index = left
        else:
            while left < right:
                # 从右往左找到第一个小于pivot的数
                while left < right:
                    if list_to_sort[right] < list_to_sort[pivot_index]:
                        break
                    else:
                        right -= 1
                while left < right:
                    if list_to_sort[left] > list_to_sort[pivot_index]:
                        break
                    else:
                        left += 1

                if left != right:
                    swap_element(list_to_sort, left, right)
            if list_to_sort[left] >= list_to_sort[pivot_index]:
                swap_element(list_to_sort, left - 1, pivot_index)
                pivot_index = left - 1
            else:
                swap_element(list_to_sort, left, pivot_index)
                pivot_index = left
        return pivot_index
コード例 #5
0
def binary_search_sort(list_to_sort):
    length = len(list_to_sort)
    index = 1

    while index < length:

        left = 0
        right = index - 1

        while right >= left:
            mid = (left + right) / 2

            if list_to_sort[mid] <= list_to_sort[index]:
                left = mid + 1
            else:
                right = mid - 1

        # print left, right
        mid = left
        while mid < index:
            swap_element(list_to_sort, mid, index)
            mid += 1

        index += 1