Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- array = [5, 1, 3, 10, 11, -1, 0, -5, 50, 20, 17, 12, 12, 2, 12]
- def bubble_sort(array):
- n = len(array)
- for i in range(n):
- swapped = False
- for j in range(0, n - i - 1):
- if array[j] > array[j + 1]:
- array[j], array[j + 1] = array[j + 1], array[j]
- swapped = True
- if not swapped:
- print("Список уже отсортирован!")
- break
- def insertion(list_nums):
- for i in range(1, len(list_nums)):
- item = list_nums[i]
- i2 = i - 1
- while i2 >= 0 and list_nums[i2] > item:
- list_nums[i2 + 1] = list_nums[i2]
- i2 -= 1
- print(array)
- list_nums[i2 + 1] = item
- def merge_sort(arr):
- if len(arr) <= 1:
- return arr
- mid = len(arr) // 2
- left_half = arr[:mid]
- right_half = arr[mid:]
- left_half = merge_sort(left_half)
- right_half = merge_sort(right_half)
- sorted_arr = merge(left_half, right_half)
- return sorted_arr
- def merge(left, right):
- result = []
- left_index, right_index = 0, 0
- while left_index < len(left) and right_index < len(right):
- if left[left_index] < right[right_index]:
- result.append(left[left_index])
- left_index += 1
- else:
- result.append(right[right_index])
- right_index += 1
- result.extend(left[left_index:])
- result.extend(right[right_index:])
- return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement