Python code to Reverse an Array using Different Methods

In this blog post, you can learn the different methods of Python code to Reverse an Array.

Single line of Python code to Reverse an Array:

arr = [1, 2, 3, 4, 5]
print(arr[::-1])  # Output: [5, 4, 3, 2, 1]

or

arr = [1, 2, 3, 4, 5]
reversed_arr = arr[::-1]
print(reversed_arr)  # Output: [5, 4, 3, 2, 1]

This code uses slicing to create a new array with the elements of the original array in reverse order. The [::-1] part of the slice syntax specifies that the elements should be taken from the beginning of the array ([:]) to the end ([:]) in reverse order (-1).

The time complexity and space complexity of the single-line code reversed_arr = arr[::-1] to reverse an array in Python are both O(n), where n is the length of the original array.

Time Complexity:

The slicing operation arr[::-1] operates on the original array "in-place," meaning it doesn't explicitly create a new copy of the entire array. However, it still needs to iterate through all elements of the original array to determine the reversed order. This iteration takes O(n) time.

Space Complexity:

Although the slice doesn't create a full copy, it might create some internal data structures for representing the reversed view. In Python, this is usually optimized efficiently, and the additional space used remains constant regardless of the array size. Therefore, the space complexity is considered O(1), meaning it doesn't grow significantly with the input size.

Python code to Reverse an Array without using built-in functions:

def reverse_array(arr):
  left = 0
  right = len(arr) - 1
  while left < right:
    arr[left], arr[right] = arr[right], arr[left]
    left += 1
    right -= 1

# Example usage
arr = [1, 2, 3, 4, 5]
reverse_array(arr)
print(arr)  # Output: [5, 4, 3, 2, 1]

This code uses a two-pointer approach to swap elements from opposite ends of the array until the middle is reached, effectively reversing the order. It modifies the original array in-place, making it efficient in terms of space complexity.

Python code to reverse an array using an Extra List:

def reverse_array(arr):
  reversed_arr = []
  for i in range(len(arr) - 1, -1, -1):
    reversed_arr.append(arr[i])
  return reversed_arr

# Example usage
arr = [1, 2, 3, 4, 5]
reversed_arr = reverse_array(arr)
print(reversed_arr)  # Output: [5, 4, 3, 2, 1]

This code iterates through the original array in reverse order and appends each element to a new list, effectively creating a reversed copy of the original array.

This approach has a time complexity of O(n) due to the iteration through the original array and a space complexity of O(n) due to the creation of a new list. It's a simple and readable way to reverse an array while keeping the original array unmodified.

Python code to Reverse an Array using Recursion:

def reverse_array(arr, start=0):
  if start >= len(arr) // 2:
    return
  arr[start], arr[-start - 1] = arr[-start - 1], arr[start]
  reverse_array(arr, start + 1)

# Example usage
arr = [1, 2, 3, 4, 5]
reverse_array(arr)
print(arr)  # Output: [5, 4, 3, 2, 1]

This approach has a time complexity of O(n), as each element is visited and swapped once on average during the recursive calls. However, it has a space complexity of O(n) due to the function call overhead for each recursive level.

Remember that recursion can be less memory-efficient compared to iterative approaches, so consider its trade-offs based on your specific needs and problem size.