Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def binary_search(numbers, target):
- left = 0
- right = len(numbers) - 1
- while left <= right:
- mid_index = (left + right) // 2
- mid_el = numbers[mid_index]
- if mid_el == target:
- return mid_index
- if mid_el < target:
- left = mid_index + 1
- else:
- right = mid_el - 1
- return "Not Found"
- # ////////////////////////////////////////////////
- def selection_sort(numbers):
- for index in range(len(numbers)):
- min_index = index
- for curr_index in range(index + 1, len(numbers)):
- if numbers[curr_index] < numbers[min_index]:
- min_index = curr_index
- numbers[index], numbers[min_index] = numbers[min_index], numbers[index]
- return numbers
- # ////////////////////////////////////////////////
- def bubble_sort_1(numbers):
- is_sorted = False
- counter = 0
- while not is_sorted:
- is_sorted = True
- for index in range(1, len(numbers) - counter):
- if numbers[index] < numbers[index - 1]:
- numbers[index], numbers[index - 1] = numbers[index - 1], numbers[index]
- is_sorted = False
- counter += 1
- return numbers
- # ////////////////////////////////////////////////
- def bubble_sort_2(numbers):
- for i in range(len(numbers)):
- for j in range(1, len(numbers) - i):
- if numbers[j - 1] > numbers[j]:
- numbers[j], numbers[j - 1] = numbers[j - 1], numbers[j]
- return numbers
- # ////////////////////////////////////////////////
- def insertion_sort(numbers):
- for i in range(1, len(numbers)):
- for j in range(i, 0, -1):
- if numbers[j] < numbers[j - 1]:
- numbers[j], numbers[j - 1] = numbers[j - 1], numbers[j]
- else:
- break
- return numbers
- # ////////////////////////////////////////////////
- def quick_sort(start, end, nums):
- if start >= end:
- return
- pivot, left, right = start, start + 1, end
- while left <= right:
- if nums[left] > nums[pivot] > nums[right]:
- nums[left], nums[right] = nums[right], nums[left]
- if nums[left] <= nums[pivot]:
- left += 1
- if nums[right] >= nums[pivot]:
- right -= 1
- nums[pivot], nums[right] = nums[right], nums[pivot]
- quick_sort(start, right - 1, nums)
- quick_sort(left, end, nums)
- nums = [int(x) for x in input().split()]
- quick_sort(0, len(nums) - 1, nums)
- print(*nums, sep=' ')
- # ////////////////////////////////////////////////
- def merge_arrays(left, right):
- result = [None] * (len(left) + len(right))
- left_idx = 0
- right_idx = 0
- result_idx = 0
- while left_idx < len(left) and right_idx < len(right):
- if left[left_idx] < right[right_idx]:
- result[result_idx] = left[left_idx]
- left_idx += 1
- else:
- result[result_idx] = right[right_idx]
- right_idx += 1
- result_idx += 1
- while left_idx < len(left):
- result[result_idx] = left[left_idx]
- left_idx += 1
- result_idx += 1
- while right_idx < len(right):
- result[result_idx] = right[right_idx]
- right_idx += 1
- result_idx += 1
- return result
- def merge_sort(nums):
- if len(nums) == 1:
- return nums
- mid_idx = len(nums) // 2
- left = nums[:mid_idx]
- right = nums[mid_idx:]
- return merge_arrays(merge_sort(left), merge_sort(right))
- nums = [int(x) for x in input().split()]
- result = merge_sort(nums)
- print(*result, sep=" ")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement