Exemplo n.º 1
0
# coding=utf-8
from com.igitras.algorithm.libs import swap_element, random_list, validate_list_sorted

__author__ = 'mason'


# listToSort待排序数组, sort排序方式,True代表升序
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()


listToSort = random_list(20, 0, 50)
bubble(listToSort, False)
validate_list_sorted(listToSort, False)
print listToSort
bubble(listToSort, True)
print listToSort
    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


arr_to_sort = random_list(100000, 0, 10000)
print arr_to_sort
print time.time()
binary_search_sort(arr_to_sort)
print time.time()
validate_list_sorted(arr_to_sort, True)
print arr_to_sort
Exemplo n.º 3
0
    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


test_count = 100
random = Random()
while test_count > 0:
    test_count -= 1
    arr_to_sort = random_list(random.randint(1, 100), 1, 100)
    shell_sort(arr_to_sort)
    print arr_to_sort
    validate_list_sorted(arr_to_sort)

# arr_to_sort = random_list(100000, 0, 10000)
# print arr_to_sort
#
# print time.time()
# shell_sort(arr_to_sort)
# print time.time()
# validate_list_sorted(arr_to_sort, True)
# print arr_to_sort