diff --git a/README.md b/README.md index 268f597..a43193d 100644 --- a/README.md +++ b/README.md @@ -1 +1,40 @@ -# algorithm-lab \ No newline at end of file + +# algorithm-lab +1. clone my repository: +(if you are working with a console) +``` +https://github.com/Maria-Sparrow/algorithm-lab.git +``` +* or download and extract it; + +2. open command prompt and switch the directory to **/algorithm-lab**: +``` +cd algorithm-lab +``` +* if you decided to download and extract it to, let us say, D:/projects, you would do it like that: +``` +D: +``` +``` +cd projects/algorithm-lab +``` + + +3. if you do not have Phyton 3+ installed, you must use the console: +``` +python main.py +``` + +*** + +this program shows you the results of insertion and merge sorts for each algorithm, it also shows some additional information, such as: +* time, +* swap counter, +* comparison counter. + +counters represent the numbers of corresponding operations that occurred during the sorting process. + +the list of objects is taken from *basin.csv* file. Each row there represents a new object as a sequence of its properties' values separated by commas. The order is: +``` +address, volume_of_water, max_number_of_visitors +``` diff --git a/basin.csv b/basin.csv new file mode 100644 index 0000000..8a598a1 --- /dev/null +++ b/basin.csv @@ -0,0 +1,7 @@ +'Horodotska 60',100,150 +'Horodotska 12',110,151 +'Horodotska 3466',140,155 +'Horodotska 42',120,157 +'Horodotska 124',170,153 +'Horodotska 1',140,159 +'Horodotska 46',167,156 diff --git a/counter.py b/counter.py new file mode 100644 index 0000000..a127387 --- /dev/null +++ b/counter.py @@ -0,0 +1,4 @@ +merge_sort_swap_counter = 0 +merge_sort_compare_counter = 0 +select_swap_counter = 0 +select_compare_counter = 0 \ No newline at end of file diff --git a/counter_meth.py b/counter_meth.py new file mode 100644 index 0000000..b364a71 --- /dev/null +++ b/counter_meth.py @@ -0,0 +1,13 @@ +from datetime import timedelta +import counter + + +def print_counter_result(elapsed_time: timedelta, sort_type: str): + if sort_type == 'SELECTION': + print('counter: ' + str(counter.select_compare_counter)) + print('swap: ' + str(counter.select_swap_counter)) + elif sort_type == 'MERGE': + print('counter: ' + str(counter.merge_sort_compare_counter)) + print('swap: ' + str(counter.merge_sort_swap_counter)) + print('time: ' + str(elapsed_time)) + diff --git a/main.py b/main.py new file mode 100644 index 0000000..4223139 --- /dev/null +++ b/main.py @@ -0,0 +1,41 @@ +from timeit import default_timer as timer +from datetime import timedelta +import sys +import copy +import counter_meth +from sortPool import * + +file = "basin.csv" +if len(sys.argv) != 0: + file = sys.argv[0] + + list_of_basins = get_objects_from_csv(file) + list_for_selection = copy.deepcopy(list_of_basins) + print('-------------------------------------------') + start = timer() + print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + selection_sort_by_volume(list_for_selection) + elapsed_time = timedelta(((timer() - start) * 1000)) + counter_meth.print_counter_result(elapsed_time, 'SELECTION') + + + for basin in list_for_selection: + print(str(basin)) + print('--------------------------------------------') + + start = timer() + list_of_basins = merge_sort_by_visitors(list_of_basins) + elapsed_time = timedelta(((timer() - start) * 1000)) + counter_meth.print_counter_result(elapsed_time, 'MERGE') + print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + for basin in list_of_basins: + print(str(basin)) + + + + + + + + + diff --git a/sortPool.py b/sortPool.py new file mode 100644 index 0000000..d649d84 --- /dev/null +++ b/sortPool.py @@ -0,0 +1,70 @@ +import csv +import swimmingPool +import counter + +file = "basin.csv" + + +def get_objects_from_csv(input_file): + csv_file = open(file, newline='') + reader = csv.reader(csv_file) + list_of_pool = [] + for row in reader: + print(row) + new_pool = swimmingPool.SwimmingPool(address=row[0], volume_of_water=int(row[1]), + max_number_of_visitors=int(row[2])) + list_of_pool.append(new_pool) + return list_of_pool + + +def selection_sort_by_volume(basin_list): + length = len(basin_list) + + for item in range(length): + minimum = item #мінім елем + + for iteration in range(item + 1, length): + counter.select_compare_counter += 1 + if basin_list[iteration].volume_of_water > basin_list[minimum].volume_of_water: + minimum = iteration + counter.select_swap_counter += 1 + (basin_list[item], basin_list[minimum]) = (basin_list[minimum], basin_list[item]) + + +def merge_sort_by_visitors(basin_list, ascending=True): + result = [] + if len(basin_list) == 1: + return basin_list + middle = len(basin_list) // 2 + + visitors_list1 = merge_sort_by_visitors(basin_list[:middle]) + + visitors_list2 = merge_sort_by_visitors(basin_list[middle:]) + + first_visitor = last_visitor = 0 + + counter.merge_sort_compare_counter += 1 + while first_visitor < len(visitors_list1) and last_visitor < len(visitors_list2): + + counter.merge_sort_compare_counter += 1 + + if visitors_list1[first_visitor].max_number_of_visitors < visitors_list2[last_visitor].max_number_of_visitors: + counter.merge_sort_swap_counter += 1 + result.append(visitors_list2[last_visitor]) + last_visitor = last_visitor + 1 + + else: + counter.merge_sort_swap_counter += 1 + result.append(visitors_list1[first_visitor]) + first_visitor = first_visitor + 1 + + counter.merge_sort_compare_counter += 1 + + result = result + visitors_list1[first_visitor:] + + result = result + visitors_list2[last_visitor:] + if ascending == True: + return result + else: + result.reverse() + return result \ No newline at end of file diff --git a/swimmingPool.py b/swimmingPool.py new file mode 100644 index 0000000..198a6cd --- /dev/null +++ b/swimmingPool.py @@ -0,0 +1,15 @@ +class SwimmingPool(): + def __init__(self, address, volume_of_water, max_number_of_visitors): + self.address = address + self.volume_of_water = volume_of_water + self.max_number_of_visitors = max_number_of_visitors + + def __str__(self): + return "address " + self.address + ", volume_of_water " + str(self.volume_of_water) + ", visitors " + str(self.max_number_of_visitors) + + @staticmethod + def compare(small, deep, tag="volume_of_water", greater_then=True): + if tag =="volume_of_water": + return small.volume_of_water > deep.volume_of_water + else: + return small.max_number_of_visitors > deep.max_number_of_visitors \ No newline at end of file