Example #1
0
def start_algorithm():
	global data, result, numbers
	
	if not data:
		return

	if (algmenu.get() == 'Selection Sort'):
		selection_sort(data, drawData)
		drawData(data, ['Green' for x in range(len(data))])
		result = "Selection Sort \n" + ' '.join(map(str, data))
		data = copy.deepcopy(numbers)

	if (algmenu.get() == 'Insertion Sort'):
		insertion_sort(data, drawData)
		drawData(data, ['Green' for x in range(len(data))])
		result = "Insertion Sort \n" + ' '.join(map(str, data))
		data = copy.deepcopy(numbers)

	if (algmenu.get() == 'Bubble Sort'):
		bubble_sort(data, drawData)
		drawData(data, ['Green' for x in range(len(data))])
		result = "Bubble Sort \n" + ' '.join(map(str, data))
		data = copy.deepcopy(numbers)

	if (algmenu.get() == 'Quick Sort'):
		quick_sort(data, 0, len(data)-1, drawData)
		drawData(data, ['Green' for x in range(len(data))])
		result = "Quick Sort \n" + ' '.join(map(str, data))
		data = copy.deepcopy(numbers)

	if (algmenu.get() == 'Merge Sort'):
		merge_sort(data, 0, len(data)-1, drawData)
		drawData(data, ['Green' for x in range(len(data))])
		result = "Merge Sort \n"' '.join(map(str, data))
		data = copy.deepcopy(numbers)
Example #2
0
def wx_gauss(wx, m, sigma):
    wy = []
    for i in range(len(wx)):
        wy.append((1 / (sigma * np.sqrt(2 * np.pi))) *
                  np.exp(-1 * (wx[i] - m)**2 / (2 * (sigma**2))))
    bubble.bubble_sort(wx, wy)
    plt.plot(wx, wy, color='r')
Example #3
0
def get_piecewice_parabolic_interpolation(x, y, z):
    print("сортировка массивов")
    print(x, y)
    bubble_sort(x, y)
    print(x, y)
    xnew = np.linspace(np.min(x), np.max(x), z)
    ynew = [piecewice_parabolic_interpolation(x, y, i) for i in xnew]
    return xnew, ynew
Example #4
0
def get_by_scipy(x, y, z):
    print("сортировка массивов")
    print(x, y)
    bubble_sort(x, y)
    print(x, y)
    spl = InterpolatedUnivariateSpline(x, y)
    xnew = np.linspace(np.min(x), np.max(x), z)
    ynew = spl(xnew)
    return xnew, ynew
Example #5
0
def wx_rayleigh(wx, sigma):
    wy = []
    for i in range(len(wx)):
        wy.append((wx[i] / (sigma**2)) * np.exp(-1 * ((wx[i]**2) /
                                                      (2 * (sigma**2)))))
    print(wx, "неотфильтр значения ")
    print(wy)
    bubble.bubble_sort(wx, wy)
    print(wx, "отфильтр значения ")
    print(wy)
    plt.plot(wx, wy, color='r')
Example #6
0
def StartAlgo():
    global data
    if not data:
        return
    if menu.get() == 'Quick Sort':
        quick_sort(data, 0, len(data) - 1, drawbar, visualspeed.get())
    elif menu.get() == 'Merge Sort':
        merge_sort(data, drawbar, visualspeed.get())
    elif menu.get() == 'Bubble Sort':
        bubble_sort(data, drawbar, visualspeed.get())
    drawbar(data, ['green' for x in range(len(data))])
Example #7
0
def StartAlgo():
    global data
    speedforce = 0.01 * (100 - speedscale.get())
    sorter = selected_algo.get()
    if sorter == 'bubble SORT':
        bubble_sort(data, drawdata, speedforce)
    elif sorter == 'quick SORT':
        quick_sort(data, 0, len(data) - 1, drawdata, speedforce)
        drawdata(data, ['green' for x in range(len(data))])
    elif sorter == 'merge SORT':
        merge_sort(data, data, 0, len(data) - 1, drawdata, speedforce)
