diff options
| author | haoyuren <13851610112@163.com> | 2025-06-27 10:56:07 -0700 |
|---|---|---|
| committer | haoyuren <13851610112@163.com> | 2025-06-27 10:56:07 -0700 |
| commit | bcd3e28631925186a89b0958fd40b72e59788ffc (patch) | |
| tree | ce8b0e338d1d3f5c2c2f46402174b411e087e376 /2025-06-27 | |
| parent | 10181a2e7d17af7c3908cb47e9bd9deff5fe8c78 (diff) | |
Diffstat (limited to '2025-06-27')
| -rw-r--r-- | 2025-06-27/2025-06-27-notes.pdf | bin | 0 -> 99111 bytes | |||
| -rw-r--r-- | 2025-06-27/2025-06-27-notes.tex | 69 |
2 files changed, 69 insertions, 0 deletions
diff --git a/2025-06-27/2025-06-27-notes.pdf b/2025-06-27/2025-06-27-notes.pdf Binary files differnew file mode 100644 index 0000000..3507725 --- /dev/null +++ b/2025-06-27/2025-06-27-notes.pdf diff --git a/2025-06-27/2025-06-27-notes.tex b/2025-06-27/2025-06-27-notes.tex new file mode 100644 index 0000000..8f153d0 --- /dev/null +++ b/2025-06-27/2025-06-27-notes.tex @@ -0,0 +1,69 @@ +\documentclass{article} +\usepackage[utf8]{inputenc} +\usepackage[margin=1.5cm, top=2cm, bottom=2cm]{geometry} +\usepackage{amsmath} +\usepackage{amsfonts} +\usepackage{amssymb} +\usepackage{graphicx} +\usepackage{setspace} +\setstretch{0.9} + +\title{CS161 Notes - Merge Sort} +\author{Yuren Hao} +\date{\today} + +\begin{document} + +\maketitle + +\section{Merge Sort} + +Divide-and-conquer sorting algorithm: recursively divide array into halves, sort each half, then merge. + +\textbf{Algorithm:} +\begin{enumerate} + \item \textbf{Divide:} Split $A[1..n]$ at midpoint $\lfloor n/2 \rfloor$ + \item \textbf{Conquer:} Recursively sort both halves + \item \textbf{Combine:} Merge sorted halves +\end{enumerate} + +\section{Pseudocode} + +\textbf{MERGE-SORT}$(A, p, r)$: +\begin{enumerate} + \item \textbf{if} $p < r$: + \begin{itemize} + \item $q = \lfloor (p + r) / 2 \rfloor$ + \item \textbf{MERGE-SORT}$(A, p, q)$ + \item \textbf{MERGE-SORT}$(A, q + 1, r)$ + \item \textbf{MERGE}$(A, p, q, r)$ + \end{itemize} +\end{enumerate} + +\textbf{MERGE}$(A, p, q, r)$: +\begin{enumerate} + \item $n_1 = q - p + 1$, $n_2 = r - q$ + \item Create arrays $L[1..n_1 + 1]$, $R[1..n_2 + 1]$ + \item Copy $A[p..q] \to L[1..n_1]$, $A[q+1..r] \to R[1..n_2]$ + \item $L[n_1 + 1] = R[n_2 + 1] = \infty$ + \item $i = j = 1$ + \item \textbf{for} $k = p$ \textbf{to} $r$: + \begin{itemize} + \item \textbf{if} $L[i] \leq R[j]$: $A[k] = L[i++]$ + \item \textbf{else}: $A[k] = R[j++]$ + \end{itemize} +\end{enumerate} + +\section{Complexity Analysis} + +\textbf{Time:} $T(n) = 2T(n/2) + \Theta(n) = \Theta(n \log n)$ (Master Theorem) + +\textbf{Space:} $\Theta(n)$ auxiliary arrays + $\Theta(\log n)$ recursion stack = $\Theta(n)$ + +\section{Properties} + +\textbf{Advantages:} Stable, predictable $O(n \log n)$, parallelizable + +\textbf{Disadvantages:} $O(n)$ extra space, not in-place + +\end{document}
\ No newline at end of file |
