Exemplo n.º 1
0
def run_program():
    pygame.init()
    screen = pygame.display.set_mode((900, 650))
    sr_setting = Setting()
    pygame.display.set_caption("Sorting alogrithms")
    fnt = pygame.font.SysFont("comicsans", 25)
    fnt1 = pygame.font.SysFont("comicsans", 30)
    array = [0] * 151
    arr_color = [sr_setting.Green] * 151
    clr = [
        sr_setting.Green, sr_setting.Red, sr_setting.Dark_blue,
        sr_setting.Orange, sr_setting.bg_color
    ]
    pf.shuffel_arr(array)
    while True:
        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    sys.exit()
                if event.key == pygame.K_m:
                    ms.mergesort(array, 1,
                                 len(array) - 1, arr_color, clr, screen, fnt,
                                 fnt1)
                if event.key == pygame.K_b:
                    bs.bubblesort(array, arr_color, clr, screen, fnt, fnt1)
                if event.key == pygame.K_s:
                    qs.quicksort(array, arr_color, clr, screen, fnt, fnt1)
        screen.fill(sr_setting.bg_color)
        pf.draw(screen, fnt, fnt1, array, arr_color, clr)
        pygame.display.flip()
Exemplo n.º 2
0
def test_stable(coll):
    prior = list(coll)
    post = mergesort(coll, by=lambda x: x['a'])
    sorted_vals = map(lambda x: x['a'], coll)
    for v in sorted_vals:
        prior_subset = filter(lambda x: x['a'] == v, prior)
        post_subset = filter(lambda x: x['a'] == v, post)
        assert (prior == post)
Exemplo n.º 3
0
 def test_case_9(self):
     self.assertEqual(
         program.mergesort([
             8, -6, 7, 10, 8, -1, 6, 2, 4, -5, 1, 10, 8, -10, -9, -10, 8, 9,
             -2, 7, -2, 4
         ]), [
             -10, -10, -9, -6, -5, -2, -2, -1, 1, 2, 4, 4, 6, 7, 7, 8, 8, 8,
             8, 9, 10, 10
         ])
Exemplo n.º 4
0
 def test_case_11(self):
     self.assertEqual(
         program.mergesort([
             2, -2, -6, -10, 10, 4, -8, -1, -8, -4, 7, -4, 0, 9, -9, 0, -9,
             -9, 8, 1, -4, 4, 8, 5, 1, 5, 0, 0, 2, -10
         ]), [
             -10, -10, -9, -9, -9, -8, -8, -6, -4, -4, -4, -2, -1, 0, 0, 0,
             0, 1, 1, 2, 2, 4, 4, 5, 5, 7, 8, 8, 9, 10
         ])
Exemplo n.º 5
0
 def test_case_12(self):
     self.assertEqual(
         program.mergesort([
             4, 1, 5, 0, -9, -3, -3, 9, 3, -4, -9, 8, 1, -3, -7, -4, -9, -1,
             -7, -2, -7, 4
         ]), [
             -9, -9, -9, -7, -7, -7, -4, -4, -3, -3, -3, -2, -1, 0, 1, 1, 3,
             4, 4, 5, 8, 9
         ])
Exemplo n.º 6
0
 def test_case_7(self):
     self.assertEqual(
         program.mergesort([
             -4, 5, 10, 8, -10, -6, -4, -2, -5, 3, 5, -4, -5, -1, 1, 6, -7,
             -6, -7, 8
         ]), [
             -10, -7, -7, -6, -6, -5, -5, -4, -4, -4, -2, -1, 1, 3, 5, 5, 6,
             8, 8, 10
         ])
