Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
c60330e96c |
60
algo_tri.py
Normal file
60
algo_tri.py
Normal file
@ -0,0 +1,60 @@
|
||||
#Trie les éléments dans un tableau
|
||||
def trie_bulle(arr):
|
||||
n = len(arr)
|
||||
for i in range(n):
|
||||
for j in range(0, n - i - 1):
|
||||
if arr[j] > arr[j + 1]:
|
||||
arr[j], arr[j + 1] = arr[j + 1], arr[j] # Échange les éléments
|
||||
|
||||
# Trie les tableau en fonction de la somme de leurs éléments
|
||||
def trie_bulle_somme(array):
|
||||
n = len(array)
|
||||
for i in range(n):
|
||||
for j in range(0, n - i - 1):
|
||||
if sum(detache_tab(array[j])) > sum(detache_tab(array[j + 1])):
|
||||
array[j], array[j + 1] = array[j + 1], array[j] # Échange les sous-tableaux
|
||||
|
||||
# Détache les tableaux entre eux
|
||||
def detache_tab(array):
|
||||
if isinstance(array, int): # Si val unique
|
||||
return [array]
|
||||
else:
|
||||
return [value for subarray in array for value in detache_tab(subarray)]
|
||||
|
||||
def trie_tab(array):
|
||||
if isinstance(array[0], int): # Si c'est un tableau 1D
|
||||
trie_bulle(array) # Trie le tableau 1D
|
||||
return array
|
||||
else: # Si multu dimension
|
||||
for i in range(len(array)):
|
||||
array[i] = trie_tab(array[i]) # Trie chaque sous-tableau
|
||||
# Trie les sous-tableaux par somme des valeur
|
||||
trie_bulle_somme(array)
|
||||
return array
|
||||
|
||||
|
||||
array = [[0, 3, 2], [9, 4, 5], [4, 1, 3]]
|
||||
result = trie_tab(array)
|
||||
print(result)
|
||||
|
||||
array_4d = [
|
||||
[
|
||||
[
|
||||
[1, 3, 2], [4, 6, 5]
|
||||
],
|
||||
[
|
||||
[7, 1, 9], [2, 1, 3]
|
||||
]
|
||||
],
|
||||
[
|
||||
[
|
||||
[3, 4, 1], [5, 2, 6]
|
||||
],
|
||||
[
|
||||
[9, 6, 8], [3, 1, 2]
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
result2 = trie_tab(array_4d)
|
||||
print(result2)
|
BIN
rapport_td4_landrin.odt
Normal file
BIN
rapport_td4_landrin.odt
Normal file
Binary file not shown.
BIN
rapport_td4_landrin.pdf
Normal file
BIN
rapport_td4_landrin.pdf
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user