GA2 done. Selection sort added
This commit is contained in:
parent
13030cc6bc
commit
8301104864
5 changed files with 138 additions and 38 deletions
BIN
GA2/ga2.pdf
BIN
GA2/ga2.pdf
Binary file not shown.
121
GA2/ga2.tex
121
GA2/ga2.tex
|
@ -32,14 +32,131 @@
|
||||||
\lstlistoflistings
|
\lstlistoflistings
|
||||||
\newpage
|
\newpage
|
||||||
|
|
||||||
|
\section{Exercise 1}
|
||||||
|
|
||||||
|
\subsection{Mergesort}
|
||||||
|
|
||||||
|
\begin{verbatim}
|
||||||
|
[5, 6, 12, 8, 4, 10, 3, 12, 11, 1]
|
||||||
|
[5, 6, 12, 8, 4] [10, 3, 12, 11, 1]
|
||||||
|
[5, 6, 12, 8, 4]
|
||||||
|
[5, 6], [12, 8, 4]
|
||||||
|
[5, 6]
|
||||||
|
[5], [6]
|
||||||
|
[5]
|
||||||
|
[6]
|
||||||
|
[5, 6]
|
||||||
|
[12, 8, 4]
|
||||||
|
[12] [8, 4]
|
||||||
|
[12]
|
||||||
|
[8, 4]
|
||||||
|
[8], [4]
|
||||||
|
[8]
|
||||||
|
[4]
|
||||||
|
[4, 8]
|
||||||
|
[4, 8, 12]
|
||||||
|
[4, 5, 6, 8, 12]
|
||||||
|
[10, 3, 12, 11, 1]
|
||||||
|
[10, 3], [12, 11, 1]
|
||||||
|
[10], [3]
|
||||||
|
[10]
|
||||||
|
[3]
|
||||||
|
[3, 10]
|
||||||
|
[12, 11, 1]
|
||||||
|
[12], [11, 1]
|
||||||
|
[12]
|
||||||
|
[11, 1]
|
||||||
|
[11], [1]
|
||||||
|
[11]
|
||||||
|
[1]
|
||||||
|
[1, 11]
|
||||||
|
[1, 11, 12]
|
||||||
|
[1, 3, 10, 11, 12]
|
||||||
|
[1, 3, 4, 5, 6, 8, 10, 11, 12, 12]
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
|
\subsection{Selection sort}
|
||||||
|
\begin{verbatim}
|
||||||
|
[4, 6, 12, 8, 5, 10, 3, 12, 11, 1]
|
||||||
|
[3, 6, 12, 8, 5, 10, 4, 12, 11, 1]
|
||||||
|
[1, 6, 12, 8, 5, 10, 4, 12, 11, 3]
|
||||||
|
[1, 5, 12, 8, 6, 10, 4, 12, 11, 3]
|
||||||
|
[1, 4, 12, 8, 6, 10, 5, 12, 11, 3]
|
||||||
|
[1, 3, 12, 8, 6, 10, 5, 12, 11, 4]
|
||||||
|
[1, 3, 8, 12, 6, 10, 5, 12, 11, 4]
|
||||||
|
[1, 3, 6, 12, 8, 10, 5, 12, 11, 4]
|
||||||
|
[1, 3, 5, 12, 8, 10, 6, 12, 11, 4]
|
||||||
|
[1, 3, 4, 12, 8, 10, 6, 12, 11, 5]
|
||||||
|
[1, 3, 4, 8, 12, 10, 6, 12, 11, 5]
|
||||||
|
[1, 3, 4, 6, 12, 10, 8, 12, 11, 5]
|
||||||
|
[1, 3, 4, 5, 12, 10, 8, 12, 11, 6]
|
||||||
|
[1, 3, 4, 5, 10, 12, 8, 12, 11, 6]
|
||||||
|
[1, 3, 4, 5, 8, 12, 10, 12, 11, 6]
|
||||||
|
[1, 3, 4, 5, 6, 12, 10, 12, 11, 8]
|
||||||
|
[1, 3, 4, 5, 6, 10, 12, 12, 11, 8]
|
||||||
|
[1, 3, 4, 5, 6, 8, 12, 12, 11, 10]
|
||||||
|
[1, 3, 4, 5, 6, 8, 11, 12, 12, 10]
|
||||||
|
[1, 3, 4, 5, 6, 8, 10, 12, 12, 11]
|
||||||
|
[1, 3, 4, 5, 6, 8, 10, 11, 12, 12]
|
||||||
|
[1, 3, 4, 5, 6, 8, 10, 11, 12, 12]
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
|
\subsection{Quicksort}
|
||||||
|
\begin{verbatim}
|
||||||
|
[5, 6, 12, 8, 4, 10, 3, 12, 11, 1]
|
||||||
|
[5, 6, 3, 1, 4] 8 [12, 12, 11, 10]
|
||||||
|
[] 1 [6, 3, 4, 5]
|
||||||
|
[] 3 [5, 4, 6]
|
||||||
|
[5, 4] 6 []
|
||||||
|
[4] 5 []
|
||||||
|
[4] 5 []
|
||||||
|
[4, 5] 6 []
|
||||||
|
[] 3 [4, 5, 6]
|
||||||
|
[] 1 [3, 4, 5, 6]
|
||||||
|
[10] 11 [12, 12]
|
||||||
|
[12] 12 []
|
||||||
|
[12] 12 []
|
||||||
|
[10] 11 [12, 12]
|
||||||
|
[1, 3, 4, 5, 6] 8 [10, 11, 12, 12]
|
||||||
|
[1, 3, 4, 5, 6, 8, 10, 11, 12, 12]
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
|
\subsection{Insertion sort}
|
||||||
|
\begin{verbatim}
|
||||||
|
[5, 6, 12, 8, 4, 10, 3, 12, 11, 1]
|
||||||
|
[5, 6, 8, 12, 4, 10, 3, 12, 11, 1]
|
||||||
|
[5, 6, 8, 4, 12, 10, 3, 12, 11, 1]
|
||||||
|
[5, 6, 4, 8, 12, 10, 3, 12, 11, 1]
|
||||||
|
[5, 4, 6, 8, 12, 10, 3, 12, 11, 1]
|
||||||
|
[4, 5, 6, 8, 12, 10, 3, 12, 11, 1]
|
||||||
|
[4, 5, 6, 8, 10, 12, 3, 12, 11, 1]
|
||||||
|
[4, 5, 6, 8, 10, 3, 12, 12, 11, 1]
|
||||||
|
[4, 5, 6, 8, 3, 10, 12, 12, 11, 1]
|
||||||
|
[4, 5, 6, 3, 8, 10, 12, 12, 11, 1]
|
||||||
|
[4, 5, 3, 6, 8, 10, 12, 12, 11, 1]
|
||||||
|
[4, 3, 5, 6, 8, 10, 12, 12, 11, 1]
|
||||||
|
[3, 4, 5, 6, 8, 10, 12, 12, 11, 1]
|
||||||
|
[3, 4, 5, 6, 8, 10, 12, 11, 12, 1]
|
||||||
|
[3, 4, 5, 6, 8, 10, 11, 12, 12, 1]
|
||||||
|
[3, 4, 5, 6, 8, 10, 11, 12, 1, 12]
|
||||||
|
[3, 4, 5, 6, 8, 10, 11, 1, 12, 12]
|
||||||
|
[3, 4, 5, 6, 8, 10, 1, 11, 12, 12]
|
||||||
|
[3, 4, 5, 6, 8, 1, 10, 11, 12, 12]
|
||||||
|
[3, 4, 5, 6, 1, 8, 10, 11, 12, 12]
|
||||||
|
[3, 4, 5, 1, 6, 8, 10, 11, 12, 12]
|
||||||
|
[3, 4, 1, 5, 6, 8, 10, 11, 12, 12]
|
||||||
|
[3, 1, 4, 5, 6, 8, 10, 11, 12, 12]
|
||||||
|
[1, 3, 4, 5, 6, 8, 10, 11, 12, 12]
|
||||||
|
\end{verbatim}
|
||||||
|
|
||||||
\section{Exercise 2}
|
\section{Exercise 2}
|
||||||
|
|
||||||
\subsection{Exercise a}
|
\subsection{Exercise a}
|
||||||
The pseudocode for \textit{Sum of two} can be found in listing \ref{lst:sum2}.
|
The pseudocode for \textit{Sum of two} can be found in listing \ref{lst:sum2}.
|
||||||
The total cost of this algorithm in the worst case is the sum of the worst case
|
The total cost of this algorithm in the worst case is the sum of the worst case
|
||||||
of mergesort ($O(n log(n))$) and the cost of the worst case in the partition
|
of mergesort ($O(n log(n))$) and the cost of the worst case in the partition
|
||||||
done afterwards (which is equivalent to not finding the sum, i.e. $2 n =
|
done afterwards (which is equivalent to not finding a sum close to the median,
|
||||||
O(n)$). Therefore, the total cost is $\theta(n log(n))$.
|
i.e. $2 n = O(n)$). Therefore, the total cost is $\theta(n log(n))$.
|
||||||
|
|
||||||
\begin{lstlisting}[caption=Sum of two in pseudocode, label={lst:sum2}]
|
\begin{lstlisting}[caption=Sum of two in pseudocode, label={lst:sum2}]
|
||||||
FUNCTION SUM-OF-TWO(A, s):
|
FUNCTION SUM-OF-TWO(A, s):
|
||||||
|
|
|
@ -8,11 +8,10 @@ def insertionsort(A):
|
||||||
# insert the next element in the right place in the first elements
|
# insert the next element in the right place in the first elements
|
||||||
# continue until the array is done
|
# continue until the array is done
|
||||||
for j in range(1,len(A)):
|
for j in range(1,len(A)):
|
||||||
print(A)
|
|
||||||
print(A)
|
|
||||||
i = j - 1
|
i = j - 1
|
||||||
# while swapping is necessary or at the end of array
|
# while swapping is necessary or at the end of array
|
||||||
while i >= 0 and A[i+1] < A[i]:
|
while i >= 0 and A[i+1] < A[i]:
|
||||||
|
print(A)
|
||||||
# Swap A[i] and A[j]
|
# Swap A[i] and A[j]
|
||||||
A[i+1], A[i] = A[i], A[i+1]
|
A[i+1], A[i] = A[i], A[i+1]
|
||||||
i = i - 1
|
i = i - 1
|
||||||
|
|
34
mergesort.py
34
mergesort.py
|
@ -1,34 +0,0 @@
|
||||||
#!/usr/bin/env python3
|
|
||||||
# vim: set ts=4 sw=4 et tw=80:
|
|
||||||
|
|
||||||
import sys
|
|
||||||
import math
|
|
||||||
|
|
||||||
def find(A,x):
|
|
||||||
for a in A:
|
|
||||||
if a == x:
|
|
||||||
return True
|
|
||||||
return False
|
|
||||||
|
|
||||||
def merge(A,B):
|
|
||||||
X = []
|
|
||||||
for a in A:
|
|
||||||
if not find(X, a):
|
|
||||||
X.append(a)
|
|
||||||
for b in B:
|
|
||||||
if not find(X, b):
|
|
||||||
X.append(b)
|
|
||||||
return X
|
|
||||||
|
|
||||||
def mergesort(A):
|
|
||||||
l = len(A)
|
|
||||||
if l < 2:
|
|
||||||
return A
|
|
||||||
else:
|
|
||||||
lh = len(A) // 2
|
|
||||||
h1 = mergesort(A[:lh])
|
|
||||||
h2 = mergesort(A[lh:])
|
|
||||||
return merge(h1,h2)
|
|
||||||
|
|
||||||
args = [int(x) for x in sys.argv[1:]]
|
|
||||||
print(mergesort(args))
|
|
18
selection.py
Executable file
18
selection.py
Executable file
|
@ -0,0 +1,18 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
import sys
|
||||||
|
|
||||||
|
def selectionsort(A):
|
||||||
|
if len(A) < 2:
|
||||||
|
return A
|
||||||
|
for i in range(len(A)):
|
||||||
|
for j in range(i + 1, len(A)):
|
||||||
|
if A[i] > A[j]:
|
||||||
|
A[i], A[j] = A[j], A[i]
|
||||||
|
print(A)
|
||||||
|
return A
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
args = [int(x) for x in sys.argv[1:]]
|
||||||
|
print(selectionsort(args))
|
||||||
|
|
Reference in a new issue