コード例 #1
0
ファイル: test_bubble_sort.py プロジェクト: DDilshani/COMLabs
def test_bubble_sort():

    import bubble_sort

    array1 = [6, 2, 8, 22, 1]
    #print (bubble_sort(array1))
    bubble_sort(array1)
コード例 #2
0
	def test_bubble_sort(self):
		self.assertEqual([0], bubble_sort([0]))
		self.assertEqual([-2,-1,0,1,2], bubble_sort([-1,-2,2,1,0]))
		
		self.assertEqual(["a"], bubble_sort(["a"]))
		self.assertEqual(["a","b","c","d"], bubble_sort(["c","d","b","a"]))
		
		self.assertEqual([1,2,3,"a","b","c"], bubble_sort(["b","c",2,"a",1,3]))
コード例 #3
0
def start_algorithm():
    global data
    if not data: return 

    if(alg_menu.get() == 'Quick sort'):
        quick_sort(data, 0,len(data) - 1,draw_data,speed_scale.get())
    elif alg_menu.get() == 'Bubble sort':
        bubble_sort(data,draw_data,speed_scale.get())
    elif alg_menu.get() == 'Merge sort':
        merge_sort(data, draw_data, speed_scale.get())
    elif alg_menu.get() == 'Insertion sort':
        insertion_sort(data, draw_data, speed_scale.get())
    draw_data(data,[['green'] for x in range(len(data))])
コード例 #4
0
class bubbleSortTestMethods(unittest.TestCase):

    bubble_sort Bubble = new bubble_sort()
    def setUp(self):
        self.unsortedArray = [3,2,1,4,5]


    def test_default(self):
     #   self.assertCountEqual(Bubble.bu,[1,2,3,4,5], 'Wrong after sorting')
コード例 #5
0
def main():
    list_1 = random.sample(range(200000000), 100000)
    t_1 = time.time()
    bubble_sort(list_1)
    t_2 = time.time()
    print("bubble_sort_took", t_2 - t_1)
    t_1 = time.time()
    merge_sort(list_1, 0, 3)
    t_2 = time.time()
    print("merge_sort_took", t_2 - t_1)
    t_1 = time.time()
    merge_sort_without(list_1, 0, 3)
    t_2 = time.time()
    print("merge_sort_without_sentinel_took", t_2 - t_1)
    t_1 = time.time()
    insertion_sort(list_1)
    t_2 = time.time()
    print("insertion_sort", t_2 - t_1)
コード例 #6
0
def start():
    print('Sorting...')
    global data
    if not data:
        return

    # These if else statements call the function of the selected algorithm
    if algMenu.get() == 'Quick Sort':
        quick_sort(data, draw_data, speedScale.get())
    elif algMenu.get() == 'Bubble Sort':
        bubble_sort(data, draw_data, speedScale.get())
    elif algMenu.get() == 'Insertion Sort':
        insertion_sort(data, draw_data, speedScale.get())
    elif algMenu.get() == 'Selection Sort':
        selection_sort(data, draw_data, speedScale.get())
    elif algMenu.get() == 'Merge Sort':
        merge_sort(data, 0, len(data) - 1, draw_data, speedScale.get())
    elif algMenu.get() == 'Counting Sort':
        count_sort(data, draw_data, speedScale.get())
コード例 #7
0
ファイル: test_sort.py プロジェクト: Lai-YT/sort-prc
    def test_sort(self):
        self._list.push_back(10)
        self._list.push_back(5)
        self._list.push_back(9)
        self._list.push_back(6)
        self._list.push_back(7)
        self._list.push_back(8)
        self._list.push_back(4)
        self._list.push_back(1)
        self._list.push_back(2)
        self._list.push_back(3)
        # 10->5->9->6->7->8->4->1->2->3

        bubble_sort(self._list)
        # 1->2->3->4->5->6->7->8->9->10

        curr = self._list.front
        for i in range(1, 11):
            self.assertEqual(i, curr.val)
            curr = curr.next
コード例 #8
0
ファイル: tests.py プロジェクト: JackMorris/Python-Utilities
 def test_zero_input(self):
     data = []
     bubble_sort(data)
     self.assertEqual(data, [])
コード例 #9
0
sys.path.append(
    sys.path.insert(
        0, os.path.abspath(os.path.join(os.path.dirname(__file__), '.'))))

from random import random
from datetime import datetime
from insertion_sort import *
from merge_sort import *
from bubble_sort import *

