DEV Community

Abirami Prabhakar
Abirami Prabhakar

Posted on

To Reverse an array without function

To carry out reversing a array without function considering all the approaches in the refernce link https://www.geeksforgeeks.org/write-a-program-to-reverse-an-array-or-string/?authuser=2

I took interest in the two pointer approach as it was about swapping the
left -> end
right -> start

even when there are odd number of events we can leave the middle element untouched and it has a very simple ideology and can be implemented with simple swap functions and very efficent in terms of time and space constraint in comparision to the other approaches is also very beginner friendly

the code block I used was :

# initilize the value of left pointer as the first index value and  right pointer as the last index value
def reversearray(arr):
    left = i
    right = len(arr)-1
    while left < right:
        arr[left],arr[right] = arr[right],arr[left]
        #the index value in increased in right and decreased in right
        left = left +1
        right = right - 1
    return arr

print(reversearray([4, 5, 1, 2])

Enter fullscreen mode Exit fullscreen mode

Top comments (0)