diff --git a/README.md b/README.md new file mode 100644 index 0000000..16e16a1 --- /dev/null +++ b/README.md @@ -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]] diff --git a/exercice_3.py b/exercice_3.py new file mode 100644 index 0000000..c36d24e --- /dev/null +++ b/exercice_3.py @@ -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) \ No newline at end of file