-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtri | python_code.py
More file actions
49 lines (42 loc) · 1.42 KB
/
tri | python_code.py
File metadata and controls
49 lines (42 loc) · 1.42 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
from numpy import array
# procédure pour saisir et valider la taille du tableau
def saisir():
n=int(input("Donner le taille du tableau=:"))
while not (3<=n<=5):
n=int(input("Donner taille du tableau=:"))
return n
# procédure pour remplir le tableau avec des valeurs saisies par l'utilisateur
def remplir(t,n):
for i in range(0,n):
t[i]=int(input("t["+str(i)+"]=:"))
# Fonction de tri du tableau (tri par sélection)
def tri_seléction(t, n):
# Parcourir tous les éléments du tableau
for i in range(n-1):
# Position du minimum actuel
pMin = i
# Chercher le minimum dans la partie non triée
for j in range(i+1, n):
if t[j] < t[pMin]:
pMin = j
# Échanger les éléments si nécessaire
if i != pMin:
aux = t[i]
t[i] = t[pMin]
t[pMin] = aux
# procédure pour afficher les éléments du tableau séparés par "|"
def afficher(t,n):
for i in range(0,n):
print(t[i],end="|")
# Programme Principal
n=saisir() # Saisie de la taille du tableau
t=array([int()]*n) # Création du tableau avec NumPy
remplir(t,n) # Remplissage du tableau
# Affichage du tableau avant le tri
print("le tableau avant le tri")
afficher(t,n)
print()#pour retourner a la ligne
tri(t,n) # Tri du tableau
# Affichage du tableau après le tri
print("le tableau aprés le tri")
afficher(t,n)