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
Ejemplo n.º 2
0
                    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


def quick_sort_with_order(list_to_sort, sort):
    quick_sort(list_to_sort, 0, len(list_to_sort) - 1)

    if not sort:
        list_to_sort.reverse()


test_count = 100
random = Random()
while test_count > 0:
    test_count -= 1
    listToSort = random_list(random.randint(1, 100), 1, 100)
    print listToSort
    quick_sort_with_order(listToSort, False)
    validate_list_sorted(listToSort, False)
    print listToSort
Ejemplo n.º 3
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
Ejemplo n.º 4
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