Example #8
0
def merge_sort(filename, comp = cmp, key = None):
	files = []
	cur_line = []
	f = open(filename, 'r')
	RAMSIZE = 10
	for line in f:
		if line.isspace(): continue
		cur_line.append(line)
		if len(cur_line) == RAMSIZE:
			bubble_sort(cur_line, comp, key)
			if files == None:
				temp_f = open('tmp_0.txt', 'w')
			else:
				temp_f = open('tmp_' + str(len(files)) + '.txt', 'w')
			for i in cur_line:
				temp_f.write(i)
			temp_f.close()
			files.append(temp_f)
			cur_line = []
	if cur_line:
		bubble_sort(cur_line, comp, key)
		if files == None:
			temp_f = open('tmp_0.txt', 'w')
		else:
				temp_f = open('tmp_' + str(len(files)) + '.txt', 'w')
		for i in cur_line:
				temp_f.write(i)
		temp_f.close()
		files.append(temp_f)
	f.close()
	f2 = open('mysort.txt', 'w')
	buffers = []
	for i in range(len(files)): 
		files[i] = open('tmp_' + str(i) + '.txt', 'r')
	for line in files: 
		buffers.append(line.readline())
	while files:
		min_n = 0
		for i in range(1, len(buffers)):
			if comp(buffers[min_n], buffers[i]) > 0:
				min_n = i
		f2.write(buffers[min_n])
		t = files[min_n].readline()
		if t == '':
			del files[min_n]
			del buffers[min_n]
		else:
			buffers[min_n] = t
	f2.close()
Example #9
0
def simulation():    
    
    bubble_time = 0
    selection_time = 0
    insertion_time = 0
    merge_time = 0
    quick_time = 0
    tim_time = 0
    
    for i in range(EXPERIMENTS):

        # create a list with some number of values from the given range
        my_list = random.sample(range(MAX_VALUE), LIST_SIZE)    
        start = time.time()
        bubble_sort(my_list)
        bubble_time += time.time() - start
        
        my_list = random.sample(range(MAX_VALUE), LIST_SIZE)    
        start = time.time()
        selection_sort(my_list)
        selection_time += time.time() - start
        
        my_list = random.sample(range(MAX_VALUE), LIST_SIZE)    
        start = time.time()
        insertion_sort(my_list)
        insertion_time += time.time() - start
        
        my_list = random.sample(range(MAX_VALUE), LIST_SIZE)    
        start = time.time()
        merge_sort(my_list)
        merge_time += time.time() - start
        
        my_list = random.sample(range(MAX_VALUE), LIST_SIZE)    
        start = time.time()
        quick_sort(my_list)
        quick_time += time.time() - start
        
        my_list = random.sample(range(MAX_VALUE), LIST_SIZE)    
        start = time.time()
        my_list.sort()
        tim_time += time.time() - start
        
    print('Bubble sort took', bubble_time/EXPERIMENTS*1000, 'ms')    
    print('Selection sort took', selection_time/EXPERIMENTS*1000, 'ms')    
    print('Insertion sort took', insertion_time/EXPERIMENTS*1000, 'ms')      
    print('Merge sort took', merge_time/EXPERIMENTS*1000, 'ms')    
    print('Quick sort took', quick_time/EXPERIMENTS*1000, 'ms')
    print('Timsort took', tim_time/EXPERIMENTS*1000, 'ms')
Example #10
0
def test_bubble_sort_sorts_big_list():
    """Test that bubble sort sorts big list."""
    from bubble import bubble_sort
    from random import shuffle
    big_list = list(range(100))
    shuffle(big_list)
    assert bubble_sort(big_list) == list(range(100))
