-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverse_Array.py
More file actions
32 lines (26 loc) · 804 Bytes
/
Reverse_Array.py
File metadata and controls
32 lines (26 loc) · 804 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
"""
File: Reverse_Array.py
Description: Implementation of array reversal algorithm using two-pointer technique
Author: Madhu Kumar K S
"""
def reverse_array(arr):
"""
Reverse an array in-place using two-pointer technique
Args:
arr: List of elements to be reversed
Returns:
List: The reversed array
"""
# Initialize pointers at start and end of array
left, right = 0, len(arr)-1
# Swap elements from both ends moving towards center
while left < right :
# Swap elements at left and right positions
arr[left], arr[right] = arr[right], arr[left]
# Move pointers towards center
left +=1
right -=1
return arr
# Test the function with sample array
arr = [1,2,3,4,5,6]
print(reverse_array(arr))