Ejemplo n.º 1
0
def test_shell_quicksort():
    quicksort_items = []
    # random.seed(42)
    for i in range(0, 10000):
        quicksort_items.append(random.randint(1, 100))
    shellsort_items = quicksort_items.copy()

    start_quicksort = time.time()
    quicksort(quicksort_items, 0, len(quicksort_items) - 1)
    end_quicksort = time.time()

    start_shellsort = time.time()
    shell_sort(shellsort_items)
    end_shellsort = time.time()

    assert is_sorted(quicksort_items)
    assert is_sorted(shellsort_items)

    print(f'Quicksort time: {end_quicksort - start_quicksort}')
    print(f'Shellsort time: {end_shellsort - start_shellsort}')
Ejemplo n.º 2
0
def bucket_sort(array):
    largest = max(array)
    length = len(array)
    size = largest / length

    buckets = [[] for _ in range(length)]

    for i in range(length):
        j = int((array[i] / size))
        if j != length:
            buckets[j].append(array[i])
        else:
            buckets[length - 1].append(array[i])

    for i in range(length):
        shell_sort.shell_sort(buckets[i])

    result = []
    for i in range(length):
        result = result + buckets[i]

    return result
        right = m2
        result(left, a[0])
        result(a[1], right)

    return result(0, len(M) - 1)


ls = [random.randint(0, 30000) for i in range(30000)]  #lst = copy.copy(ls)
lst = copy.copy(ls)

start_time = time.time()
quick_sort(ls)
print("Q_sort => %s seconds" % (time.time() - start_time))

start_time = time.time()
shell_sort.shell_sort(lst)
print("shell_sort => %s seconds" % (time.time() - start_time))


class TestSplitArray(unittest.TestCase):
    def test_split_array(self):

        ls = [3, 0, 1, 4]
        quick_sort(ls)
        self.assertEqual(ls, [0, 1, 3, 4])

        ls = [4, 3, 0, 1]
        quick_sort(ls)
        self.assertEqual(ls, [0, 1, 3, 4])

        ls = [30, 20, 10, 4]
Ejemplo n.º 4
0
def shuffle(iterable):
    if not iterable:
        return []
    mapping = {k: v for k, v in zip(rand_range(), iterable)}
    return [mapping[k] for k in shell_sort(mapping.keys())]
Ejemplo n.º 5
0
 def testShellSortInts(self):
   list_ints = [4, 2, 1, 9, 3, 7, 5, 6]
   self.assertEqual(shell_sort.shell_sort(list_ints),
       [1, 2, 3, 4, 5, 6, 7, 9])
Ejemplo n.º 6
0
 def testShellSortStrings(self):
   list_strings = ['this', 'is', 'my', 'test', 'list']
   self.assertEqual(shell_sort.shell_sort(list_strings),
       ['is', 'list', 'my', 'test', 'this'])
Ejemplo n.º 7
0
 def test_shell_sort_sorted(self):
     self.assertTrue(is_sorted(shell_sort(self.sorted)))
Ejemplo n.º 8
0
 def test_shell_sort(self):
     self.assertTrue(is_sorted(shell_sort(create_unsorted(10000))))
Ejemplo n.º 9
0
def perf_shell_sort():
    r = {}
    for gap_fn in [original_gap]:
        r[gap_fn.__name__] = bench_sort(lambda l: shell_sort(l, gap_fn),
                                        [100, 10000, 1000000])
    return r
Ejemplo n.º 10
0
        # shell = shell_sort.shell_sort(l)
        assert( i_s == s_s )
        #assert( i_s == shell )
    num = 2000

    setup = '''import random
import shell_sort
import sort'''

    selection = f'''
l = []
for i in range({num}):
    l.append(random.randint(-100000, 100000))
sort.selection_sort(l)'''

    insertion = f'''
l = []
for i in range({num}):
    l.append(random.randint(-100000, 100000))
sort.insertion_sort(l)'''

    print("sel_s", timeit.repeat(stmt=selection, setup=setup, number=1, repeat=3))
    print("ins_s", timeit.repeat(stmt=insertion, setup=setup, number=1, repeat=3))

    shell = f'''
l = []
for i in range({num}):
    l.append(random.randint(-100000, 100000))
shell_sort.shell_sort(l)'''
    print("shell", timeit.repeat(stmt=shell, setup=setup, number=1, repeat=3))
Ejemplo n.º 11
0
        f.write(str(one) + "\t")