Example #11
0
def test_merge_sort(fixtures):
    for fixture in fixtures:
        assert merge_sort(fixture[0]) == fixture[1]
        assert quick_sort(fixture[0]) == fixture[1]
        assert insertion_sort(fixture[0]) == fixture[1]
        assert bubble_sort(fixture[0]) == fixture[1]
        assert heap_sort(fixture[0]) == fixture[1]
Example #12
0
def sort():
    global data
    if not data:
        return
    print("Selected Algorithm:" + option1.get())
    if algoslec.get() == 'Bubble Sort':
        bubble_sort(data, visualise, float(timeslec.get()))

    if algoslec.get() == 'Merge Sort':
        merge_sort(data, visualise, float(timeslec.get()))

    if algoslec.get() == 'Selection Sort':
        select_sort(data, visualise, float(timeslec.get()))

    if algoslec.get() == 'Quick Sort':
        quick_sort(data, 0, len(data) - 1, visualise, float(timeslec.get()))

    visualise(data, ['#D5F5E3' for x in range(len(data))])
Example #13
0
 def test_bubble_sort_empty(self):
     self.assertEqual(bubble_sort([]), 'Nothing to sort.')
Example #14
0
 def test_bubble_sort_none(self):
     self.assertEqual(bubble_sort(None), 'Nothing to sort.')
Example #15
0
 def test_bubble_sort_nums_positive_and_negative(self):
     self.assertEqual(bubble_sort([-12, -19, -20, 45, 91, 55]),
                      [-20, -19, -12, 45, 55, 91])
Example #16
0
max_bubble = []
max_quick = []
max_counting_small = []
max_counting_large = []
max_radix = []

#bubble-quick
for cicle in range(5):
  for size in range(Steps):
    realSize = MaxSize/Steps*size if MaxSize/Steps*size > 1 else 2 
    size_array.append(realSize)
    sequence = getSequence(realSize)
    
    bubbleSequence = sequence[:]
    startTime = time.time()
    bubble.bubble_sort(bubbleSequence)
    endTime = time.time()
    table_bubble[size][cicle] = endTime - startTime

    quickSequence = sequence[:]
    startTime = time.time()
    quick.quick_sort(quickSequence)
    endTime = time.time()
    table_quick[size][cicle] = endTime - startTime
    print size

print "done-quick/bubble"

#counting - k=3
for cicle in range(5):
  for size in range(Steps):
Example #17
0
 def test_sorted(self):
     """add bubble test for sorted array"""
     _sorted = [1, 2, 3, 4]
     self.assertEqual(_sorted, bubble_sort(_sorted))
Example #18
0
def test_bubble_sort_short_list():
    """Test bubble with small list."""
    short_list = [4, 3, 7, 6]
    assert bubble_sort(short_list) == [3, 4, 6, 7]
Example #19
0
def test_bubble_sort_negative_num():
    """Test bubble sort works with negative numbers."""
    list_negative_num = [72, 4, -6]
    assert bubble_sort(list_negative_num) == [-6, 4, 72]
Example #20
0
def test_bubble_non_int_raises_error():
    """Entering an iterable containing non-integers raises an error."""
    from bubble import bubble_sort
    with pytest.raises(ValueError):
        bubble_sort([1, 2, 3, 5, 'burp'])
Example #21
0
    print("partition_sorted_list1: " + str(partition_sorted_list1))
    print("partition_sorted_list2: " + str(partition_sorted_list2))
    print("partition_sorted_list3: " + str(partition_sorted_list3))

    # 归并排序
    list1, list2, list3 = get_list()
    merge_sorted_list1 = merge_sort(list1)
    merge_sorted_list2 = merge_sort(list2)
    merge_sorted_list3 = merge_sort(list3)
    print("merge_sorted_list1: " + str(merge_sorted_list1))
    print("merge_sorted_list2: " + str(merge_sorted_list2))
    print("merge_sorted_list3: " + str(merge_sorted_list3))

    # 冒泡排序
    list1, list2, list3 = get_list()
    bubble_sorted_list1 = bubble_sort(list1)
    bubble_sorted_list2 = bubble_sort(list2)
    bubble_sorted_list3 = bubble_sort(list3)
    print("bubble_sorted_list1: " + str(bubble_sorted_list1))
    print("bubble_sorted_list2: " + str(bubble_sorted_list2))
    print("bubble_sorted_list3: " + str(bubble_sorted_list3))

    # 选择排序
    list1, list2, list3 = get_list()
    select_sorted_list1 = select_sort(list1)
    select_sorted_list2 = select_sort(list2)
    select_sorted_list3 = select_sort(list3)
    print("select_sorted_list1: " + str(select_sorted_list1))
    print("select_sorted_list2: " + str(select_sorted_list2))
    print("select_sorted_list3: " + str(select_sorted_list3))
