Compare commits
1 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
d5d07915af |
21
README.md
Normal file
21
README.md
Normal file
@@ -0,0 +1,21 @@
|
||||
# Exercice 2
|
||||
|
||||
---
|
||||
|
||||
## `function_1`
|
||||
|
||||
Deux boucles = O(n\*m)
|
||||
|
||||
## `function_2`
|
||||
|
||||
While qui parcours le tableau = O(n)
|
||||
|
||||
## `function_3`
|
||||
|
||||
Un seul process d'information = O(1)
|
||||
|
||||
# Exercice 3
|
||||
|
||||
---
|
||||
|
||||
[[exercice_3.py]]
|
37
exercice_3.py
Normal file
37
exercice_3.py
Normal file
@@ -0,0 +1,37 @@
|
||||
def sort1d(arr):
|
||||
"""
|
||||
Cet algo à une complexité de O(n log n)
|
||||
"""
|
||||
|
||||
if len(arr) < 2:
|
||||
return arr
|
||||
|
||||
mid = len(arr) // 2
|
||||
left = sort1d(arr[:mid])
|
||||
right = sort1d(arr[mid:])
|
||||
|
||||
result = []
|
||||
i = j = 0
|
||||
|
||||
while i < len(left) and j < len(right):
|
||||
lv = left[i] if type(left[i]) is int else sum(left[i])
|
||||
rv = right[j] if type(right[j]) is int else sum(right[j])
|
||||
|
||||
if lv < rv:
|
||||
result.append(left[i])
|
||||
i += 1
|
||||
else:
|
||||
result.append(right[j])
|
||||
j += 1
|
||||
|
||||
result.extend(left[i:])
|
||||
result.extend(right[j:])
|
||||
return result
|
||||
|
||||
def sort2d(arr):
|
||||
"""
|
||||
Cet algo à une complexité de O(m n log n + m log m)
|
||||
"""
|
||||
|
||||
sorted_rows = [sort1d(row) for row in arr]
|
||||
return sort1d(sorted_rows)
|
Reference in New Issue
Block a user