Counting sort/fr: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | - | ||
| 1 | + | Le tri par comptage est algorithme de tri sur les entiers. | |
| 2 | 2 | ||
| 3 | - | ||
| 3 | + | ## Contents | |
| 4 | 4 | ||
| 5 | - | ||
| 6 | - | ||
| 5 | + | * 1 Propriété | |
| 6 | + | * 2 unité UCountingSortExtra.pas | |
| 7 | + | * 3 unité UCountingSort.pas | |
| 8 | + | * 4 Exemple d'emploi | |
| 7 | 9 | ||
| 8 | - | ||
| 10 | + | ## Propriété | |
| 9 | 11 | ||
| 10 | - | ||
| 12 | + | * Très rapide | |
| 13 | + | * Seulement avec de petits entiers | |
| 11 | 14 | ||
| 15 | + | ## unité UCountingSortExtra.pas | |
| 16 | + | ``` | |
| 12 | 17 | unit UCountingSortExtra; | |
| 13 | - | ||
| 14 | - | ||
| 15 | - | ||
| 16 | - | ||
| 17 | - | ||
| 18 | - | ||
| 19 | - | ||
| 20 | - | ||
| 21 | - | ||
| 22 | - | ||
| 23 | - | ||
| 24 | - | ||
| 25 | - | ||
| 26 | - | ||
| 27 | - | ||
| 28 | - | ||
| 29 | - | ||
| 30 | - | ||
| 31 | - | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | interface | |
| 21 | + | ||
| 22 | + | type | |
| 23 | + | // data type | |
| 24 | + | TItemCountingSort=integer; | |
| 25 | + | ||
| 26 | + | // sort order | |
| 27 | + | function IsAscendingCountingSort: boolean; inline; | |
| 28 | + | ||
| 29 | + | implementation | |
| 30 | + | ||
| 31 | + | // sort order | |
| 32 | + | function IsAscendingCountingSort: boolean; inline; | |
| 33 | + | begin | |
| 34 | + | result := true; | |
| 35 | + | end; | |
| 36 | + | ||
| 37 | + | end. | |
| 32 | 38 | ``` | |
| 33 | 39 | ||
| 34 | - | ||
| 35 | - | ||
| 36 | - | ||
| 40 | + | ||
| 37 | 41 | ||
| 42 | + | ## unité UCountingSort.pas | |
| 43 | + | ``` | |
| 38 | 44 | unit UCountingSort; | |
| 39 | - | ||
| 40 | - | ||
| 41 | - | ||
| 42 | - | ||
| 43 | - | ||
| 44 | - | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 45 | + | ||
| 46 | + | ||
| 47 | + | interface | |
| 48 | + | ||
| 49 | + | uses | |
| 50 | + | UCountingSortExtra; | |
| 51 | + | ||
| 52 | + | // sorting function | |
| 53 | + | procedure CountingSort( var a: array of TItemCountingSort ); | |
| 54 | + | ||
| 55 | + | implementation | |
| 56 | + | ||
| 57 | + | ||
| 58 | + | procedure CountingSort( var a: array of TItemCountingSort ); | |
| 59 | + | var | |
| 60 | + | min, max : TItemCountingSort; | |
| 61 | + | count_a : array of integer; | |
| 62 | + | i, j, z : integer; | |
| 59 | 63 | begin | |
| 60 | - | ||
| 61 | - | ||
| 64 | + | min := high( a ); | |
| 65 | + | max := min; | |
| 66 | + | for i := low( a ) to high( a ) do | |
| 67 | + | begin | |
| 68 | + | if a[ i ] < min then min := a[ i ]; | |
| 69 | + | if a[ i ] > max then max := a[ i ]; | |
| 70 | + | end; | |
| 71 | + | SetLength( count_a, max - min + 1); | |
| 72 | + | for i := 0 to ( max - min ) do count_a[ i ] := 0; | |
| 73 | + | for i := low( a ) to high( a ) do | |
| 74 | + | count_a[ a[ i ] - min ] := count_a[ a[ i ] - min ] + 1; | |
| 75 | + | if IsAscendingCountingSort then z:= low( a ) else z := high( a ); | |
| 76 | + | for i := min to max do | |
| 77 | + | for j := 0 to ( count_a[ i - min ] - 1 ) do | |
| 78 | + | begin | |
| 79 | + | a[ z ] := i; | |
| 80 | + | if IsAscendingCountingSort then inc( z ) else dec( z ); | |
| 81 | + | end; | |
| 62 | 82 | end; | |
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 83 | + | ||
| 84 | + | end. | |
| 77 | 85 | ``` | |
| 78 | 86 | ||
| 79 | 87 | ## Exemple d'emploi | |
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 88 | + | ``` | |
| 83 | 89 | uses | |
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 90 | + | UCountingSort | |
| 91 | + | ||
| 92 | + | ... | |
| 93 | + | ||
| 94 | + | var | |
| 95 | + | ||
| 96 | + | a: array[0..100] of integer; | |
| 97 | + | ||
| 98 | + | ||
| 99 | + | begin | |
| 100 | + | ||
| 101 | + | ... | |
| 102 | + | ||
| 103 | + | CountingSort( a ); | |
| 97 | 104 | ``` | |
| 98 | 105 | ||
| 99 | - | ||
| 100 | - | ||
| 101 | 106 | --- | |
| 102 | - | ||
| 107 | + | *From the Free Pascal wiki, page "Counting sort/fr", as the Internet Archive kept it on 2023-03-22. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