Example #22
0
def test_bubble():
    """test bubble sort function on list of ints."""
    a = [123, 55, 2, 7, 22, -10, 1]
    bubble_sort(a)
    assert a == [-10, 1, 2, 7, 22, 55, 123]
Example #23
0
def is_anagram(s1, s2):
    s1, s2 = list(s1), list(s2)
    bubble_sort(s1), bubble_sort(s2)
    return s1 == s2
Example #24
0
 def test_bubble_sort(self):
     bubble_sort(self.shuffle_lst)
     self.assertEqual(self.shuffle_lst, self.ordered_lst)
Example #25
0
def test_bubble_sort_long_list():
    """Test bubble with long list."""
    long_list = [72, 4, 10, 6, 20, 18, 91, 45, 3, 15]
    assert bubble_sort(long_list) == [3, 4, 6, 10, 15, 18, 20, 45, 72, 91]
Example #26
0
def test_bubble_sort_sorts_random_list():
    """Bubble sort returns an ordered list."""
    from bubble import bubble_sort
    input = [randint(0, 1000) for i in range(100)]
    expected = sorted(input)
    assert bubble_sort(input) == expected
Example #27
0
def test_bubble_sort_decimals():
    """Test bubble sort takes a decimal float."""
    list_decimals = [5.5, 5.3, 5.2, 4]
    assert bubble_sort(list_decimals) == [4, 5.2, 5.3, 5.5]
def bubble(*args):
	return bubble_sort(*args)
Example #29
0
 def test_bubble(self):
     """add bubble tests for unsorted array"""
     unsorted = [4, 3, 2, 1, 12]
     _sorted = bubble_sort(unsorted)
     expected_results = [1, 2, 3, 4, 12]
     self.assertEqual(expected_results, _sorted)
Example #30
0
 def test_bubble_sort_nums_positive(self):
     self.assertEqual(bubble_sort([20, 12, 45, 19, 91, 55]),
                      [12, 19, 20, 45, 55, 91])
Example #31
0
def test_bubble_non_list_raises_error():
    """Entering a non-list/tuple param raises an error."""
    from bubble import bubble_sort
    with pytest.raises(TypeError):
        bubble_sort('Hello')
Example #32
0
 def test_bubble_sort_nums_negative(self):
     self.assertEqual(bubble_sort([-20, -12, -45, -19, -91, -55]),
                      [-91, -55, -45, -20, -19, -12])
Example #33
0
def test_bubble_sort_returns_ordered_list(input, expected):
    """Bubble sort returns an ordered list."""
    from bubble import bubble_sort
    assert bubble_sort(input) == expected
Example #34
0
 def test_bubble_sort_not_nums(self):
     self.assertEqual(bubble_sort([0, 'a', 3]),
                      'Can only sort lists of just numbers.')
     self.assertEqual(bubble_sort(['a', 'b', 'c']),
                      'Can only sort lists of just numbers.')
Example #35
0
def test_bubble_sort_sorts(input, output):
    """Check if bubble sort sorts lists given against sorted lists given as output."""
    assert bubble_sort(input) == output
Example #36
0
 def test_bubble_sort_single_value(self):
     self.assertEqual(bubble_sort([5]), [5])