Пример #1
0
def test_quick_sort_strings():
    assert quick_sort([
        "C59OK", "5QV09", "TCNId", "ne58v", "ges28", "EzMbM", "qdPku", "ltinw",
        "Nt1Rj", "nSb8R"
    ]) == [
        "5QV09", "C59OK", "EzMbM", "Nt1Rj", "TCNId", "ges28", "ltinw", "nSb8R",
        "ne58v", "qdPku"
    ]
Пример #2
0
    def sort(self):
        self.draw_bars()
        self.display_sorttext()
        if self.algorithm == 'bubble':
            algorithms.bubble_sort(self)

        if self.algorithm == 'insertion':
            algorithms.insertion_sort(self)

        if self.algorithm == 'quick':
            algorithms.quick_sort(self, isfirst_call = True)
            if algorithms.stop_recursive_sort == False:
                self.issorted = True

        if self.algorithm == 'merge':
            algorithms.merge_sort(self, isfirst_call = True)
            if algorithms.stop_recursive_sort == False:
                self.issorted = True

        if self.algorithm == 'heap':
            algorithms.heap_sort(self)
            if algorithms.stop_recursive_sort == False:
                self.issorted = True
Пример #3
0
def startAlgorithm():
    global data
    tick = speedScale.get()
    if select_alg.get() == "Bubble sort":
        sorted_data = bubble_sort(data, draw_data, tick)
        print(sorted_data)
        canvas.delete("all")
        draw_data(sorted_data, ["white" for x in range(len(data))])

    elif select_alg.get() == "Insertion Sort":
        sorted_data = insertion_sort(data, draw_data, tick)
        print(sorted_data)
        canvas.delete("all")
        draw_data(sorted_data, ["white" for x in range(len(data))])

    elif select_alg.get() == "Selection Sort":
        sorted_data = selection_sort(data, draw_data, tick)
        print(sorted_data, )
        canvas.delete("all")
        draw_data(sorted_data, ["white" for x in range(len(data))])

    elif select_alg.get() == "Heap Sort":
        sorted_data = heap_sort(data, draw_data, tick)
        print(sorted_data, )
        canvas.delete("all")
        draw_data(sorted_data, ["white" for x in range(len(data))])

    elif select_alg.get() == "Quick Sort":
        sorted_data = quick_sort(data, draw_data, tick)
        print(sorted_data, )
        canvas.delete("all")
        draw_data(sorted_data, ["white" for x in range(len(data))])

    elif select_alg.get() == "Merge Sort":
        sorted_data = merge_sort(data, draw_data, tick)
        print(sorted_data, )
        canvas.delete("all")
        draw_data(sorted_data, ["white" for x in range(len(data))])
Пример #4
0
 def call_sort_method(self, random_list, max_vet, j):
     begin = time.time()
     quick_sort(random_list)
     end = time.time()
     print('quick sort with {} items vet[{}]: {} seconds'.format(max_vet, j + 1, end - begin))
     return end - begin
Пример #5
0
 def sortUsingQuickSort(self):
     algorithms.quick_sort(self.bookSortedCatalog)
Пример #6
0
 def test_quick_sort_with_one_item(self):
   iterable = [1]
   algorithms.quick_sort(iterable)
   expected = [1]
   self.assertEqual(iterable, expected)
Пример #7
0
def test_descending_array(descending_array):
    assert quick_sort(descending_array) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Пример #8
