Tri d’un tableau

Fermé
Nanafa - 30 avril 2019 à 20:45
 cest_pas_faux - 9 août 2019 à 20:01
Bonjour,
Je suis un étudiant,j'ai besoin de faire un programme de trier un tableau par sélection en langage c.Qelqu'un peut m'aider stp.


Configuration: iPhone / Safari 10.0

2 réponses

TheKill_TnT Messages postés 148 Date d'inscription lundi 15 mai 2017 Statut Membre Dernière intervention 22 juin 2019 32
30 avril 2019 à 21:00
Tu peux reformuler ? Plus d'explications s'il te plaît.
0
cest_pas_faux
9 août 2019 à 20:01

#include <stdio.h>

int *exchange(int *tab, int a, int b)
{
int temp = 0;
temp = tab[a];
tab[a] = tab[b];
tab[b] = temp;
return tab;
}

void print(int *tab, int size)
{
for (int i = 0; i < size; i++)
{
printf("%d ", tab[i]);
}
printf("\n");
}

int *selection(int *tab, int size)
{
int min = 0;
for (int i = 0; i < size - 1; i++)
{
for (int j = i; j < size; j++)
{
if (tab[j] < tab[min])
{
min = j;
}
}
tab = exchange(tab, i, min);
min = i + 1;
}
return tab;
}

int main()
{
int t[] = {6, 2, 3, 1, 5, 4};

int size = sizeof t / sizeof (int);
int *tab = selection(t, size);
print(tab, size);

return 0;
}
0