School Commit Init
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
# Byte-compiled / optimized / DLL files
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
*$py.class
|
||||
|
||||
# C extensions
|
||||
*.so
|
||||
|
||||
# Distribution / packaging
|
||||
.Python
|
||||
build/
|
||||
develop-eggs/
|
||||
dist/
|
||||
downloads/
|
||||
eggs/
|
||||
.eggs/
|
||||
lib/
|
||||
lib64/
|
||||
parts/
|
||||
sdist/
|
||||
var/
|
||||
wheels/
|
||||
pip-wheel-metadata/
|
||||
share/python-wheels/
|
||||
*.egg-info/
|
||||
.installed.cfg
|
||||
*.egg
|
||||
MANIFEST
|
||||
|
||||
# PyInstaller
|
||||
# Usually these files are written by a python script from a template
|
||||
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
||||
*.manifest
|
||||
*.spec
|
||||
|
||||
# Installer logs
|
||||
pip-log.txt
|
||||
pip-delete-this-directory.txt
|
||||
|
||||
# Unit test / coverage reports
|
||||
htmlcov/
|
||||
.tox/
|
||||
.nox/
|
||||
.coverage
|
||||
.coverage.*
|
||||
.cache
|
||||
nosetests.xml
|
||||
coverage.xml
|
||||
*.cover
|
||||
*.py,cover
|
||||
.hypothesis/
|
||||
.pytest_cache/
|
||||
|
||||
# Translations
|
||||
*.mo
|
||||
*.pot
|
||||
|
||||
# Django stuff:
|
||||
*.log
|
||||
local_settings.py
|
||||
db.sqlite3
|
||||
db.sqlite3-journal
|
||||
|
||||
# Flask stuff:
|
||||
instance/
|
||||
.webassets-cache
|
||||
|
||||
# Scrapy stuff:
|
||||
.scrapy
|
||||
|
||||
# Sphinx documentation
|
||||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
target/
|
||||
|
||||
# Jupyter Notebook
|
||||
.ipynb_checkpoints
|
||||
|
||||
# IPython
|
||||
profile_default/
|
||||
ipython_config.py
|
||||
|
||||
# pyenv
|
||||
.python-version
|
||||
|
||||
# pipenv
|
||||
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
||||
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
||||
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
||||
# install all needed dependencies.
|
||||
#Pipfile.lock
|
||||
|
||||
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
|
||||
__pypackages__/
|
||||
|
||||
# Celery stuff
|
||||
celerybeat-schedule
|
||||
celerybeat.pid
|
||||
|
||||
# SageMath parsed files
|
||||
*.sage.py
|
||||
|
||||
# Environments
|
||||
.env
|
||||
.venv
|
||||
env/
|
||||
venv/
|
||||
ENV/
|
||||
env.bak/
|
||||
venv.bak/
|
||||
|
||||
# Spyder project settings
|
||||
.spyderproject
|
||||
.spyproject
|
||||
|
||||
# Rope project settings
|
||||
.ropeproject
|
||||
|
||||
# mkdocs documentation
|
||||
/site
|
||||
|
||||
# mypy
|
||||
.mypy_cache/
|
||||
.dmypy.json
|
||||
dmypy.json
|
||||
|
||||
# Pyre type checker
|
||||
.pyre/
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
# Computational Complexity
|
||||
|
||||
## 1. Bubble Sort
|
||||
|
||||
### a. Best Case
|
||||
|
||||
The best case scenerio for the Bubble Sort is $O(n)$. This happens when the inputed list is already ordered.
|
||||
|
||||
ex: list = [1,2,6,8,13,34,75,88,91,100]
|
||||
|
||||
In this case, bubble sort will go through the array once, comparing each neighbor pair, but not changing any of the values.
|
||||
|
||||
### b. Worst Case
|
||||
|
||||
The worst case scenerio for the Bubble Sort is $O(n^2)$.
|
||||
This happens when the inputed list is already ordered, but in the reverse.
|
||||
|
||||
ex: list = [100,91,88,,75,34,13,8,6,2,1]
|
||||
|
||||
In this case, bubble sort will have to move every number from their position to last position - their curent position,going through the list $\frac{n*(n-1)}{2}$ times in the procces.
|
||||
|
||||
## 2. Strand Sort
|
||||
|
||||
### a. Best Case
|
||||
|
||||
The best case scenerio for the Strand Sort is $O(n)$. This happens when the inputed list is already ordered.
|
||||
|
||||
ex: list = [1,2,6,8,13,34,75,88,91,100]
|
||||
|
||||
This way, every element of the initial list gets transfered to the auxiliary list in the first passing, afterwards merging with the final list.
|
||||
|
||||
### b. Worst Case
|
||||
|
||||
The worst case scenerio for the Strand Sort is $O(n^2)$.
|
||||
This happens when the inputed list is already ordered, but in the reverse.
|
||||
|
||||
ex: list = [100,91,88,,75,34,13,8,6,2,1]
|
||||
|
||||
This way, only one element gets transfered to the auxiliary list each passing of the initial list. Strand sort will have to pass through the initial list $\frac{n*(n-1)}{2}$ times.
|
||||
@@ -0,0 +1,12 @@
|
||||
# :computer: Assignment 03 - Algorithm Complexity
|
||||
|
||||
## Requirements
|
||||
- Calculate the complexity of the sorting algorithms you've implemented for the previous assignment. You may use additional resources for help, but you have to understand and be able to explain the complexity for both the **best case** as well as **worst case**. You can show this on screen or written on paper.
|
||||
- Update the program created for the previous assignment by adding the following three additional menu entries -- one for ***best case***, one for ***average case*** and the last one for ***worst case***. When the user selects one of these, the program will time and illustrate the runtime of the implemented algorithms by sorting 5 data structures, each having twice the number of elements of the previous one. The elements of the data structure must be in accordance with the current case. See the example below.
|
||||
|
||||
## Example
|
||||
- Let's say that as part of **A2** you've implemented BubbleSort and ShellSort. Let's say the user wishes to see the behaviour of these algorithms in the worst case. Your program will generate five lists (e.g., lenghts `500`, `1000`, `2000`, `4000` and `8000` elements) in which elements are in worst case configuration (for bubble sort, this happens if the list is already sorted, but in reverse). Then, you will time how long it takes to sort each list using a module such as [timeit](https://docs.python.org/3/library/timeit.html), and display the results on the console.
|
||||
- In case the algorithm takes too long/little, you can adjust the list lenghts (e.g., permutation sort)
|
||||
- Make sure that all calls to the sorting algorithms use the same input lists.
|
||||
- Make sure the sorting algorithms do not perform additional work (e.g., displaying on the console, as required for **A2**)
|
||||
- Display the results (list length, sort duration) in a way that allows users to see the progression of the runtime.
|
||||
@@ -0,0 +1,179 @@
|
||||
import os
|
||||
import random
|
||||
import timeit
|
||||
import prettytable
|
||||
|
||||
|
||||
def GUI():
|
||||
current_list=[]
|
||||
while True:
|
||||
print("1 - Generate a List")
|
||||
print("2 - Show Current List")
|
||||
print("3 - Bubble Sort")
|
||||
print("4 - Strand Sort")
|
||||
print("5 - Best Case")
|
||||
print("6 - Average Case")
|
||||
print("7 - Worst Case")
|
||||
print("8 - Clear screen")
|
||||
print("0 - Exit")
|
||||
value = input("> ")
|
||||
match value:
|
||||
case "1":
|
||||
n=int(input("Select lenght of the list: "))
|
||||
current_list = generate_list(n)
|
||||
print("Generated list: {}\n".format(current_list))
|
||||
case "2":
|
||||
show_list(current_list)
|
||||
case "3":
|
||||
steps=int(input("Please select the number of steps: "))
|
||||
current_list=bubble_sort(current_list,steps)
|
||||
case "4":
|
||||
steps=int(input("Please select the number of steps: "))
|
||||
current_list=strand_sort(current_list,steps)
|
||||
case "5":
|
||||
print("Please choose the sorting algorithm: ")
|
||||
print("1 - Bubble Sort")
|
||||
print("2 - Strand Sort")
|
||||
s=input(">")
|
||||
match s:
|
||||
case "1":
|
||||
best_case(bubble_sort)
|
||||
case "2":
|
||||
best_case(strand_sort)
|
||||
case _:
|
||||
print("Invalid command")
|
||||
case "6":
|
||||
print("Please choose the sorting algorithm: ")
|
||||
print("1 - Bubble Sort")
|
||||
print("2 - Strand Sort")
|
||||
s=input("> ")
|
||||
match s:
|
||||
case "1":
|
||||
average_case(bubble_sort)
|
||||
case "2":
|
||||
average_case(strand_sort)
|
||||
case _:
|
||||
print("Invalid command")
|
||||
case "7":
|
||||
print("Please choose the sorting algorithm: ")
|
||||
print("1 - Bubble Sort")
|
||||
print("2 - Strand Sort")
|
||||
s=input("> ")
|
||||
match s:
|
||||
case "1":
|
||||
worst_case(bubble_sort)
|
||||
case "2":
|
||||
worst_case(strand_sort)
|
||||
case _:
|
||||
print("Invalid command")
|
||||
case "8":
|
||||
cls = lambda: os.system('cls')
|
||||
cls()
|
||||
case "0":
|
||||
exit()
|
||||
case _:
|
||||
print("Invalid command")
|
||||
|
||||
def bubble_sort(l,step):
|
||||
if step!=0:
|
||||
print("Starting list: {}".format(l))
|
||||
current_step=0
|
||||
for i in range(len(l)-1):
|
||||
sorted=True
|
||||
for j in range(len(l)-1-i):
|
||||
if l[j]>l[j+1]:
|
||||
l[j],l[j+1]=l[j+1],l[j]
|
||||
sorted=False
|
||||
current_step+=1
|
||||
if step!=0 and current_step % step == 0:
|
||||
print("List after {} steps: {}".format(current_step,l))
|
||||
if sorted:
|
||||
break
|
||||
if step!=0:
|
||||
print("Sorted list: {}\n".format(l))
|
||||
return l
|
||||
|
||||
def strand_sort(l,step):
|
||||
if step!=0:
|
||||
print("Starting list: {}\n".format(l))
|
||||
current_step=0
|
||||
aux_list=[]
|
||||
final_list=[]
|
||||
while l:
|
||||
aux_list=[]
|
||||
aux_list.append(l.pop(0))
|
||||
current_step+=1
|
||||
strand_sort_message(current_step,l,aux_list,final_list,step)
|
||||
for i in l.copy():
|
||||
if aux_list[-1] <= i:
|
||||
aux_list.append(l.pop(l.index(i)))
|
||||
current_step+=1
|
||||
strand_sort_message(current_step,l,aux_list,final_list,step)
|
||||
if final_list:
|
||||
i=0
|
||||
while aux_list:
|
||||
if aux_list[-1]>final_list[i]:
|
||||
i+=1
|
||||
else:
|
||||
final_list.insert(i,aux_list[-1])
|
||||
aux_list.pop()
|
||||
current_step+=1
|
||||
strand_sort_message(current_step,l,aux_list,final_list,step)
|
||||
i=0
|
||||
else:
|
||||
final_list=aux_list.copy()
|
||||
aux_list.clear()
|
||||
current_step+=1
|
||||
strand_sort_message(current_step,l,aux_list,final_list,step)
|
||||
if step!=0:
|
||||
print("Sorted list: {}\n".format(final_list))
|
||||
return final_list
|
||||
|
||||
def strand_sort_message(current_step,l,aux_list,final_list,step):
|
||||
if step!=0 and current_step%step==0:
|
||||
print("Lists after {} steps:".format(current_step))
|
||||
print("Starting list: {}".format(l))
|
||||
print("Working list: {}".format(aux_list))
|
||||
print("Final list: {}\n".format(final_list))
|
||||
|
||||
def generate_list(n):
|
||||
return [random.randint(0,100) for _ in range(n)]
|
||||
|
||||
def show_list(l):
|
||||
print("Current list: {}\n".format(l))
|
||||
|
||||
def best_case(sort):
|
||||
n=500
|
||||
times=[]
|
||||
while n<=8000:
|
||||
time=timeit.timeit(lambda: sort(list(range(1,n+1)),0),number=1)
|
||||
times.append((n,time))
|
||||
n*=2
|
||||
display_times(times)
|
||||
|
||||
def worst_case(sort):
|
||||
n=500
|
||||
times=[]
|
||||
while n<=8000:
|
||||
time=timeit.timeit(lambda: sort(list(range(n,0,-1)),0),number=1)
|
||||
times.append((n,time))
|
||||
n*=2
|
||||
display_times(times)
|
||||
|
||||
def average_case(sort):
|
||||
n=500
|
||||
times=[]
|
||||
while n<=8000:
|
||||
time=timeit.timeit(lambda: sort(generate_list(n),0),number=1)
|
||||
times.append((n,time))
|
||||
n*=2
|
||||
display_times(times)
|
||||
|
||||
def display_times(times):
|
||||
x=prettytable.PrettyTable()
|
||||
x.field_names=["List lenght","Time"]
|
||||
x.add_rows(times)
|
||||
print(x)
|
||||
|
||||
if __name__=="__main__":
|
||||
GUI()
|
||||
Reference in New Issue
Block a user