Exemplo n.º 7
0
def StartAlgorithm():
    global data

    if not data: return

    if (algmenu.get() == 'Quick Sort'):
        quicksort(data, 0, len(data) - 1, DrawData, speedScale.get())

    elif ((algmenu.get() == 'Bubble Sort')):
        bubblesort(data, DrawData, speedScale.get())

    elif (algmenu.get() == 'Merge Sort'):
        mergesort(data, DrawData, speedScale.get())

    elif (algmenu.get() == 'Inertion Sort'):
        insertionSort(data, DrawData, speedScale.get())

    elif (algmenu.get() == 'Selection Sort'):
        selectionsort(data, DrawData, speedScale.get())

    elif (algmenu.get() == 'Heap Sort'):
        heapSort(data, DrawData, speedScale.get())

    DrawData(data, ['green' for x in range(len(data))])
Exemplo n.º 8
0
 def test_case_14(self):
     self.assertEqual(
         program.mergesort([
             991, -731, -882, 100, 280, -43, 432, 771, -581, 180, -382,
             -998, 847, 80, -220, 680, 769, -75, -817, 366, 956, 749, 471,
             228, -435, -269, 652, -331, -387, -657, -255, 382, -216, -6,
             -163, -681, 980, 913, -169, 972, -523, 354, 747, 805, 382,
             -827, -796, 372, 753, 519, 906
         ]), [
             -998, -882, -827, -817, -796, -731, -681, -657, -581, -523,
             -435, -387, -382, -331, -269, -255, -220, -216, -169, -163,
             -75, -43, -6, 80, 100, 180, 228, 280, 354, 366, 372, 382, 382,
             432, 471, 519, 652, 680, 747, 749, 753, 769, 771, 805, 847,
             906, 913, 956, 972, 980, 991
         ])
Exemplo n.º 9
0
 def test_case_13(self):
     self.assertEqual(
         program.mergesort([
             427, 787, 222, 996, -359, -614, 246, 230, 107, -706, 568, 9,
             -246, 12, -764, -212, -484, 603, 934, -848, -646, -991, 661,
             -32, -348, -474, -439, -56, 507, 736, 635, -171, -215, 564,
             -710, 710, 565, 892, 970, -755, 55, 821, -3, -153, 240, -160,
             -610, -583, -27, 131
         ]), [
             -991, -848, -764, -755, -710, -706, -646, -614, -610, -583,
             -484, -474, -439, -359, -348, -246, -215, -212, -171, -160,
             -153, -56, -32, -27, -3, 9, 12, 55, 107, 131, 222, 230, 240,
             246, 427, 507, 564, 565, 568, 603, 635, 661, 710, 736, 787,
             821, 892, 934, 970, 996
         ])
Exemplo n.º 10
0
 def test_case_16(self):
     self.assertEqual(
         program.mergesort([
             544, -578, 556, 713, -655, -359, -810, -731, 194, -531, -685,
             689, -279, -738, 886, -54, -320, -500, 738, 445, -401, 993,
             -753, 329, -396, -924, -975, 376, 748, -356, 972, 459, 399,
             669, -488, 568, -702, 551, 763, -90, -249, -45, 452, -917, 394,
             195, -877, 153, 153, 788, 844, 867, 266, -739, 904, -154, -947,
             464, 343, -312, 150, -656, 528, 61, 94, -581
         ]), [
             -975, -947, -924, -917, -877, -810, -753, -739, -738, -731,
             -702, -685, -656, -655, -581, -578, -531, -500, -488, -401,
             -396, -359, -356, -320, -312, -279, -249, -154, -90, -54, -45,
             61, 94, 150, 153, 153, 194, 195, 266, 329, 343, 376, 394, 399,
             445, 452, 459, 464, 528, 544, 551, 556, 568, 669, 689, 713,
             738, 748, 763, 788, 844, 867, 886, 904, 972, 993
         ])
