-
Notifications
You must be signed in to change notification settings - Fork 65
/
reverse_array_inplace.py
45 lines (33 loc) · 1.12 KB
/
reverse_array_inplace.py
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
36
37
38
39
40
41
42
43
44
45
""" The problem is that we want to reverse a T[] array in O(N) linear
time complexity and we want the algorithm to be in-place as well!
For example: input is [1,2,3,4,5] then the output is [5,4,3,2,1]
"""
def iterative_reverse(arr):
""" reverse a list using iterative approach """
start = 0
end = len(arr) - 1
while start < end:
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
def recursive_reverse(arr, start, end):
""" reverse a list using recursive approach """
if start < end:
arr[start], arr[end] = arr[end], arr[start]
recursive_reverse(arr, start + 1, end - 1)
def python_slicing_reverse(arr):
""" reverse list using python list slicing """
return arr[::-1]
def main():
""" operational function """
arr = [1, 2, 3, 4, 5, 6]
iterative_reverse(arr)
assert arr == [6, 5, 4, 3, 2, 1]
arr = [1, 2, 3, 4, 5]
recursive_reverse(arr, 0, len(arr) - 1)
assert arr == [5, 4, 3, 2, 1]
arr = [1, 2, 3, 4, 5]
arr = python_slicing_reverse(arr)
assert arr == [5, 4, 3, 2, 1]
if __name__ == '__main__':
main()