From 922b87bc1cd72f883148a1b6d743bfe08e158c23 Mon Sep 17 00:00:00 2001 From: santiagosayshey Date: Mon, 4 Mar 2024 02:11:33 +1030 Subject: [PATCH] Backup of Obsidian Vault on 04-03-24 : 02:11:32 from SamPC | 1 files changed Details: M content/Computer Science/Algorithms & Data Structures Analysis/Exercises/3.2.1.md --- .../Exercises/3.2.1.md | 75 +++++++++---------- 1 file changed, 37 insertions(+), 38 deletions(-) diff --git a/content/Computer Science/Algorithms & Data Structures Analysis/Exercises/3.2.1.md b/content/Computer Science/Algorithms & Data Structures Analysis/Exercises/3.2.1.md index 19bfedd0..32a155fd 100644 --- a/content/Computer Science/Algorithms & Data Structures Analysis/Exercises/3.2.1.md +++ b/content/Computer Science/Algorithms & Data Structures Analysis/Exercises/3.2.1.md @@ -1,69 +1,68 @@ - > [!exercise]+ Exercise 1 - Sort a list whose numbers span from 1 to 9. - - -> [!exercise]+ 1. Create an array of size $k$, and increment each position by 1 for every occurrence of that position in the array. > -> $$ -> \text{Unsorted Array:} \quad \begin{array}{|c|c|c|c|c|c|c|} +> 1. Create an array of size $k$, and increment each position by 1 for every occurrence of that position in the array. +> $$\begin{array}{l} +> \text{Unsorted Array:} \\ +> \begin{array}{|c|c|c|c|c|c|c|} > \hline > 1 & 4 & 1 & 2 & 7 & 5 & 2 \\ > \hline > \end{array} -> $$ +> \end{array}$$ > -> $$ -> \text{Element Counts:} \quad \begin{array}{|c|c|c|c|c|c|c|c|c|c|} -> \hline -> 0 & 2 & 2 & 0 & 1 & 1 & 0 & 1 & 0 & 0 \\ -> \hline -> \end{array} -> $$ -> 2. Starting from the second position, we add the value of the previous position to the current position. -> $$ -> \text{Element Counts:} \quad \begin{array}{|c|c|c|c|c|c|c|c|c|c|} +> 2. Starting from the second position, we add the value of the previous position to the current position. +> $$\begin{array}{l} +> \text{Element Counts:} \\ +> \begin{array}{|c|c|c|c|c|c|c|c|c|c|} > \hline > 0 & 2 & 4 & 4 & 5 & 6 & 6 & 7 & 7 & 7 \\ > \hline > \end{array} -> $$ -> 3. Create a sorted array of length max of previous array (7) -> $$ -> \text{Sorted Array:} \quad \begin{array}{|c|c|c|c|c|c|c|} +> \end{array}$$ +> +> 3. Create a sorted array of length max of previous array (7) +> $$\begin{array}{l} +> \text{Sorted Array:} \\ +> \begin{array}{|c|c|c|c|c|c|c|} > \hline > & & & & & & \\ > \hline > \end{array} -> $$ -> 4. Iterate through the unsorted array. For each element $i$ in the unsorted array, find the value at position $i$ in the element counts array. This value is the position that $i$ will be in the sorted array. Decrement the value in the counts array by 1. -> - For example, the first element in the unsorted array is 1. We go to $counts[1]$ and find that the value is $2$. We place $1$ at position $2$ in the sorted array, and decrement $counts[1]$ by 1. -> $$ -> \text{Element Counts:} \quad \begin{array}{|c|c|c|c|c|c|c|c|c|c|} +> \end{array}$$ +> +> 4. Iterate through the unsorted array. For each element $i$ in the unsorted array, find the value at position $i$ in the element counts array. This value is the position that $i$ will be in the sorted array. Decrement the value in the counts array by 1. +> - For example, the first element in the unsorted array is 1. We go to $\text{counts}[1]$ and find that the value is $2$. We place $1$ at position $2$ in the sorted array, and decrement $\text{counts}[1]$ by 1. +> $$\begin{array}{l} +> \text{Element Counts:} \\ +> \begin{array}{|c|c|c|c|c|c|c|c|c|c|} > \hline > 0 & 1 & 4 & 4 & 5 & 6 & 6 & 7 & 7 & 7 \\ > \hline > \end{array} -> $$ -> $$ -> \text{Sorted Array:} \quad \begin{array}{|c|c|c|c|c|c|c|} +> \end{array}$$ +> $$\begin{array}{l} +> \text{Sorted Array:} \\ +> \begin{array}{|c|c|c|c|c|c|c|} > \hline > & 1 & & & & & \\ > \hline > \end{array} -> $$ -> 5. Repeat this for every element in the unsorted array -> $$ -> \text{Element Counts:} \quad \begin{array}{|c|c|c|c|c|c|c|c|c|c|} +> \end{array}$$ +> +> 5. Repeat this for every element in the unsorted array +> $$\begin{array}{l} +> \text{Element Counts:} \\ +> \begin{array}{|c|c|c|c|c|c|c|c|c|c|} > \hline > 0 & 0 & 2 & 4 & 4 & 5 & 6 & 6 & 7 & 7 \\ > \hline > \end{array} -> $$ -> $$ -> \text{Sorted Array:} \quad \begin{array}{|c|c|c|c|c|c|c|} +> \end{array}$$ +> $$\begin{array}{l} +> \text{Sorted Array:} \\ +> \begin{array}{|c|c|c|c|c|c|c|} > \hline > 1 & 1 & 2 & 2 & 4 & 5 & 7 \\ > \hline > \end{array} -> $$ - +> \end{array}$$