Exemplo n.º 11
0
 def test_case_15(self):
     self.assertEqual(
         program.mergesort([
             384, -67, 120, 759, 697, 232, -7, -557, -772, -987, 687, 397,
             -763, -86, -491, 947, 921, 421, 825, -679, 946, -562, -626,
             -898, 204, 776, -343, 393, 51, -796, -425, 31, 165, 975, -720,
             878, -785, -367, -609, 662, -79, -112, -313, -94, 187, 260, 43,
             85, -746, 612, 67, -389, 508, 777, 624, 993, -581, 34, 444,
             -544, 243, -995, 432, -755, -978, 515, -68, -559, 489, 732,
             -19, -489, 737, 924
         ]), [
             -995, -987, -978, -898, -796, -785, -772, -763, -755, -746,
             -720, -679, -626, -609, -581, -562, -559, -557, -544, -491,
             -489, -425, -389, -367, -343, -313, -112, -94, -86, -79, -68,
             -67, -19, -7, 31, 34, 43, 51, 67, 85, 120, 165, 187, 204, 232,
             243, 260, 384, 393, 397, 421, 432, 444, 489, 508, 515, 612,
             624, 662, 687, 697, 732, 737, 759, 776, 777, 825, 878, 921,
             924, 946, 947, 975, 993
         ])
Exemplo n.º 12
0
 def test_case_17(self):
     self.assertEqual(
         program.mergesort([
             -19, 759, 168, 306, 270, -602, 558, -821, -599, 328, 753, -50,
             -568, 268, -92, 381, -96, 730, 629, 678, -837, 351, 896, 63,
             -85, 437, -453, -991, 294, -384, -628, -529, 518, 613, -319,
             -519, -220, -67, 834, 619, 802, 207, 946, -904, 295, 718, -740,
             -557, -560, 80, 296, -90, 401, 407, 798, 254, 154, 387, 434,
             491, 228, 307, 268, 505, -415, -976, 676, -917, 937, -609, 593,
             -36, 881, 607, 121, -373, 915, -885, 879, 391, -158, 588, -641,
             -937, 986, 949, -321
         ]), [
             -991, -976, -937, -917, -904, -885, -837, -821, -740, -641,
             -628, -609, -602, -599, -568, -560, -557, -529, -519, -453,
             -415, -384, -373, -321, -319, -220, -158, -96, -92, -90, -85,
             -67, -50, -36, -19, 63, 80, 121, 154, 168, 207, 228, 254, 268,
             268, 270, 294, 295, 296, 306, 307, 328, 351, 381, 387, 391,
             401, 407, 434, 437, 491, 505, 518, 558, 588, 593, 607, 613,
             619, 629, 676, 678, 718, 730, 753, 759, 798, 802, 834, 879,
             881, 896, 915, 937, 946, 949, 986
         ])
Exemplo n.º 13
0
 def test_case_18(self):
     self.assertEqual(
         program.mergesort([
             -823, 164, 48, -987, 323, 399, -293, 183, -908, -376, 14, 980,
             965, 842, 422, 829, 59, 724, -415, -733, 356, -855, -155, 52,
             328, -544, -371, -160, -942, -51, 700, -363, -353, -359, 238,
             892, -730, -575, 892, 490, 490, 995, 572, 888, -935, 919, -191,
             646, -120, 125, -817, 341, -575, 372, -874, 243, 610, -36,
             -685, -337, -13, 295, 800, -950, -949, -257, 631, -542, 201,
             -796, 157, 950, 540, -846, -265, 746, 355, -578, -441, -254,
             -941, -738, -469, -167, -420, -126, -410, 59
         ]), [
             -987, -950, -949, -942, -941, -935, -908, -874, -855, -846,
             -823, -817, -796, -738, -733, -730, -685, -578, -575, -575,
             -544, -542, -469, -441, -420, -415, -410, -376, -371, -363,
             -359, -353, -337, -293, -265, -257, -254, -191, -167, -160,
             -155, -126, -120, -51, -36, -13, 14, 48, 52, 59, 59, 125, 157,
             164, 183, 201, 238, 243, 295, 323, 328, 341, 355, 356, 372,
             399, 422, 490, 490, 540, 572, 610, 631, 646, 700, 724, 746,
             800, 829, 842, 888, 892, 892, 919, 950, 965, 980, 995
         ])