N = 4000

values = [int(random() * N * 1000) for y in range(N)]
t1 = datetime.now()
bubble_sort(values)
t2 = datetime.now()
print("bubble sort time:   ", (t2 - t1).total_seconds() * 1000)

values = [int(random() * N * 1000) for y in range(N)]
t1 = datetime.now()
insertion_sort(values)
t2 = datetime.now()
print("insert sort time:   ", (t2 - t1).total_seconds() * 1000)

values = [int(random() * N * 1000) for y in range(N)]
t1 = datetime.now()
merge_sort(values, 0, len(values) - 1)
t2 = datetime.now()
print("merge sort time:   ", (t2 - t1).total_seconds() * 1000)
コード例 #10
0
 def test_is_string(self):
     a_list = [5, 1, 90, 54, 72, 39, 20]
     self.assertEqual(bubble_sort(a_list), [1, 5, 20, 39, 54, 72, 90])
コード例 #11
0
    start_time = t.time()
    insert_sort(test_arr)
    total_time = t.time() - start_time
    data["insertion_sort"].append(total_time)

    #merge sort
    test_arr = copy_arr()
    start_time = t.time()
    merge_sort(test_arr)
    total_time = t.time() - start_time
    data["merge_sort"].append(total_time)

    #bubble sort
    test_arr = copy_arr()
    start_time = t.time()
    bubble_sort(test_arr)
    total_time = t.time() - start_time
    data["bubble_sort"].append(total_time)

    #sorted insert sort
    test_arr = copy_arr()
    start_time = t.time()
    sorted_insert_sort(test_arr)
    total_time = t.time() - start_time
    data["sorted_insert_sort"].append(total_time)

for s in sorts:  #graph the data
    plt.plot([i for i in range(min_size, max_size + 1)], data[s])

#configure graph
plt.xlabel("Length of array")
コード例 #12
0
import bubble_sort
def reverse(some_function):


    def wrapper(*arg, **kwarg):
      
      result =  some_function(*arg)
      return result[::-1] 
    return wrapper

@reverse
def bubble_sort(list1):
    swapped = 0
    for i in range(0, len(list1)):
        for j in range(0, len(list1)-1-i):
            if list1[j] > list1[j+1]:
                list1[j], list1[j+1] = list1[j+1], list1[j]
                swapped = 1
            if swapped == 0:
                return list1
    return list1

arr = [1, 3, 2, 5, 7]
print bubble_sort(arr)
コード例 #13
0
print()

print("Quick sorted: ")
quick_sorted_list = list(original)
a = time.time()
quick_sort(quick_sorted_list)
b = time.time()
print(b - a)
print_list(quick_sorted_list)
test_properties(sorted, quick_sorted_list)
print()

print("Max heap sorted: ")
max_heap_sorted_list = list(original)
a = time.time()
max_heap_sorted_list = max_heap_sort(max_heap_sorted_list)
b = time.time()
print(b - a)
max_heap_sorted_list = list(reversed(max_heap_sorted_list))
print_list(max_heap_sorted_list)
test_properties(sorted, max_heap_sorted_list)
print()

print("Bubble sorted: ")
bubble_sorted_list = list(original)
a = time.time()
bubble_sort(bubble_sorted_list)
b = time.time()
print(b - a)
print_list(bubble_sorted_list)
test_properties(sorted, bubble_sorted_list)
コード例 #14
0
from heap_sort import *


