Tri counting
The tri counting (called also tri rack) is a Algorithme of sorting which applies to whole values .
Definition
The principle rests on the construction of the Histogramme of the data, then the sweeping of this one in an increasing way, in order to rebuild the sorted data.
Here, the concept of stability (Tri stable) does not have a direction réllement, since the histogram factorizes the data (c.a.d several identical elements will be represented by a single quantified element). This sorting cannot thus be applied to complex structures, and it is appropriate exclusively for the data made up of Integers ranging between a limits min and a limits known max . In a preoccupation with an effectiveness, those must be relatively close one to it, as well as the number of elements must be relatively large.
In this configuration, and with a distribution of data according to a uniform Law, this sorting is fastest (one barter, to some extent, of the computing time against memory).
The very particular restriction imposed on its values of entry in fact a sorting in linear time, whereas an optimal sorting by comparisons requires a number of operations about .
Example
It is supposed that one has a table composed of 100 entireties between 0 and 30 (terminals included/understood).
The process of the sorting by counting is the following: one counts the number of the " 0" , the number of the " 1" ,…, the number of the " 30" present in , and is rebuilt by adding the values to it according to their increasing quantity (one does not sort the values but the counting of these values within the table).
The table of 5 entireties 1,27,3,1,3 contains 2 times 1,2 times 3 and 1 time 27, the table sorted by the method of the tri counting is thus: 1,1,3,3,27
Table before and after sorting:
Table of counting:
Algorithm
The algorithm presented here is not the only solution with the problem, and is perhaps not most optimal. The sign ⇐ is used for the assignments.
Table is the table with sorting, and passed in parameter of the function tri_par_comptage . The variable borne_superieure , is the maximum whole value present in .
It is considered that the index of the tables starts to 0.
The function tri_par_comptage uses intermediate variables:
- tab_comptage, is a table containing N elements, N being the maximum value in .
- I and J is of the variables of the whole type, being used to traverse the tables and tab_comptage.
function tri_par_comptage ( table , whole borne_superieure) /* Initialization of the variables * tab_comptage + 1 taille_tab ⇐ size () - 1 /* Initialization of the table of counting to 0 * For I ⇐ 0 Until borne_superieure To make tab_comptage I ⇐ 0 FinPour /* Création of the table of counting * For I ⇐ 0 Until taille_tab To make tab_comptage [I] ⇐ tab_comptage [I] + 1 FinPour /* Création of the table sorted * L ⇐ 0 For I ⇐ 0 Until borne_superieure To make For J ⇐ 1 Until tab_comptage To make = I L ⇐ L + 1 FinPour FinPour Turns over
Implementation
The implementation is commonplace in C
void tri_hist (int T, int len)
{
int I, J, K;
int * hist = calloc (MAX, sizeof (int));
for (i=0; I < len; i++)
hist T [I] ++;
k=0;
for (i=0; I < MAX; i++)
for (j=0; J < hist; j++)
T = I;
free (hist);
}
External bond
| Random links: | Chalk feature | Lo Nuestro 2005 | Cut off | Hristo Yanev | Fermentek | Comté_de_Garza,_le_Texas |