Exemplo n.º 14
0
 def test_case_5(self):
     self.assertEqual(program.mergesort([3, 1, 2]), [1, 2, 3])
Exemplo n.º 15
0
import merge_sort

for i in range(1, 4, 1):
    test_name = 'test_' + str(i) + '.txt'
    ans_name = 'ans_' + str(i) + '.txt'
    test_file = open(test_name, 'r')
    ans_file = open(ans_name, 'r')

    test = test_file.readline().split()
    test = list(map(int, test))
    count = 0
    ans = ans_file.readline().split()
    ans = list(map(int, ans))
    ans_count = int(ans_file.readline())
    result, count = merge_sort.mergesort(test, count)
    if result == ans and ans_count == count:
        print('Pass test case %d.' % i)
    else:
        print('Test case %d fail.' % i)

Exemplo n.º 16
0
# Sort a user provided list

from merge_sort import mergesort
from quick_sort import quicksort
from heap_sort import heapsort
from bubble_sort import bubblesort

# Check if input is an int
def represents_int(s):
    try:
        int(s)
        return True
    except ValueError:
        return False

print "Enter integers separated by a comma (e.g. 1,3,4,6)\n"
user_input = raw_input("Enter Numbers: ")

input_list = [ int(x.strip()) for x in user_input.split(',') if(represents_int(x)) ]
print "You entered: " + str(input_list)
print "merge sort: " + str(mergesort(input_list))
print "quick sort: " + str(quicksort(input_list))
print "heap sort: " + str(heapsort(input_list))
print "bubble sort: " + str(bubblesort(input_list))
Exemplo n.º 17
0
def test_worst_case():
    worst_case = [i for i in range(1, 10 ** 5) if i % 2] + [i for i in range(1, 10 ** 5) if not i % 2]
    assert mergesort(worst_case) == range(1, 10 ** 5)
Exemplo n.º 18
0
def test_empty_list():
    assert mergesort([]) == []
Exemplo n.º 19
0
def test_sorted_list():
    presorted = range(30)
    assert mergesort(presorted) == presorted
Exemplo n.º 20
0
 def test_case_2(self):
     self.assertEqual(program.mergesort([1, 2]), [1, 2])
Exemplo n.º 21
0
 def test_case_10(self):
     self.assertEqual(
         program.mergesort([5, -2, 2, -8, 3, -10, -6, -1, 2, -2, 9, 1, 1]),
         [-10, -8, -6, -2, -2, -1, 1, 1, 2, 2, 3, 5, 9])
Exemplo n.º 22
0
def test_normal_case():
    normal_case = [random.randint(0, 100) for i in range(15)]
    assert mergesort(normal_case) == sorted(normal_case)
Exemplo n.º 23
0
def test_repeating_list():
    repeating = [4] * 30
    assert mergesort(repeating) == [4] * 30
Exemplo n.º 24
0
import selection_sort
import merge_sort
import insertion_sort
import counting_sort
import bubble_sort

# how to use selection sort
# give nlist as an input list to sort
nlist = [9, 12, 98, 17, 22, 3]
selection_sort.selectionsort(nlist)
print(nlist)

# how to use merge sort
# give nlist as an input list to sort
nlist = [23, 88, 98, 99, 12, 18, 71, 4]
merge_sort.mergesort(nlist)
print(nlist)

# how to use insertion sort
# give 'data' as an input list to sort
data = [3, 8, 12, 98, 92, 64, 23, 10]
insertion_sort.insertion(data)
print(data)

