Important Notice:

Reverse Order of Elements of an Array

Reverse Order of Elements of an Array

6 views 2 min read

Reverse Order of Elements of an Array (ऐरे के तत्वों को उल्टे क्रम में व्यवस्थित करना)

Question - Write an algorithm to reverse the order of elements of an array.

किसी Array के Elements को उल्टे क्रम (Reverse Order) में व्यवस्थित करने के लिए Algorithm लिखिए।

Example -

Input Array:

A = [10, 20, 30, 40, 50]

Output Array:

A = [50, 40, 30, 20, 10]

 

Solution - Algorithm: Reverse Order of Elements of an Array

Step 1: Start.

Step 2: Input the number of elements N.

Step 3: Input the array elements.

Step 4: Set i = 0 (First Index).

Step 5: Set j = N − 1 (Last Index).

Step 6: Swap A[i] and A[j].

Step 7: Set i = i + 1.

Step 8: Set j = j − 1.

Step 9: Repeat Steps 6 to 8 while i < j.

Step 10: Display the reversed array.

Step 11: Stop.

Logic / Working (कार्यप्रणाली) -

  • i = 0 सेट करें, अर्थात पहले Element से शुरुआत करें। — Set i = 0, i.e., start from the first element.
  • j = N − 1 सेट करें, अर्थात अंतिम Element से शुरुआत करें। — Set j = N − 1, i.e., start from the last element.
  • A[i] और A[j] की आपस में अदला-बदली (Swap) करें। — Swap A[i] and A[j], i.e., exchange the first and last elements.
  • i = i + 1 करें, अर्थात बाएँ Pointer को एक स्थान आगे बढ़ाएँ। — Set i = i + 1, i.e., move the left pointer one position forward.
  • j = j − 1 करें, अर्थात दाएँ Pointer को एक स्थान पीछे लाएँ। — Set j = j − 1, i.e., move the right pointer one position backward.
  • जब तक i < j हो, उपरोक्त Steps को दोहराएँ। — Repeat the above steps while i < j.
  • अंत में Reverse Array को Display करें। — Finally, display the reversed array.

Flow Chart -

Reverse Order of Elements of an Array

Related Notes