-
Notifications
You must be signed in to change notification settings - Fork 76
Hw 7 #570
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: master
Are you sure you want to change the base?
Hw 7 #570
Changes from all commits
e3e8053
796c355
ed7a918
89eed76
877ade6
c266c31
fc1ead0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| import matplotlib.pyplot as plt | ||
| from random import randrange | ||
| from time import time | ||
|
|
||
|
|
||
| def timer(func): | ||
| def wrapper(*args, **kwargs): | ||
| start = time() | ||
| func(*args, **kwargs) | ||
| end = time() - start | ||
| return end | ||
| return wrapper | ||
|
|
||
|
|
||
| def get_list(stop, length, start=1): | ||
| return [randrange(start, stop) for _ in range(length)] | ||
|
|
||
|
|
||
| def swap(i, j): | ||
| temp = b_list[i] | ||
| b_list[i] = b_list[j] | ||
| b_list[j] = temp | ||
|
|
||
|
|
||
| def bubble_sort(lst_): | ||
| for j in range(len(lst_) - 1, 0, -1): | ||
| for i in range(j): | ||
| if lst_[i] < lst_[i + 1]: | ||
| swap(i, i + 1) | ||
| return lst_ | ||
|
|
||
|
|
||
| def merge_sort(arr): | ||
| def wrapper(arr_): | ||
| if len(arr_) > 1: | ||
| mid = len(arr_) // 2 | ||
| left = arr_[:mid] | ||
| right = arr_[mid:] | ||
|
|
||
| merge_sort(left) | ||
| merge_sort(right) | ||
|
|
||
| i = j = k = 0 | ||
|
|
||
| # Copy data to temp arr_ays l[] and right[] | ||
| while i < len(left) and j < len(right): | ||
| if left[i] < right[j]: | ||
| arr_[k] = left[i] | ||
| i += 1 | ||
| else: | ||
| arr_[k] = right[j] | ||
| j += 1 | ||
| k += 1 | ||
|
|
||
| # Checking if any element was left | ||
| while i < len(left): | ||
| arr_[k] = left[i] | ||
| i += 1 | ||
| k += 1 | ||
|
|
||
| while j < len(right): | ||
| arr_[k] = right[j] | ||
| j += 1 | ||
| k += 1 | ||
| wrapper(arr) | ||
|
|
||
|
|
||
| def hoare_sort(arr): | ||
| if len(arr) <= 1: | ||
| return | ||
| barrier = arr[0] | ||
| left = [] | ||
| middle = [] | ||
| right = [] | ||
| for el in arr: | ||
| if el < barrier: | ||
| left.append(el) | ||
| elif el == barrier: | ||
| middle.append(el) | ||
| else: | ||
| right.append(el) | ||
| hoare_sort(left) | ||
| hoare_sort(right) | ||
| k = 0 | ||
| for el in left + middle + right: | ||
| arr[k] = el | ||
| k += 1 | ||
|
|
||
|
|
||
| @timer | ||
| def run_bubble_sort(lst_): | ||
| bubble_sort(lst_) | ||
|
|
||
|
|
||
| @timer | ||
| def run_hoare_sort(lst_): | ||
| merge_sort(lst_) | ||
|
|
||
|
|
||
| @timer | ||
| def run_merge_sort(lst_): | ||
| hoare_sort(lst_) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| results = dict( | ||
| b_sort=(list(), list()), | ||
| h_sort=(list(), list()), | ||
| m_sort=(list(), list()) | ||
| ) | ||
| for x in [1, 50, 100, 250, 500, 750, 1000, 1250, 1500, 1750, 2000]: | ||
| lst = get_list(start=-100, stop=100, length=x) | ||
| m_list = lst[:] | ||
| b_list = lst[:] | ||
| h_list = lst[:] | ||
| m_sort = run_merge_sort(m_list) | ||
| b_sort = run_bubble_sort(b_list) | ||
| h_sort = run_hoare_sort(h_list) | ||
| for i in ['b_sort', 'h_sort', 'm_sort']: | ||
| results[i][1].append(eval(i)) | ||
| results[i][0].append(x) | ||
|
|
||
|
|
||
| print(results.get('b_sort')) | ||
| plt.plot(*results.get('b_sort'), 'r--', *results.get('h_sort'), 'b-', *results.get('m_sort'), 'g--') | ||
| plt.show() |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,3 +8,49 @@ | |
| Исходный - [46.11436617832828, 41.62921998361278, 18.45859540989644, 12.128870723745806, 8.025098788570562] | ||
| Отсортированный - [8.025098788570562, 12.128870723745806, 18.45859540989644, 41.62921998361278, 46.11436617832828] | ||
| """ | ||
|
|
||
| from random import randrange, random | ||
|
|
||
|
|
||
| def get_list(stop, length, start=1): | ||
| return [random()*50 for _ in range(length)] | ||
|
|
||
|
|
||
| def merge_sort(arr): | ||
| if len(arr) > 1: | ||
| mid = len(arr) // 2 | ||
| left = arr[:mid] | ||
| right = arr[mid:] | ||
|
|
||
| merge_sort(left) | ||
| merge_sort(right) | ||
|
|
||
| i = j = k = 0 | ||
|
|
||
| # Copy data to temp arrays l[] and right[] | ||
| while i < len(left) and j < len(right): | ||
| if left[i] < right[j]: | ||
| arr[k] = left[i] | ||
| i += 1 | ||
| else: | ||
| arr[k] = right[j] | ||
| j += 1 | ||
| k += 1 | ||
|
|
||
| # Checking if any element was left | ||
| while i < len(left): | ||
| arr[k] = left[i] | ||
| i += 1 | ||
| k += 1 | ||
|
|
||
| while j < len(right): | ||
| arr[k] = right[j] | ||
| j += 1 | ||
| k += 1 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| lst = get_list(start=0, stop=50, length=100) | ||
| merge_sort(lst) | ||
| print(lst) | ||
|
|
||
|
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. слияние реализовано |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,44 @@ | |
| массива. Но если это слишком сложно, то используйте метод сортировки, | ||
| который не рассматривался на уроках | ||
| """ | ||
|
|
||
| from random import randrange | ||
|
|
||
|
|
||
| def get_list(stop, length, start=1): | ||
| return [randrange(start, stop) for _ in range(length)] | ||
|
|
||
|
|
||
| def hoare_sort(arr): | ||
| if len(arr) <= 1: | ||
| return | ||
| barrier = arr[0] | ||
| left = [] | ||
| middle = [] | ||
| right = [] | ||
| for el in arr: | ||
| if el < barrier: | ||
| left.append(el) | ||
| elif el == barrier: | ||
| middle.append(el) | ||
| else: | ||
| right.append(el) | ||
| hoare_sort(left) | ||
| hoare_sort(right) | ||
| k = 0 | ||
| for el in left + middle + right: | ||
| arr[k] = el | ||
| k += 1 | ||
|
|
||
|
|
||
| def find_media(arr, med): | ||
| return arr[med] | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| median_index = 48 | ||
| array_length = 2 * median_index + 1 | ||
| lst = get_list(start=-50, stop=50, length=array_length) | ||
| hoare_sort(lst) | ||
| median = lst[median_index] | ||
| print(f"median = {median} \nsorted list = {lst}") | ||
|
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. здесь тоже хороши бы итоговые выводы - применили сортировку или нет и т.д. |
||
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.
не увидел у вас оптимизацию пузырька