print("待排序数据 共1000个数字", testlist)
print("\n")
start_time = time.time()
bubble = BubbleSortMethod(testlist)  # new object of BubbleSortMethod class
sortlist = bubble.bubble_sort1()
print("冒泡排序:", sortlist)
print("冒泡排序用时:", time.time() - start_time)
print("\n")
start_time = time.time()
insert = InsertSortMethod(testlist)  # new object of InsertSortMethod class
sortlist = insert.insert_sort1()
print("直接插入排序:", sortlist)
print("直接插入排序用时:", time.time() - start_time)
print("\n")
start_time = time.time()
sortlist = select_sort(testlist)
print("直接选择排序:", sortlist)
print("直接选择排序用时:", time.time() - start_time)
print("\n")
start_time = time.time()
sortlist = shell_sort(testlist)
print("希尔排序:", sortlist)
print("希尔排序用时:", time.time() - start_time)
print("\n")
start_time = time.time()
sortlist = merge_sort(testlist, 0, len(testlist) - 1)
print("归并排序:", sortlist)
print("归并排序用时:", time.time() - start_time)
Ejemplo n.º 12
0
    replace(i, 'red', num, time)


dimensions.anim = anim

a = drawn_sticks

options = lambda time: (anim, a, time)

choice = int(
    input("""
1.radix_sort
2.cocktail_sort
3.gnome_sort
4.quick_sort
5.comb_sort
6.shell_sort
7.merge_sort
8.bubble_sort
Enter your choice : """))

if choice == 1: radix_sort(*options(0.0001))
elif choice == 2: cocktail_sort(*options(0))
elif choice == 3: gnome_sort(*options(0))
elif choice == 4: quick_sort(*options(0))
elif choice == 5: comb_sort(*options(0))
elif choice == 6: shell_sort(*options(0))
elif choice == 7: merge_sort(*options(0))
elif choice == 8: bubble_sort(*options(0))
window.root.mainloop()
Ejemplo n.º 13
0
from quicksort import quicksort
from shell_sort import shell_sort
import random
import time


def create_sort_data():
    elements_length = 30000
    data = []
    for _ in range(elements_length):
        data.append(random.randint(0, elements_length))

    return data


sort_data = create_sort_data()

start_time = time.time()
quicksort(sort_data)

quicksort_time = time.time() - start_time

start_time = time.time()
shell_sort(sort_data)

shell_sort_time = time.time() - start_time

print('quicksort time', quicksort_time)
print('shell sort time', shell_sort_time)
Ejemplo n.º 14
0
from random import randint

from bubble_sort import bubble_sort
from insertion_sort import insertion_sort
from merge_sort import run_merge_sort
from quick_sort import run_quick_sort
from selection_sort import selection_sort
from shell_sort import shell_sort

data = [randint(1, 5000) for _ in range(20000)]

def random_values():
    return data.copy()


# Pay attention on the difference of it/s at the start of the sort and at the end
bubble_sort(random_values())

# Pay attention on the difference of it/s at the start of the sort and at the end
selection_sort(random_values())

insertion_sort(random_values())

shell_sort(random_values())

run_quick_sort(random_values())

run_merge_sort(random_values())
Ejemplo n.º 15
0
 def test_shell(self):
     for i in range(len(testing_lists)):
         self.assertListEqual(shell_sort(testing_lists[i][0]),
                              testing_lists[i][1])
Ejemplo n.º 16
0
import bubble_sort
import insertion_sort
import selection_sort
import shell_sort
import timeit

# A = [10,20,30,40,90,80,70,60,50]    # 어느정도 정렬이 되어있는 배열
A = [90, 80, 70, 60, 50, 40, 30, 20, 10]  # 정렬이 전혀 되어있지않은 배열
n = len(A)
start_time = timeit.default_timer()
print("정렬된 배열 :", bubble_sort.bubble_sort(A, n))
print("버블정렬의 시간은 : ", timeit.default_timer() - start_time)
start_time = timeit.default_timer()
print("정렬된 배열 :", selection_sort.selection_sort(A, n))
print("선택정렬의 시간은 : ", timeit.default_timer() - start_time)
start_time = timeit.default_timer()
print("정렬된 배열 :", insertion_sort.insertion_sort(A, n))
print("삽입정렬의 시간은 : ", timeit.default_timer() - start_time)
start_time = timeit.default_timer()
print("정렬된 배열 :", shell_sort.shell_sort(A, n))
print("쉘 정렬의  시간은 : ", timeit.default_timer() - start_time)