Sign in Join
Wiki copytemporary

Counting sort/fr: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Counting sort/fr

OldNewLine
1+Le tri par comptage est algorithme de tri sur les entiers.
2+
3+## Propriété
4+
5+- Très rapide
6+- Seulement avec de petits entiers
7+
8+## unité UCountingSortExtra.pas
9+
10+``` pascal
11+
12+unit UCountingSortExtra;
13+
14+interface
15+
16+type
17+ // data type
18+ TItemCountingSort=integer;
19+
20+// sort order
21+function IsAscendingCountingSort: boolean; inline;
22+
23+implementation
24+
25+// sort order
26+function IsAscendingCountingSort: boolean; inline;
27+begin
28+ result := true;
29+end;
30+
31+end.
32+```
33+
34+## unité UCountingSort.pas
35+
36+``` pascal
37+
38+unit UCountingSort;
39+
40+interface
41+
42+uses
43+ UCountingSortExtra;
44+
45+// sorting function
46+procedure CountingSort( var a: array of TItemCountingSort );
47+
48+implementation
49+
50+procedure CountingSort( var a: array of TItemCountingSort );
51+var
52+ min, max : TItemCountingSort;
53+ count_a : array of integer;
54+ i, j, z : integer;
55+begin
56+ min := high( a );
57+ max := min;
58+ for i := low( a ) to high( a ) do
59+ begin
60+ if a[ i ] < min then min := a[ i ];
61+ if a[ i ] > max then max := a[ i ];
62+ end;
63+ SetLength( count_a, max - min + 1);
64+ for i := 0 to ( max - min ) do count_a[ i ] := 0;
65+ for i := low( a ) to high( a ) do
66+ count_a[ a[ i ] - min ] := count_a[ a[ i ] - min ] + 1;
67+ if IsAscendingCountingSort then z:= low( a ) else z := high( a );
68+ for i := min to max do
69+ for j := 0 to ( count_a[ i - min ] - 1 ) do
70+ begin
71+ a[ z ] := i;
72+ if IsAscendingCountingSort then inc( z ) else dec( z );
73+ end;
74+end;
75+
76+end.
77+```
78+
79+## Exemple d'emploi
80+
81+``` pascal
82+
83+uses
84+ UCountingSort
85+
86+ ...
87+
88+var
89+
90+ a: array[0..100] of integer;
91+
92+begin
93+
94+ ...
95+
96+ CountingSort( a );
97+```
98+
99+\
100+
101+---
102+*From the Free Pascal wiki, page "Counting sort/fr", as it stood on 2020-02-12 after a change by Trev, out of the wiki's dump of 2024-05-26. 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).*

Back to history