-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
76 lines (64 loc) · 2.59 KB
/
__main__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
# coding: utf-8
from __future__ import print_function
import os
import sys
import glob
import random
import argparse
import importlib
from tabulate import tabulate
def sort_algos(sample, sample_size, no_of_times, sample_range):
pretty_print = lambda char, count: (print("{}".format(char*count)))
current_dir = os.path.dirname(os.path.realpath(__file__))
sorting_algos = glob.glob("{}/*_sort.py".format(current_dir))
sorting_algos = [x.split('/')[-1][:-3] for x in sorting_algos]
time_taken = [['Algorithm Name', 'Time Taken (Less Is Better!!)'], ]
for algo in sorting_algos:
module = importlib.import_module(algo)
time_taken.append([algo.title().replace('_', ' '),
"{0:.20f} sec".format(getattr(module, algo)(array=sample[:],
repeat=no_of_times).time)])
pretty_print('|', 50)
print(
"Sorting Algorithms Ran!\nArray Details And Algorithms Summary As Follow")
pretty_print('|', 50)
pretty_print('-', 50)
print("Length Of Array: {}".format(sample_size))
print("Range Of Numbers In Array: 0 to {}".format(sample_range-1))
print("Number Of Times Array Were Sorted: {}".format(no_of_times))
pretty_print('-', 50)
print(tabulate(sorted(time_taken, key=lambda x: x[1], reverse=True),
headers='firstrow', tablefmt="fancy_grid"))
def run():
"""
Main Function to execute sorting algorithms
"""
parser = argparse.ArgumentParser()
parser.add_argument("-s", "--size",
type=int,
help="Size of array to be sorted",
default=1000)
parser.add_argument("-l", "--loop",
type=int,
help="Number of times array sorting should be repeated",
default=1)
parser.add_argument("-r", "--range",
type=int,
help="Max range of number that should be present in array",
default=None)
args = parser.parse_args()
size = args.size
repeat = args.loop
max_range = args.range if args.range else size
# Generate sample which will be sorted by all algorithms
sample = []
try:
sample = random.sample(range(max_range), size)
except ValueError:
print("Provided values range has to be greater than sample size")
return None
# Run all sorting algorithms
sort_algos(sample, size, no_of_times=repeat, sample_range=max_range)
if __name__ == "__main__":
run()