-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequentialInsertionSort.h
46 lines (43 loc) · 1.11 KB
/
SequentialInsertionSort.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
#include "list.h"
TRI SequentialInsertionSort_Create()
{
TRI This;
This.name = "Sequential Insertion Sort";
This.sort = sequentialInsertionSort;
return This;
}
/**
* \fn getSequentialPosition (int i, int tab[])
* \author Julien TEULLE
* \brief obtain the index of the table where a value must be insert
* with a sequential way.
* \param tab The table which is concern.
* \param size The size of tab.
*/
int getSequentialPosition (int i, int tab[])
{
int p = 0;
while (tab[p] < tab[i])
p++;
return p;
}
/**
* \fn sequentialInsertionSort (int tab[], int size)
* \author Julien TEULLE
* \brief Sort a table
* \param tab The table which is sort.
* \param size The size of tab.
*/
void sequentialInsertionSort (int tab[], int size)
{
int i, p, x;
for (i = 1; i < size; i++)
{
p = getSequentialPosition(i, tab);
x = tab[i];
int j;
for (j = i - 1; p <= j; j--)
tab[j+1] = tab[j];
tab[p] = x;
}
}