|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: a post with pseudo code |
| 4 | +date: 2024-04-15 00:01:00 |
| 5 | +description: this is what included pseudo code could look like |
| 6 | +tags: formatting code |
| 7 | +categories: sample-posts |
| 8 | +pseudocode: true |
| 9 | +--- |
| 10 | + |
| 11 | +This is an example post with some pseudo code rendered by [pseudocode](https://github.com/SaswatPadhi/pseudocode.js). The example presented here is the same as the one in the [pseudocode.js](https://saswat.padhi.me/pseudocode.js/) documentation, with only one simple but important change: everytime you would use `$`, you should use `$$` instead. Also, note that the `pseudocode` key in the front matter is set to `true` to enable the rendering of pseudo code. As an example, using this code: |
| 12 | + |
| 13 | +````markdown |
| 14 | +```pseudocode |
| 15 | +% This quicksort algorithm is extracted from Chapter 7, Introduction to Algorithms (3rd edition) |
| 16 | +\begin{algorithm} |
| 17 | +\caption{Quicksort} |
| 18 | +\begin{algorithmic} |
| 19 | +\PROCEDURE{Quicksort}{$$A, p, r$$} |
| 20 | + \IF{$$p < r$$} |
| 21 | + \STATE $$q = $$ \CALL{Partition}{$$A, p, r$$} |
| 22 | + \STATE \CALL{Quicksort}{$$A, p, q - 1$$} |
| 23 | + \STATE \CALL{Quicksort}{$$A, q + 1, r$$} |
| 24 | + \ENDIF |
| 25 | +\ENDPROCEDURE |
| 26 | +\PROCEDURE{Partition}{$$A, p, r$$} |
| 27 | + \STATE $$x = A[r]$$ |
| 28 | + \STATE $$i = p - 1$$ |
| 29 | + \FOR{$$j = p$$ \TO $$r - 1$$} |
| 30 | + \IF{$$A[j] < x$$} |
| 31 | + \STATE $$i = i + 1$$ |
| 32 | + \STATE exchange |
| 33 | + $$A[i]$$ with $$A[j]$$ |
| 34 | + \ENDIF |
| 35 | + \STATE exchange $$A[i]$$ with $$A[r]$$ |
| 36 | + \ENDFOR |
| 37 | +\ENDPROCEDURE |
| 38 | +\end{algorithmic} |
| 39 | +\end{algorithm} |
| 40 | +``` |
| 41 | +```` |
| 42 | + |
| 43 | +Generates: |
| 44 | + |
| 45 | +```pseudocode |
| 46 | +% This quicksort algorithm is extracted from Chapter 7, Introduction to Algorithms (3rd edition) |
| 47 | +\begin{algorithm} |
| 48 | +\caption{Quicksort} |
| 49 | +\begin{algorithmic} |
| 50 | +\PROCEDURE{Quicksort}{$$A, p, r$$} |
| 51 | + \IF{$$p < r$$} |
| 52 | + \STATE $$q = $$ \CALL{Partition}{$$A, p, r$$} |
| 53 | + \STATE \CALL{Quicksort}{$$A, p, q - 1$$} |
| 54 | + \STATE \CALL{Quicksort}{$$A, q + 1, r$$} |
| 55 | + \ENDIF |
| 56 | +\ENDPROCEDURE |
| 57 | +\PROCEDURE{Partition}{$$A, p, r$$} |
| 58 | + \STATE $$x = A[r]$$ |
| 59 | + \STATE $$i = p - 1$$ |
| 60 | + \FOR{$$j = p$$ \TO $$r - 1$$} |
| 61 | + \IF{$$A[j] < x$$} |
| 62 | + \STATE $$i = i + 1$$ |
| 63 | + \STATE exchange |
| 64 | + $$A[i]$$ with $$A[j]$$ |
| 65 | + \ENDIF |
| 66 | + \STATE exchange $$A[i]$$ with $$A[r]$$ |
| 67 | + \ENDFOR |
| 68 | +\ENDPROCEDURE |
| 69 | +\end{algorithmic} |
| 70 | +\end{algorithm} |
| 71 | +``` |
0 commit comments