def testquicksort(self):
     items = [random.randint(-100,100) for c in range(10000)]
     print("before sort ",items)
     QuickSort.sort(items)
     print("after sort ", items)
     for i in range(len(items)-1):
         if(items[i] > items[i+1]):
             self.fail("Quick sort failed!")
 def test_quick_sort(self) -> None:
     elements: List[int] = []
     #random.seed(a=1)
     elements = [random.randrange(0, 101) for _ in range(100)]
     #elements = [75, 10, 92, 92, 35]
     print('Elements are :{}'.format(elements))
     qs = QuickSort()
     sorted_elements = qs.sort(elements)
     print('Elements after sorting are : {}'.format(elements))
     elements.sort()
     self.assertEqual(elements, sorted_elements)
Beispiel #3
0
def run_quick_sort():
    ll = list(np.random.randint(
        -10, 10,
        15))  # [7, 6, 5, 4, 3, 2, 1] # np.random.randint(-10, 10, 10)#
    print(ll)
    qs = QuickSort(ll)
    res = qs.sort()
    print("QuickSort")
    print()
    print(f"before: {ll}")
    print(f"after: {res}")
    print()
Beispiel #4
0
 def do_sort_user(self, key=None):
     """sort user table with desired key"""
     if hasattr(self, 'user_data'):
         if key is None:
             key = input("Enter a field through which you want to sort :")
         else:
             key = key
         sort_obj = QuickSort()
         result_set = sort_obj.resultset(self.user_data)
         sorted_data = sort_obj.sort(result_set, key=key)
         self.__class__.print_func(self, sorted_data)
         print(f"Sorted using {key}")
     else:
         self.do_all_users(self)
         self.do_sort_user(key=key)
Beispiel #5
0
 def do_search_user(self, item=None):
     """Search user profile."""
     if not item:
         key = input("Provide the key to search item :")
         item = input(f"Provide {key} of user to be searched :")
         try:
             item = int(item)
         except:
             item = item
     else:
         key = 'id'
         item = int(item)
     self.do_all_users(self)
     sort_obj = QuickSort()
     result_set = sort_obj.resultset(self.user_data)
     sorted_data = sort_obj.sort(result_set, key=key)
     search_obj = BinarySearch()
     data = search_obj.search(sorted_data, item, key=key)
     print(data)
Beispiel #6
0
def check_sort(original):
    numbers = original[:]

    qs = QuickSort(numbers)
    output = qs.sort()

    print(output)

    if is_sorted(output):
        print("** SUCCESS! **")
    else:
        print("Uh oh - not in order.")

    if contain_same_ints(original, numbers):
        print("** Contain the same elements! **")
    else:
        print("Uh oh - something is missing.")

    print("---")
Beispiel #7
0
def main():
    data = []
    random.seed(time.time())
    data = [_ for _ in range(0, 20)]
    random.shuffle(data)
    # data = [40] * 40
    # data = [_ for _ in reversed(range(40))]

    print('source: {0}'.format(data))
    start = time.time()
    qs = QuickSort(debug=True, save_fig=True)
    swap_times, fig_path = qs.sort(data)
    save_gif(fig_path, 'quick_sort.gif')
    stop = time.time()
    print('result: {0}\n'.format(data))

    print('----------------------------------')
    print('swap times: {0}'.format(swap_times))
    print('spend time: {0}s'.format(stop - start))
    print('image path: {0}'.format(fig_path))
    print('----------------------------------')
Beispiel #8
0
def main():
    original = [325432, 989, 547510, 3, -93, 189019, 5042, 123,
                597, 42, 7506, 184, 184, 2409, 45, 824,
                4, -2650, 9, 662, 3928, -170, 45358, 395,
                842, 7697, 110, 14, 99, 221]

    numbers = original[:]

    qs = QuickSort(numbers)
    output = qs.sort()

    if is_sorted(output):
        print("** SUCCESS! **")
    else:
        print("Uh oh - not in order.")

    if contain_same_ints(original, numbers):
        print("** Contain the same elements! **")
    else:
        print("Uh oh - something is missing.")

    print(output)
        # repetition for different orders of array size
        for i in range(1,n):    
                
                l = 10**i                                               # order of array size
                arr = [int(random.random()*100) for j in range(l)]      # create an array of random numbers with incremental order
                alist = [0]*len(arr)                                    # initialize an empty array of same size
                times[i-1][0] = l                                       # add the order as the first column in the matrix

                # a loop to go through all the sorting algorithms
                for g in range(1, 6):                                   
                        start = time.clock()                            # get a CPU clock tick
                        if g == 1:
                                temp = SelectionSort.sort(dup(alist, arr))
                        elif g == 2:
                                temp = InsertionSort.sort(dup(alist, arr))
                        elif g == 3:
                                temp = MergeSort.sort(dup(alist, arr))
                        elif g == 4:
                                temp = QuickSort.sort(dup(alist, arr))
                        elif g == 5:
                                temp = CountSort.sort(dup(alist, arr))
                        end = time.clock() - start                       # get a CPU clock tick and estimate algorithm r
                        setin(i, end, g)

endit = time.clock() - begin                                             # estimate overal calculation time
print ('Total time Elapsed:', endit, 'seconds.') 

# show the benchmark matrix
for i in range(0, n-1):
        print (times[i])
Beispiel #10
0
 def test(self, args):
     QuickSort.sort(args)
     assert QuickSort.is_sorted(args)
     QuickSort.show(args)
Beispiel #11
0
 def quick_sort(self, nums):
     instance = QuickSort(nums)
     instance.sort()
Beispiel #12
0
 def quick_sort(array):
     start = timeit.default_timer()
     copied_array = copy.copy(array)
     QuickSort.sort(array=copied_array)
     end = timeit.default_timer()
     return format(end - start, '.5f')