|
7 | 7 |
|
8 | 8 | ## Motivation |
9 | 9 |
|
10 | | -[Wikipedia](https://en.wikipedia.org/wiki/Sorting_algorithm) explains it very nicely: sorting is ubiquitous in Computer Sciences. |
11 | | -It is a simple problem ("*How can I sort the following values the most efficiently?*") that has many solutions, but still offers open problems. |
| 10 | +[Higher-order programming](https://en.wikipedia.org/wiki/Higher-order_programming) allows to manipulate for example methods themselves. |
| 11 | +This can be useful for many purposes, and is called ["delegates"](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/) in C#. |
| 12 | +We explain its basics below, and refer to [the sorting lecture](./lectures/misc/sorting) for an example of how it can be used. |
12 | 13 |
|
13 | | -We only consider **correct** algorithms, i.e., one where their output is such that |
| 14 | +## Action -- In short |
14 | 15 |
|
15 | | -- each element is larger than or equal to the previous one, according to the order picked, |
16 | | -- all the elements that were in the input are present in the output (with the same cardinality, if repetition is allowed). |
| 16 | +[An *action* is](https://learn.microsoft.com/en-us/dotnet/api/system.action?view=net-9.0) |
17 | 17 |
|
18 | | -There are many ways of "comparing" sorting algorithms. |
19 | | -A sorting algorithm… |
| 18 | +> a method that has no parameters and does not return a value. |
20 | 19 |
|
21 | | -- has a best, worst and average case time complexity (measured in general in number of comparisons required), |
22 | | -- has a best, worst and average case space complexity (i.e., "how much additional memory is required?"), |
23 | | -- can be "stable" (i.e., equal values are not permutted), |
24 | | -- uses *insertion*, *exchange*, *selection*, *merging* method, |
25 | | -- is serial or parallel, |
| 20 | +An "Action<T>" is a method that has a single parameter (of type `T`) and does not return a value. |
26 | 21 |
|
27 | | -among other properties. |
| 22 | +Here are for example three actions: |
28 | 23 |
|
29 | | -# Insertion Sort Algorithm |
| 24 | +``` |
| 25 | +!include code/projects/Actions/Actions/ExampleActions.cs |
| 26 | +``` |
| 27 | + |
| 28 | +- `Test`, `Display` and `DisplayArray` all have `void` as their return type, |
| 29 | +- `Test` does not take any argument, |
| 30 | +- `Display` takes an `int` as an argument, |
| 31 | +- `DisplayArray` takes "an array of `T`" (that is, a [generic type](./lectures/oop/generic_types) as an argument. |
30 | 32 |
|
31 | | -This algorithm is [nicely explained and illustrated on wikipedia](https://en.wikipedia.org/wiki/Insertion_sort), and can be implemented as follows: |
| 33 | +We can call those easily: |
32 | 34 |
|
33 | 35 | ``` |
34 | | -!include`snippetStart="// Insertion Algorithm", snippetEnd="// Done with insertion Algorithm"` code/projects/Sorting/Sorting/Sorting.cs |
| 36 | +!include`snippetStart="// We can call our methods directly:", snippetEnd="// Or we can store them"` code/projects/Actions/Actions/Program.cs |
35 | 37 | ``` |
36 | 38 |
|
37 | | -# Heapsort Algorithm |
38 | | - |
39 | | -We first define some helper methods: |
| 39 | +We can also store them into variables and then call them: |
40 | 40 |
|
41 | 41 | ``` |
42 | | -!include`snippetStart="// Helper methods for Heapsort", snippetEnd="// Done with helper methods for Heapsort"` code/projects/Sorting/Sorting/Sorting.cs |
| 42 | +!include`snippetStart="// in variables and then call them:", snippetEnd="// Passing an Action as an argument:"` code/projects/Actions/Actions/Program.cs |
43 | 43 | ``` |
44 | 44 |
|
45 | | -and then leverage the heap structure to sort: |
| 45 | +As we can see, `ExampleActions.Display` is of type `Action<int>` since the `Display` method takes an `int` as argument. |
| 46 | + |
| 47 | +## Action as Parameter |
| 48 | + |
| 49 | +Method can take actions as parameter: |
46 | 50 |
|
47 | 51 | ``` |
48 | | -!include`snippetStart="// Heapsort Algorithm", snippetEnd="// Done with heapsort Algorithm"` code/projects/Sorting/Sorting/Sorting.cs |
| 52 | +!include code/projects/Actions/Actions/CallingAction.cs |
49 | 53 | ``` |
50 | 54 |
|
51 | | -Note that `PercDown` builds a *max heap*: once the values are "pre-sorted **greater value first**", removing the first one to move it to the *end* of the list makes the list sorted from smallest to greatest value once we are done. |
| 55 | +and then can be passed an action as an argument: |
| 56 | + |
| 57 | +``` |
| 58 | +!include`snippetStart="// Passing an action as an argument:", snippetEnd="// Done passing an action."`code/projects/Actions/Actions/Program.cs |
| 59 | +``` |
0 commit comments