0
def test_identical_array(identical_array):
    assert quick_sort(identical_array) == [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
Пример #9
0
 def test_quick_sort_with_none_iterable_raises_type_error(self):
   iterable = None
   with self.assertRaises(TypeError):
     algorithms.quick_sort(iterable)
Пример #10
0
 def test_quick_sort_with_no_items(self):
   iterable = []
   algorithms.quick_sort(iterable)
   expected = []
   self.assertEqual(iterable, expected)
Пример #11
0
 def test_quick_sort_with_strings(self):
   iterable = ['a', 's', 'd', 'f']
   algorithms.quick_sort(iterable)
   expected = ['a', 'd', 'f', 's']
   self.assertEqual(iterable, expected)
Пример #12
0
 def test_quick_sort_with_descending_items(self):
   iterable = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
   algorithms.quick_sort(iterable)
   expected = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
   self.assertEqual(iterable, expected)
Пример #13
0
 def test_quick_sort_with_three_items_6(self):
   iterable = [3, 2, 1]
   algorithms.quick_sort(iterable)
   expected = [1, 2, 3]
   self.assertEqual(iterable, expected)
Пример #14
0
 def test_quick_sort_with_two_items_2(self):
   iterable = [2, 1]
   algorithms.quick_sort(iterable)
   expected = [1, 2]
   self.assertEqual(iterable, expected)
Пример #15
0

############################################

print("How many integers would you like sorted?: ")

array_length = inputNumber(1, 2**64)
array = random.sample(range(1, array_length), k=array_length - 1)

algorithm_data = {
    1: ("Selection Sort", selection_sort(array)),
    2: ("Bubble Sort", bubble_sort(array)),
    3: ("Insertion Sort", insertion_sort(array)),
    4: ("Shell Sort", shell_sort(array)),
    5: ("Merge Sort", merge_sort(array)),
    6: ("Quick Sort", quick_sort(array, 0,
                                 len(array) - 1)),
    7: ("Heap Sort", heap_sort(array))
}
print("Welcome to Urmzd's Sorting Algorithm Visualizer: ")
print("Select one of the following sorting algorithms: ")

for k, v in algorithm_data.items():
    print(f"Type {k} for {v[0]}")

algorithm_number = inputNumber(1, 7)

algorithm_name, algorithm = algorithm_data.get(algorithm_number)

counter = [0]

Пример #16
0
def menu_bookstore_system() :
    bookStore = BookStore.BookStore()
    option=""
    while option != '0':
        print("""
        1 Load book catalog into book Catalog
        2 BFS
        3 DFS
        0 Return to Main Menu
        """)
        '''
        4 Sort bookCatalog using Merge Sort
        5 Sort bookCatalog using Quick Sort
        6 Search by PreFix using Binary Search
        7 Quick Sort Catalog using LinkedList
        8 Merge Sort Catalog using LinkedList
        9 Remove a book by index from catalog
        10 Add a book by index to shopping cart
        11 Remove from the shopping cart
        12 Search book by infix
        13 Print Best Selling Book
        14 Reverse order of shopping cart
        15 Search books by title
        16 Search book by prefix
        17 Save to file using BF traversal
        18 in-order
        19 pre-order
        20 post-order
        21 Search Book by Prefix BestSellers
        r setRandomShoppingCart
        s setShoppingCart
        0 Return to main menu
        '''
        option=input() 
        if option=="r":
            bookStore.setRandomShoppingCart()
        elif option=="s":
            bookStore.setShoppingCart()
        elif option=="1":
            file_name = input("Introduce the name of the file: ")
            bookStore.loadCatalog(file_name)
        elif option =="2":
            index = int(input("Enter the index: "))
            k = int(input("Enter the degree: "))
            #print( bookStore.similarGraph.bfs2(index,k))
            traversal = bookStore.similarGraph.bfs2(index,k)
            for i in range(1, len(traversal)):
                        print(bookStore.bookCatalog.get(traversal[i]))
        elif option =="3":
            print(bookStore.similarGraph.dfs2(int(input("Enter the index: ")),int(input("Enter the index: ")))) 
        elif option =="4":
            print("loading...")
            start_time = time.time()
            algorithms.merge_sort(bookStore.sortedBookCatalog)
            elapsed_time = time.time() - start_time
            print(f"Sort completed in {elapsed_time} seconds")
        elif option =="5":
            print("loading...")
            start_time = time.time()
            algorithms.quick_sort(bookStore.sortedBookCatalog)
            elapsed_time = time.time() - start_time
            print(f"Sort completed in {elapsed_time} seconds")
        elif option =="6":
            bookStore.binarySearchbyTitle(input("Enter a prefix: "))
        elif option =="7":
            print("loading...")
            algorithms.quick_sort(bookStore.sortedBookCatalog)
            print("Sorted")
        elif option =="8":
            print("loading...")
            bookStore.mergeSortedBooksDLList()
            print("Sorted")

        elif option=="9":
            i = int(("Introduce the index to remove from catalog: "))
            bookStore.removeFromCatalog(i)
        elif option=="10":
            i = int(input("Introduce the index to add to shopping cart: "))
            bookStore.addBookByIndex(i)
        elif option=="11":
            bookStore.removeFromShoppingCart()
        elif option=="12":
            infix = input("Introduce the query to search: ")
            bookStore.searchBookByInfix(infix)
        elif option =="13":
            bookStore.bestSellingBook()
        elif option =="14":
            bookStore.reverse()
        elif option =="15":
            t = input("Introduce the query to search: ")
            bookStore.searchBookByTitle(t)
        elif option =="16":
            t = input("Introduce the query to search: ")
            bookStore.searchByPrefix(t)
        elif option =="17":
            print("laoding")
            bookStore.indexSortedTitle.bf_traverse()
            print("Saved to File")
        elif option =="18":
            print("laoding")
            bookStore.indexSortedTitle.in_order(bookStore.indexSortedTitle.r, [])
            print("Saved to File")
        elif option =="19":
            print("laoding")
            bookStore.indexSortedTitle.pre_order(bookStore.indexSortedTitle.r, [])
            print("Saved to File")
        elif option =="20":
            print("laoding")
            bookStore.indexSortedTitle.post_order(bookStore.indexSortedTitle.r, [])
            print("Saved to File")
        elif option =="21":
            bookStore.searchByPrefix(input("Introduce the query to search: "), input("Enter K Size"))
            print("loading...")
        ''' 
Пример #17
0
 def test_quick_sort(self):
     self.assertEqual(algorithms.quick_sort([8, 4, 4, 5]), [4, 4, 5, 8])
Пример #18
0
def test_empty_array(empty_array):
    assert quick_sort(empty_array) == []
Пример #19
0
def test_quick_sort_numbers():
    assert quick_sort(
        [3, 44, 38, 5, 47, 15, 36, 26, 27, 2, 46, 4, 19, 50,
         48]) == [2, 3, 4, 5, 15, 19, 26, 27, 36, 38, 44, 46, 47, 48, 50]
Пример #20
0
        print(f'List with {elements} elements:')

        if elements < THRESHOLD_ELEMENTS_SLOW_ALGORITHMS:
            bubble_sort(deepcopy(list_unsorted))
            print(f'Bubble sort: \t\t{bubble_sort.execution_time}')

            selection_sort(deepcopy(list_unsorted))
            print(f'Selection sort: \t{selection_sort.execution_time}')

            insertion_sort(deepcopy(list_unsorted))
            print(f'Insertion sort: \t{insertion_sort.execution_time}')
        else:
            print(
                'Bubble sort: \t\tSkipping algorithm because it takes a long time'
            )
            print(
                'Selection sort: \tSkipping algorithm because it takes a long time'
            )
            print(
                'Insertion sort: \tSkipping algorithm because it takes a long time'
            )

        quick_sort(deepcopy(list_unsorted), 0, len(list_unsorted) - 1)
        print(f'Quick sort: \t\t{quick_sort.execution_time}')

        merge_sort(deepcopy(list_unsorted))
        print(f'Merge sort: \t\t{merge_sort.execution_time}')

        heap_sort(deepcopy(list_unsorted))
        print(f'Heap sort: \t\t{heap_sort.execution_time}\n')
Пример #21
0
 def test_quick_sort_is_not_stable(self):
   iterable = [[1], [1], [1], [1]]
   ids = [id(item) for item in iterable]
   algorithms.quick_sort(iterable)
   expected = [id(item) for item in iterable]
   self.assertNotEqual(ids, expected)