-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_sort_int_tab.c
More file actions
53 lines (45 loc) · 1.07 KB
/
my_sort_int_tab.c
File metadata and controls
53 lines (45 loc) · 1.07 KB
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
/* *****************************************************************************
** Write a function that sort a table made of integers in ascending order.
**
** The parameters are: a pointer to integer and the number of integers in the
** table.
**
** void my_sort_int_tab(int *tab, int size);
** ****************************************************************************/
void my_sort_int_tab(int *tab, int size);
void my_sort_int_tab(int *tab, int size)
{
int number;
int i;
int j;
i = -1;
while (++i < size)
{
j = i;
while (++j < size)
{
if (tab[j] < tab[i])
{
number = tab[i];
tab[i] = tab[j];
tab[j] = number;
}
}
}
}
#ifdef MY_SORT_INT_TAB
#include <stdio.h>
int main()
{
int tab[] = { -42, 21, 0, 42, -21 };
int tab2[] = { -42, -21, 0, 21, 42 };
int i;
my_sort_int_tab(tab, 5);
for (i = 0 ; i < 5 ; ++i)
printf("%d%s", tab[i], (i + 1 < 5 ? ", " : "\n"));
my_sort_int_tab(tab2, 5);
for (i = 0 ; i < 5 ; ++i)
printf("%d%s", tab2[i], (i + 1 < 5 ? ", " : "\n"));
return (0);
}
#endif /* !MY_SORT_INT_TAB */