Skip to content

Commit

Permalink
Backup of Obsidian Vault on 04-03-24 : 02:21: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 0104544 commit 3fd9126
Showing 1 changed file with 1 addition and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,58 +70,4 @@

To better visualise this example, checkout this [video](https://www.youtube.com/watch?v=7zuGmKfUt7s)

> [!exercise] Exercise 1 - Sort a list whose numbers span from 1 to 9.
>
> Step 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|}
> \hline
> 1 & 4 & 1 & 2 & 7 & 5 & 2 \\
> \hline
> \end{array}$
>
> Step 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|}
> \hline
> 0 & 2 & 4 & 4 & 5 & 6 & 6 & 7 & 7 & 7 \\
> \hline
> \end{array}$
>
> Step 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|}
> \hline
> & & & & & & \\
> \hline
> \end{array}$
>
> Step 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.
>
> $\text{Element Counts:} \quad \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|}
> \hline
> & 1 & & & & & \\
> \hline
> \end{array}$
>
> Step 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|}
> \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|}
> \hline
> 1 & 1 & 2 & 2 & 4 & 5 & 7 \\
> \hline
> \end{array}$

0 comments on commit 3fd9126

Please sign in to comment.