-
Notifications
You must be signed in to change notification settings - Fork 367
Expand file tree
/
Copy pathBidirectional_Selection_Sort.py
More file actions
35 lines (27 loc) · 956 Bytes
/
Bidirectional_Selection_Sort.py
File metadata and controls
35 lines (27 loc) · 956 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
def bidirectional_selection_sort(arr):
left = 0
right = len(arr) - 1
while left < right:
minimum = arr[left]
maximum = arr[right]
min_index = left
max_index = right
for i in range(left+1, right + 1):
if arr[i] < minimum:
minimum = arr[i]
min_index = i
if arr[i] > maximum:
maximum = arr[i]
max_index = i
if min_index == right:
arr[left], arr[min_index] = arr[min_index], arr[left]
else:
arr[left], arr[min_index] = arr[min_index], arr[left]
arr[right], arr[max_index] = arr[max_index], arr[right]
left += 1
right -= 1
n = int(input("Enter the number of elements in the array: "))
array = list(map(int, input("Enter the elements: ").split()))
print("Original array:", array)
bidirectional_selection_sort(array)
print("Sorted array:", array)