if __name__ == "__main__":

  buckets = 3

  all_lists = {}
  all_lists['ALREADY_SORTED'] = [1, 2, 4, 4, 6, 6, 8, 9]
  all_lists['REVERSE_SORTED'] = [9, 8, 6, 6, 4, 4, 2, 1]
  all_lists['FEW_UNIQUE'] = [3, 6, 6, 4, 3, 3, 3, 6, 6, 4]
  all_lists['RANDOM_ORDER'] = [2, 6, 4, 8, 1, 4, 6, 9]
  all_lists['BIG_RANGE'] = [170, 45, 75, 90, 802, 2, 24, 66]
  all_lists['LONG_LIST'] = [2,4,3,3,6,7,876,2,4,8,214,7,2,7,4,8,2,7,4,6,83,2,6,7,88,75,2,3,6,789,86,3,6,6,3,575,6,87,8,574]
  all_lists['ZEROES_IN_LIST'] = [9,0,3,0,54,6,1,45,1,57,1,56,8,8,3,0]
  all_lists['ALL_SAME_NUMBER'] = [7,7,7,7,7,7,7,7,7,7,7]


  for list_name, number_list in all_lists.iteritems():
    print "\nUSING LIST \n\t%s\n\t%s" %(list_name, number_list)
    
    print "Insertion sorted output: \n\t", insertion_sort(number_list)
    print "Bubble sorted output: \n\t", bubble_sort(number_list)
    print "Selection sorted output: \n\t", selection_sort(number_list)
    print "Bucket sorted output (using insertion sort and %s buckets): \n\t" %(buckets), bucket_sort(number_list, 3)
    print "Counting sorted output: \n\t", counting_sort(number_list)
    print "Quick sorted output: \n\t", quicksort(number_list)
    print "Merge sorted output: \n\t", mergesort(number_list)
    print "Radix sorted output (using counting sort): \n\t", radix_sort(number_list)
    print "Heap sorted output: \n\t", heapsort(number_list)
    
コード例 #15
0
ファイル: sort.py プロジェクト: addie/python
#!/usr/bin/python3

from bubble_sort import *
from insertion_sort import *
from selection_sort import *
from qsort import *

alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
print(alist)
bubble_sort(alist)
print(alist)

alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
print(alist)
selection_sort(alist)
print(alist)

alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
print(alist)
insertion_sort(alist)
print(alist)

alist = [54, 26, 93, 17, 77, 31, 44, 55, 20]
print(alist)
qsort(alist)
print(alist)
コード例 #16
0
ファイル: test_sorting.py プロジェクト: oknono/sorting
print "List to be sorted is {0}".format(values)
print "-" * 30
print "Selection Sort"
print "-" * 30
values1 = values[:]
selection_sort(values1)
print "-" * 30
print "Insertion Sort"
print "-" * 30
values2 = values[:]
insertion_sort(values2)
print "-" * 30
print "Bubble Sort"
print "-" * 30
values3 = values[:]
bubble_sort(values3)
print "-" * 30
print "Comb Sort"
print "-" * 30
values4 = values[:]
comb_sort(values4)
print "-" * 30
print "Shell Sort"
print "-" * 30
values5 = values[:]
shell_sort(values5)
print "-" * 30
print "Merge Sort"
print "-" * 30
values6 = values[:]
merge_sort(values6)
コード例 #17
0
ファイル: sort.py プロジェクト: abendory/python
#!/usr/bin/python3

from bubble_sort import *
from insertion_sort import *
from selection_sort import *
from qsort import *

alist = [54,26,93,17,77,31,44,55,20]
print(alist)
bubble_sort(alist)
print(alist)

alist = [54,26,93,17,77,31,44,55,20]
print(alist)
selection_sort(alist)
print(alist)

alist = [54,26,93,17,77,31,44,55,20]
print(alist)
insertion_sort(alist)
print(alist)

alist = [54,26,93,17,77,31,44,55,20]
print(alist)
qsort(alist)
print(alist)
コード例 #18
0
    quick_sort(array, 0, len(array) - 1)
    print(array)

    ## Mergesort
    print('Mergesort:')
    array = generateArray(20)
    print(array)
    array = merge_sort(array)
    print(array)

    ## Selectionsort
    print('Selectionsort:')
    array = generateArray(20)
    print(array)
    selection_sort(array)
    print(array)

    ## Heapsort
    print('Selectionsort:')
    array = generateArray(20)
    print(array)
    heap_sort(array)
    print(array)

    ## Bubblesort
    print('Selectionsort:')
    array = generateArray(20)
    print(array)
    bubble_sort(array)
    print(array)
コード例 #19
0
ファイル: tests.py プロジェクト: JackMorris/Python-Utilities
 def test_single_input(self):
     data = rand_list(count=1)
     data_cp = data[:]
     bubble_sort(data)
     self.assertEqual(data, data_cp)