# how to use counting sort
# give nlist as an input list to sort
# and an integer to set it as maximum value
nlist = [1, 4, 1, 2, 2, 5, 2]
max_value = 7
counting_sort.counting_sort(nlist, max_value)
print(nlist)
Exemplo n.º 25
0
def test_reversed_sorted():
    backwards_sort = range(30, -1, -1)
    assert mergesort(backwards_sort) == range(31)
        y = np.random.randint(1, n + 1, n)
        fig, axes = plt.subplots()

        if ch == 1:
            title = 'BUBBLE SORT O(n**2)'
            generator = bubblesort(y)
        elif ch == 2:
            title = 'SELECTION SORT O(n**2)'
            generator = selectionsort(y)
        elif ch == 3:
            title = 'INSERTION SORT O(n**2)'
            generator = insertionsort(y)
        elif ch == 4:
            title = 'MERGE SORT O(n*log(n))'
            generator = mergesort(y, 0, n - 1)
        elif ch == 5:
            title = 'QUICK SORT O(n*log(n))'
            generator = quicksort(y, 0, n - 1)
        elif ch == 6:
            title = 'HEAP SORT O(n**log(n))'
            generator = heapsort(y, n)

        mycmap = cm.get_cmap('rainbow')

        my_norm = Normalize(vmin=0, vmax=100)

        barr = axes.bar(np.arange(1, n + 1),
                        y,
                        align='edge',
                        color=mycmap(my_norm(y)))
Exemplo n.º 27
0
def test_list_len_one():
    assert mergesort([2]) == [2]
Exemplo n.º 28
0
 def firstname(self):
     self.sort_list = merge_sort.mergesort(self.leaderboard,0)
Exemplo n.º 29
0
import time
import random
from bubble_sort import sort as bubblesort
from merge_sort import sort as mergesort
from quick_sort import sort as quicksort

for n in [10, 100, 1000, 10000, 100000]:
    array = [random.randint(1, 10) for i in range(n)]

    # bubblestart = time.time()
    # bubblesorted = bubblesort(array)
    # bubbleend = time.time()
    # bubbletime = bubbleend - bubblestart

    mergestart = time.time()
    mergesorted = mergesort(array)
    mergeend = time.time()
    mergetime = mergeend - mergestart

    quickstart = time.time()
    quicksorted = quicksort(array)
    quickend = time.time()
    quicktime = quickend - quickstart

    times = {
        # 'Bubble sort': bubbletime,
        'Merge sort': mergetime,
        'Quick sort': quicktime
    }

    print()
Exemplo n.º 30
0
def test_idempotent(coll):
    first = mergesort(coll)
    second = mergesort(first)
    assert (first == second)
Exemplo n.º 31
0
import socket 
import merge_sort as ms 

HOST = '192.168.1.1' 
PORT = 50007 
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
s.connect((HOST, PORT)) 

#Receives arraystring in chunks 
arraystring = '' 
print '[DEBUG] Receiving data...' 
while 1:
	data = s.recv(4096)	# Receives data in chunks 
	arraystring += data	# Adds data to array string 
	if ']' in data:
		break
array = eval(arraystring)	
print '[DEBUG] Data received, sorting array... ' 

# Sorts assigned subarray
array = ms.mergesort(array) 
print '[DEBUG] Array sorted, sending data...' 

# Converts array into string to be sent back to server 
arraystring = repr(array) 
s.sendall(arraystring)
print '[DEBUG] Data sent.' 

s.close()
Exemplo n.º 32
0
merge = []
quick = []
quickmed = []
for i in range(iter_num):
    for a in range(length):
        arr = rnd.randint(-length * 10, length * 10)
        array.append(arr)
    ar, insertion_time = insertionsort(array)
    insertion.append(insertion_time)
    ar, selection_time = selectionsort(array)
    selection.append(selection_time)
    ar, bubble_time = bubblesort(array)
    bubble.append(bubble_time)
    ar, heap_time = insertionsort(array)
    heap.append(heap_time)
    ar, merge_time = mergesort(array)
    merge.append(merge_time)
    ar, quick_time = quicksort(array, 0, len(array) - 1)
    quick.append(quick_time)
    ar, quickmed_time = quicksortmed(array, 0, len(array) - 1)
    quickmed.append(quickmed_time)
    input_sizes.append(length)
    length = length * 3

