コード例 #1
0
ファイル: select.py プロジェクト: JohnsonChern/Algorithms
def select(source, start, end, k):
    if (end - start) <= 140:
        sort.normal_quick_sort(source, start, end)
        return [source[k - 1], k - 1]
    length = end - start

    for i in xrange(length / 5):
        sort.insert_sort(source, start + i * 5, start + i * 5 + 5)
        temp = source[start + i]
        source[start + i] = source[start + 5 * i + 2]
        source[start + 5 * i + 2] = temp

    [i, index] = select(source, start, start + length / 5, length / 10)
    temp = source[index]
    source[index] = source[end - 1]
    source[end - 1] = temp
    j = sort.partition(source, start, end)

    if j == k:
        return [source[j], j]
    elif j > k:
        return select(source, start, j, k)
    else:
        return select(source, j + 1, end, k)
コード例 #2
0
ファイル: myselect.py プロジェクト: JohnsonChern/Algorithms
def select(source, start, end, k):
    if (end - start) <= 140:
        sort.normal_quick_sort(source, start, end)
        return [source[k - 1], k-1]
    length = end - start

    for i in xrange(length/5):
        sort.insert_sort(source,start + i*5, start + i*5 + 5)
        temp = source[start + i]
        source[start + i] = source[start + 5*i + 2]
        source[start + 5*i + 2] = temp

    [i, index] = select(source, start, start + length/5, length/10)
    temp = source[index]
    source[index] = source[end - 1]
    source[end - 1] = temp
    j = sort.partition(source, start, end)
    
    if j == k:
        return [source[j], j]
    elif j > k:
        return select(source, start, j, k)
    else:
        return select(source, j + 1, end, k)
コード例 #3
0
ファイル: sort_test.py プロジェクト: caiqiqi/sort_test
# ц╟ещеепР
random.shuffle(ls)
time_start = time.clock()
sort.bubble_sort(ls)
print ("ц╟ещеепРсцй╠: % s"  %(time.clock() - time_start) )

# я║тЯеепР
random.shuffle(ls)
time_start = time.clock()
sort.select_sort(ls)
print ("я║тЯеепРсцй╠: % s"  %(time.clock() - time_start) )

# ╡ЕхКеепР
random.shuffle(ls)
time_start = time.clock()
sort.insert_sort(ls)
print ("╡ЕхКеепРсцй╠: % s"  %(time.clock() - time_start) )

# оё╤ШеепР
random.shuffle(ls)
time_start = time.clock()
sort.shell_sort(ls)
print ("оё╤ШеепРсцй╠: % s"  %(time.clock() - time_start) )

# ╧И╡╒еепР
random.shuffle(ls)
time_start = time.clock()
sort.merge_sort(ls)
print ("╧И╡╒еепРсцй╠: % s"  %(time.clock() - time_start) )

# ©ЛкыеепР
コード例 #4
0
# ц╟ещеепР
random.shuffle(ls)
time_start = time.clock()
sort.bubble_sort(ls)
print("ц╟ещеепРсцй╠: % s" % (time.clock() - time_start))

# я║тЯеепР
random.shuffle(ls)
time_start = time.clock()
sort.select_sort(ls)
print("я║тЯеепРсцй╠: % s" % (time.clock() - time_start))

# ╡ЕхКеепР
random.shuffle(ls)
time_start = time.clock()
sort.insert_sort(ls)
print("╡ЕхКеепРсцй╠: % s" % (time.clock() - time_start))

# оё╤ШеепР
random.shuffle(ls)
time_start = time.clock()
sort.shell_sort(ls)
print("оё╤ШеепРсцй╠: % s" % (time.clock() - time_start))

# ╧И╡╒еепР
random.shuffle(ls)
time_start = time.clock()
sort.merge_sort(ls)
print("╧И╡╒еепРсцй╠: % s" % (time.clock() - time_start))

# ©ЛкыеепР
コード例 #5
0
def test_insert_sort(array):
    t0 = time.time()
    sort.insert_sort(array)
    t1 = time.time()
    print "Elapsed time (insert sort): " + elapsed_to_str(t0, t1) + " sec."
コード例 #6
0
list_large = []
for i in range(5000):
    list_large.append(random.randint(1, 100))

print 'size:5000, for the slower ones'

timer = timer.Timer(time.clock())  #initialize the timer

print 'bubble sort\n'
list = sort.bubble_sort(list_large[:])
assert sort.check_sorted(list)
timer.get_time(time.clock())
print ''

print 'insertion sort\n'
list = sort.insert_sort(list_large[:])
assert sort.check_sorted(list)
timer.get_time(time.clock())
print ''

print 'insertion sort fast\n'
list = sort.insert_sort_fast(list_large[:])
assert sort.check_sorted(list)
timer.get_time(time.clock())
print ''

print 'heap sort\n'
list = sort.heap_sort(list_large[:])
assert sort.check_sorted(list)
timer.get_time(time.clock())
print ''
コード例 #7
0
def test_sort_deterministic():
    nums = [2, 1, 5, 3, 6, 2, 5]
    ans = [1, 2, 2, 3, 5, 5, 6]
    assert bubble_sort(nums) == ans
    assert insert_sort(nums) == ans
コード例 #8
0
def test_sort_stochastic():
    for i in range(20):
        nums = random.sample(range(10), 5)
        sorted_ans = sorted(nums)
        assert bubble_sort(nums) == sorted_ans
        assert insert_sort(nums) == sorted_ans