-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackSort.h
69 lines (57 loc) · 1.03 KB
/
StackSort.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
TRI StackSort_Create()
{
TRI This;
This.name = "Stack Sort";
This.sort = StackSort;
return This;
}
/**
* \fn
* \author Kurt SAVIO
* \brief
* \param
* \return
*/
void AddStack (int tab[], int i, int Value)
{
int Parent = (i - 1) / 2;
tab[i] = Value;
if (i > 0 && Value > tab[Parent])
{
tab[i] = tab[Parent];
AddStack (tab, Parent, Value);
}
}
void DeleteMaxStack (int tab[], int * size)
{
int i = 0;
int gauche = 1;
int droite = 2;
int max;
Swap(tab, i, *size - 1);
--(*size);
while (gauche < *size)
{
if (droite < *size && tab[gauche] < tab[droite]) max = droite;
else max = gauche;
if (tab[i] < tab[max]) Swap(tab, i, max);
else break;
i = max;
gauche = 2 * i + 1;
droite = 2 * i + 2;
}
}
void CreateStack (int tab[], int size)
{
int i = 0;
for ( ; i < size; ++i)
AddStack(tab, i, tab[i]);
}
void StackSort (int tab[], int size)
{
CreateStack(tab, size);
int i = size;
int StacksSize = size;
for ( ; i > 1; --i)
DeleteMaxStack(tab, &StacksSize);
}