print("Input sizes: ", input_sizes)

plt.plot(input_sizes, insertion, label="Insertion sort")
plt.plot(input_sizes, selection, label="Selection sort")
plt.plot(input_sizes, bubble, label="Bubble sort")
plt.plot(input_sizes, heap, label="Heap sort")
plt.plot(input_sizes, merge, label="Merge sort")
Exemplo n.º 33
0
print('Bubble Sort : ')
bubble_sort.bubblesort(list_bs)

print('=====================================================')

list_is = [76, 33, 46, 64, 20]
print('Data Sebelum : ', list_is)
print('Insertion Sort : ')
insertion_sort.insertionsort(list_is)

print('=====================================================')

list_ms = [2, 5, 60, 43]
print('Data Sebelum : ', list_ms)
print('Merge Sort : ')
merge_sort.mergesort(list_ms)

print('=====================================================')

list_ss = [11, 57, 33, 44, 55, 81]
print('Data Sebelum : ', list_ss)
print('Selection Sort : ')
selection_sort.selectionsort(list_ss)

print('=====================================================')
list_shs = [12, 34, 54, 2, 3]
n = len(list_shs)
print("Data Sebelum : ")
for i in range(n):
    print(list_shs[i]),
def test_mergesort():

    assert mergesort([1, 5, 4, 7, 3]) == [1, 3, 4, 5, 7]
Exemplo n.º 35
0
 def surname(self):
     self.sort_list = merge_sort.mergesort(self.leaderboard,1)
Exemplo n.º 36
0
 def test_case_8(self):
     self.assertEqual(
         program.mergesort([
             -7, 2, 3, 8, -10, 4, -6, -10, -2, -7, 10, 5, 2, 9, -9, -5, 3, 8
         ]),
         [-10, -10, -9, -7, -7, -6, -5, -2, 2, 2, 3, 3, 4, 5, 8, 8, 9, 10])
Exemplo n.º 37
0
from random import randint
from time import time



from insert_sort import insertsort 
from merge_sort import mergesort
from quick_sort import quicksort

for i in range(10):
    SIZE = 10 ** i
    print "tamanho", SIZE
    agora = time()
    quicksort([ randint(0,99) for i in range(SIZE)])
    pronto = time()
    print "qs", pronto - agora

    agora = time()
    insertsort([ randint(0,99) for i in range(SIZE)])
    pronto = time()
    print "insert", pronto - agora

    agora = time()
    mergesort([ randint(0,99) for i in range(SIZE)])
    pronto = time()
    print "merge", pronto - agora




Exemplo n.º 38
0
for i in range(procno - 1):	
	conn, addr = s.accept()	
    print '[DEBUG] Connected with ' + addr[0] + ':' + str(addr[1])
	addr_list.append(addr)	

# Start and time distributed computing sorting process	
start_time = time.time()
sections = breakarray(array, procno) # splits array into sections for every client 

for i in range(procno - 1):	# Converts array section into string to be sent
	arraystring = repr(sections[i+1]) 
	conn.sendto(arraystring, addr_list[i])

print '[DEBUG] Data sent, sorting array...'
array = ms.mergesort(sections[procID])	# Sorts section and stores it in array 
print '[DEBUG] Array sorted.' 

for i in range(procno - 1):	# Receives sorted sections from each client
	arraystring = '' 
	print '[DEBUG] Receiving data from clients...' 
	while 1:
		data = conn.recv(4096) 
		arraystring += data	
		if ']' in data:	# When end of data is received
			break

	print '[DEBUG] Data received, merging arrays...'	
	array = ms.merge(array, eval(arraystring))	# Merges current array with section from client
	print '[DEBUG] Arrays merged.'