-
Notifications
You must be signed in to change notification settings - Fork 12
Rendu Ewen Horville #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
6af4dbe
e7dbfdc
04bd49c
618f202
2ec1bc4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,51 @@ | ||
| import sort.range as srange | ||
| import sort.selection as selection | ||
| import sort.insertion as insertion | ||
| import sort.fusion as fusion | ||
| import time | ||
|
|
||
|
|
||
| def main(): | ||
| print("Hello world") | ||
| """This is the main function !""" | ||
|
|
||
| # Array Generation | ||
| """for i in range(1000000, 1100000, 100000): | ||
| start: float = time.time() | ||
| array: list[int] = srange.generate_array_of_number(i) | ||
| end: float = time.time() | ||
| print(str(end - start).replace(".", ","))""" | ||
|
|
||
| # Selection Sort | ||
| """for i in range(10000, 100000, 10000): | ||
| array: list[int] = srange.generate_array_of_number(i) | ||
| start: float = time.time() | ||
| selection.sort(array) | ||
| end: float = time.time() | ||
| print(str(end - start).replace(".", ","))""" | ||
|
|
||
| # Insertion Sort | ||
| """for i in range(10000, 100000, 10000): | ||
| array: list[int] = srange.generate_array_of_number(i) | ||
| start: float = time.time() | ||
| insertion.sort(array) | ||
| end: float = time.time() | ||
| print(str(end - start).replace(".", ","))""" | ||
|
|
||
| # Fusion Sort | ||
| """for i in range(100000, 3100000, 100000): | ||
| array: list[int] = srange.generate_array_of_number(i) | ||
| start: float = time.time() | ||
| fusion.sort(array) | ||
| end: float = time.time() | ||
| print(str(end - start).replace(".", ","))""" | ||
|
|
||
| # Python Sort | ||
| for i in range(100000, 3100000, 100000): | ||
| array: list[int] = srange.generate_array_of_number(i) | ||
| start: float = time.time() | ||
| array.sort() | ||
| end: float = time.time() | ||
| print(str(end - start).replace(".", ",")) | ||
|
|
||
|
|
||
| main() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,40 @@ | ||
| def sort(array: list[int]) -> list[int]: | ||
| return array | ||
| # Arrays of size 0/1 are already sorted. | ||
| if len(array) < 2: | ||
| return array | ||
|
|
||
| if len(array) > 2: | ||
| # Split and merge sorted | ||
| middle: int = len(array) // 2 + 1 | ||
| return merge(sort(array[:middle]), sort(array[middle:])) | ||
| else: | ||
| # For array of size 2, just swap if needed | ||
| return [array[1], array[0]] if array[1] < array[0] else array | ||
|
|
||
|
|
||
| def merge(array_1: list[int], array_2: list[int]): | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Type de retour manquant |
||
| final_array: list[int] = [] | ||
| index_1: int = 0 | ||
| index_2: int = 0 | ||
| len_1: int = len(array_1) | ||
| len_2: int = len(array_2) | ||
|
|
||
| for i in range(len_1 + len_2): | ||
| # If either list is empty, that means the other list is the end of | ||
| # the final array | ||
| if index_1 == len_1: | ||
| final_array += array_2[index_2:] | ||
| break | ||
| elif index_2 == len_2: | ||
| final_array += array_1[index_1:] | ||
| break | ||
|
|
||
| # Append the smallest value of either list. | ||
| if array_1[index_1] < array_2[index_2]: | ||
| final_array.append(array_1[index_1]) | ||
| index_1 += 1 | ||
| else: | ||
| final_array.append(array_2[index_2]) | ||
| index_2 += 1 | ||
|
|
||
| return final_array | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,26 @@ | ||
| def sort(array: list[int]) -> list[int]: | ||
| """Sorts the array using the insertion sort method.""" | ||
| # Arrays of size 0/1 are already sorted. | ||
| if len(array) < 2: | ||
| return array | ||
|
|
||
| array_length: int = len(array) | ||
|
|
||
| for i in range(array_length): | ||
| # Skip first | ||
| if i == 0: | ||
| continue | ||
|
Comment on lines
+11
to
+12
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On peut supprimer ça si on part directement du bon index ;) |
||
|
|
||
| # Main logic loop | ||
| new_index = i | ||
| while new_index > 0: | ||
| if array[new_index - 1] < array[i]: | ||
| break | ||
|
|
||
| new_index -= 1 | ||
|
|
||
| # Insert value right after the first number less than it | ||
| value_to_insert = array.pop(i) | ||
| array.insert(new_index, value_to_insert) | ||
|
|
||
| return array | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| import random | ||
|
|
||
|
|
||
| def generate_array_of_number(array_size: int) -> list[int]: | ||
| return [] | ||
| return [random.randint(0, 100) for i in range(array_size)] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| def get_factorial(number: int) -> int: | ||
| return number | ||
| if number > 1: | ||
| return number * get_factorial(number - 1) | ||
| else: | ||
| return 1 | ||
|
Comment on lines
+2
to
+5
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Utiliser un ternaire ? |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,20 @@ | ||
| def sort(array: list[int]) -> list[int]: | ||
| """Sorts the given array using the selection sort method.""" | ||
| # Arrays of size 0/1 are already sorted. | ||
| if len(array) < 2: | ||
| return array | ||
|
|
||
| array_length: int = len(array) | ||
|
|
||
| for i in range(array_length - 1): | ||
| # Sorting iteration loop | ||
| min_index: int = i | ||
| for j in range(i, array_length): | ||
| min_index = j if array[j] < array[min_index] else min_index | ||
|
|
||
| # If the current index is already sorted, do nothing. | ||
| if min_index != i: | ||
| # Swap the minimum and current numbers | ||
| array[i], array[min_index] = array[min_index], array[i] | ||
|
|
||
| return array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Soit le comportement (simplifié) de la fonction merge pour ce cas précis. Il serait plus clair de rester sur la fonction merge plutôt que de répliquer ce comportement ici.