Python programs to display array elements in reverse order have been shown here. Both of the iterative and recursive approaches have been covered. The algorithm, pseudocode and time complexity of the programs have also been given.
Example:
Given array A[] = {10, 20, 30, 40}
Elements of A[] in reverse order = {40, 30, 20, 10}
In Python, we can also reverse an array using the reverse() method or the [::-1] slicing syntax.
Page content(s):
1. Algorithm to display array elements in reverse order
1. Take input of an array $A[~]$ and the size $n$
2. Set $i = n - 1$
3. For each $i$ from $n - 1$ to $0$, dislay $A[i]$
2. Pseudocode to display array elements in reverse order
Input: An array $A[~]$ and size of array $n$
Output: Array elements in reverse order.
1. Procedure reverse($A[~]$, $n$):
2.
3.
4. End Procedure
3. Time complexity to display array elements in reverse order
Time Complexity: O(n)
Here $n$ is the number of elements in the array.
4. Program & output to display array elements in reverse order
4.1. Python Program & output to display array elements in reverse order using iterative approach
# ******************************************** # alphabetacoder.com # Python program to print elements of array # in reverse order using iteration # ******************************************** # declare an array arr = [10, 20, 30, 40, 50, 60, 70, 80] # display the array print("Original array:", arr) # display array elements in reverse order print("Reverse array : ", end="") for i in range(len(arr) - 1, -1, -1): print(arr[i], end=" ")
Output
Original array: [10, 20, 30, 40, 50, 60, 70, 80]
Reverse array : 80 70 60 50 40 30 20 10
4.2. Python Program to display array elements in reverse order using recursion
# ******************************************** # alphabetacoder.com # Python program to print elements of array # in reverse order using recursion # ******************************************** # display elements of array in reverse # order function takes an array arr and # an index i as arguments def reverse(arr, i): # exit condition of recursive call if i < 0: return # print current element print(arr[i], end=" ") # call function reverse(arr, i - 1) def main(): # declare an array arr = [10, 20, 30, 40, 50, 60, 70, 80] # display original array print("Original array:", arr) # call function to display array in reverse print("Reverse array: ", end="") reverse(arr, len(arr) - 1) # driver code main()
4.3. Python Program to display array elements in reverse order using reverse() method
# ******************************************** # alphabetacoder.com # Python program to print elements of array # in reverse order using reverse() method # ******************************************** # declare an array arr = [10, 20, 30, 40, 50, 60, 70, 80] # display the array print("Original array:", arr) # reverse the array using reverse() mthod arr.reverse() # display reversed array print("Reverse array:", arr)
4.4. Python Program to display array elements in reverse order using Using slicing [::-1]
# ******************************************** # alphabetacoder.com # Python program to print elements of array # in reverse order using slicing [::-1] # ******************************************** # declare an array arr = [10, 20, 30, 40, 50, 60, 70, 80] # display the array print("Original array:", arr) # display reversed array using slicing [::-1] print("Reverse array:", arr[::-1])