Skip to content

Commit

Permalink
Backup of Obsidian Vault on 04-03-24 : 02:11:32 from SamPC | 1 files …
Browse files Browse the repository at this point in the history
…changed

Details:
M content/Computer Science/Algorithms & Data Structures Analysis/Exercises/3.2.1.md
  • Loading branch information
santiagosayshey committed Mar 3, 2024
1 parent 12f3f7b commit 922b87b
Showing 1 changed file with 37 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -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}$$

0 comments on commit 922b87b

Please sign in to comment.