コード例 #20
0
    all_lists = {}
    all_lists['ALREADY_SORTED'] = [1, 2, 4, 4, 6, 6, 8, 9]
    all_lists['REVERSE_SORTED'] = [9, 8, 6, 6, 4, 4, 2, 1]
    all_lists['FEW_UNIQUE'] = [3, 6, 6, 4, 3, 3, 3, 6, 6, 4]
    all_lists['RANDOM_ORDER'] = [2, 6, 4, 8, 1, 4, 6, 9]
    all_lists['BIG_RANGE'] = [170, 45, 75, 90, 802, 2, 24, 66]
    all_lists['LONG_LIST'] = [
        2, 4, 3, 3, 6, 7, 876, 2, 4, 8, 214, 7, 2, 7, 4, 8, 2, 7, 4, 6, 83, 2,
        6, 7, 88, 75, 2, 3, 6, 789, 86, 3, 6, 6, 3, 575, 6, 87, 8, 574
    ]
    all_lists['ZEROES_IN_LIST'] = [
        9, 0, 3, 0, 54, 6, 1, 45, 1, 57, 1, 56, 8, 8, 3, 0
    ]
    all_lists['ALL_SAME_NUMBER'] = [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7]

    for list_name, number_list in all_lists.iteritems():
        print "\nUSING LIST \n\t%s\n\t%s" % (list_name, number_list)

        print "Insertion sorted output: \n\t", insertion_sort(number_list)
        print "Bubble sorted output: \n\t", bubble_sort(number_list)
        print "Selection sorted output: \n\t", selection_sort(number_list)
        print "Bucket sorted output (using insertion sort and %s buckets): \n\t" % (
            buckets), bucket_sort(number_list, 3)
        print "Counting sorted output: \n\t", counting_sort(number_list)
        print "Quick sorted output: \n\t", quicksort(number_list)
        print "Merge sorted output: \n\t", mergesort(number_list)
        print "Radix sorted output (using counting sort): \n\t", radix_sort(
            number_list)
        print "Heap sorted output: \n\t", heapsort(number_list)
コード例 #21
0
ファイル: tests.py プロジェクト: JackMorris/Python-Utilities
 def test_double_input(self):
     data = rand_list(count=2)
     bubble_sort(data)
     self.assertLessEqual(data[0], data[1])
コード例 #22
0
ファイル: run.py プロジェクト: jack-morrison/cs-review
#!/usr/bin/env python3
# run.py
# demonstrates a bubble sort using bubble_sort.py

from bubble_sort import *

myarray = [3, 8, 7, 1, 2, 4, 5, 6]
print("(UNSORTED) ", myarray)
print("\n ... performing bubble sort ... \n")
myarray = bubble_sort(myarray)
print("(SORTED)   ", myarray)
コード例 #23
0
ファイル: tests.py プロジェクト: JackMorris/Python-Utilities
 def test_array_input(self):
     data = rand_list()
     bubble_sort(data)
     for i in range(1, len(data)):
         self.assertLessEqual(data[i-1], data[i])
コード例 #24
0
ファイル: tests.py プロジェクト: JackMorris/Python-Utilities
 def test_all_zero_input(self):
     data = [0]*100
     bubble_sort(data)
     for i in range(0, 100):
         self.assertEqual(data[i], 0)
コード例 #25
0
ファイル: tests.py プロジェクト: JackMorris/Python-Utilities
 def test_all_same_input(self):
     single_val = rand_list(count=1)[0]
     data = [single_val]*100
     bubble_sort(data)
     for i in range(0, 100):
         self.assertEqual(single_val, data[i])
コード例 #26
0
 def test_sort(self):
     lst = [4,3,5,1,2]
     self.assertEqual(bubble_sort(lst), lst.sort())
コード例 #27
0
            n = int(input('\n\n Aperte 0 Para Voltar: '))

    elif n == 3:
        print("\n" * 130)
        print('*** Bubble Sort ***\n')
        try:
            nome_arq = input('Nome do arquivo a ser Aplicado Bubble Sort: ')
            nome_arq = nome_arq + '.txt'
            arquivo = open(nome_arq, 'r')
            lista = arquivo.readlines()
            x2 = 0
            while x2 != 9:
                for index in range(len(lista)):
                    lista[index] = lista[index].rstrip('\n')
                    lista[index] = int(lista[index])
                bubble_sort(lista)
                n_lista = lista
                print('*** Aplicado o Bubble Sort ***\n')
                print(lista)
                arquivo = open(nome_arq, "w")
                frases = list()
                for i in range(len(n_lista)):
                    frases.append(str(n_lista[i]))
                    frases.append('\n')
                arquivo.writelines(frases)
                arquivo.close()
                x2 = int(input('\n\n Aperte 9 Para Voltar: '))

        except FileNotFoundError:
            print('Arquivo não Foi encontrado ')
            print('Erros Provaveis: Nome errado ')