From 037615c08159fbaab8b592fcaee7fbd10fa3cc31 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 13 Oct 2020 09:41:57 +0300 Subject: [PATCH 01/10] csv file with data lists --- basin.csv | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 basin.csv 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 From 75ff5acc165dd2545db7da3a3febff8a30f7ead8 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 13 Oct 2020 09:45:40 +0300 Subject: [PATCH 02/10] for count/swap --- counter.py | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 counter.py 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 From aa96ce62b1ef743ca54f2d6feb72ccb7564b3f71 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 13 Oct 2020 09:46:54 +0300 Subject: [PATCH 03/10] method for count/swap --- counter_meth.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 counter_meth.py 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)) + From ce7f43fceabf8302a1f17705bee0e0f646ec02a6 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 13 Oct 2020 09:48:09 +0300 Subject: [PATCH 04/10] main method --- main.py | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 main.py diff --git a/main.py b/main.py new file mode 100644 index 0000000..962d360 --- /dev/null +++ b/main.py @@ -0,0 +1,42 @@ +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] + +if __name__ == '__main__': + list_of_basins = get_objects_from_csv(file) + list_for_selection = copy.deepcopy(list_of_basins) + print('-------------------------------------------') + start = timer() + elapsed_time = timedelta(((timer() - start) * 1000)) + counter_meth.print_counter_result(elapsed_time, 'SELECTION') + print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + selection_sort_by_volume(list_for_selection) + + + for basin in list_for_selection: + print(str(basin)) + print('--------------------------------------------') + + start = timer() + elapsed_time = timedelta(((timer() - start) * 1000)) + counter_meth.print_counter_result(elapsed_time, 'MERGE') + elapsed_time = timedelta(((timer() - start) * 1000)) + print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') + for basin in list_of_basins: + print(str(basin)) + + + + + + + + + From 3504b1ab4924c535e2769a49369bebf75f730681 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 13 Oct 2020 09:49:23 +0300 Subject: [PATCH 05/10] sort methods for select and merge --- sortPool.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 sortPool.py diff --git a/sortPool.py b/sortPool.py new file mode 100644 index 0000000..c8dd292 --- /dev/null +++ b/sortPool.py @@ -0,0 +1,59 @@ +import csv +import swimmingPool + +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): + if basin_list[iteration].volume_of_water > basin_list[minimum].volume_of_water: + minimum = iteration + + (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 + while first_visitor < len(visitors_list1) and last_visitor < len(visitors_list2): + if visitors_list1[first_visitor].max_number_of_visitors < visitors_list2[last_visitor].max_number_of_visitors: + result.append(visitors_list2[last_visitor]) + last_visitor = last_visitor + 1 + + else: + result.append(visitors_list1[first_visitor]) + first_visitor = first_visitor + 1 + + result = result + visitors_list1[first_visitor:] + + result = result + visitors_list2[last_visitor:] + if ascending == True: + return result + else: + result.reverse() + return result From 73331566e155503045aa916eaaedcc8b3dc21ff7 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 13 Oct 2020 09:50:42 +0300 Subject: [PATCH 06/10] class with constructor --- swimmingPool.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 swimmingPool.py 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 From 0cdc551616862d3b9f67aa457ebcfc713a17fdd4 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 13 Oct 2020 11:57:12 +0300 Subject: [PATCH 07/10] added main file with smth change --- main.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index 962d360..4223139 100644 --- a/main.py +++ b/main.py @@ -9,15 +9,14 @@ if len(sys.argv) != 0: file = sys.argv[0] -if __name__ == '__main__': list_of_basins = get_objects_from_csv(file) list_for_selection = copy.deepcopy(list_of_basins) print('-------------------------------------------') start = timer() - elapsed_time = timedelta(((timer() - start) * 1000)) - counter_meth.print_counter_result(elapsed_time, 'SELECTION') 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: @@ -25,9 +24,9 @@ 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') - elapsed_time = timedelta(((timer() - start) * 1000)) print('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~') for basin in list_of_basins: print(str(basin)) From 4d28bd4535c59ae55fa4ff2f21e5d10c5b025ca6 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 13 Oct 2020 11:58:03 +0300 Subject: [PATCH 08/10] added sort file for merge and select --- sortPool.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/sortPool.py b/sortPool.py index c8dd292..d649d84 100644 --- a/sortPool.py +++ b/sortPool.py @@ -1,5 +1,6 @@ import csv import swimmingPool +import counter file = "basin.csv" @@ -23,9 +24,10 @@ def selection_sort_by_volume(basin_list): 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]) @@ -40,15 +42,24 @@ def merge_sort_by_visitors(basin_list, ascending=True): 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:] @@ -56,4 +67,4 @@ def merge_sort_by_visitors(basin_list, ascending=True): return result else: result.reverse() - return result + return result \ No newline at end of file From 77b157fa44cd7ddc13008fc3efebdaa492aaee3e Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 13 Oct 2020 13:44:36 +0300 Subject: [PATCH 09/10] Update README.md --- README.md | 41 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 268f597..5df341f 100644 --- a/README.md +++ b/README.md @@ -1 +1,40 @@ -# algorithm-lab \ No newline at end of file +# algorithm-lab +# 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 +``` From d9aeb941d7558e7197827400d64f926827262385 Mon Sep 17 00:00:00 2001 From: Maria-Sparrow <53764665+Maria-Sparrow@users.noreply.github.com> Date: Tue, 13 Oct 2020 14:13:47 +0300 Subject: [PATCH 10/10] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 5df341f..a43193d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# algorithm-lab + # algorithm-lab 1. clone my repository: (if you are working with a console)