1 Array Order Reversal
1 Array Order Reversal
POTLURI SIDDHARTHA
INSTITUTE OF TECHNOLOGY
PROBLEM SOLVING
TECHNIQUES
By
B. Vinay Kumar
Assistant Professor
Dept. of CSE
PVPSIT, Kanuru.
UNIT III PVPSIT (Autonomous)
ARRAY TECHNIQUES
• The array is a powerful tool that is used in computing.
• Arrays provide for a very special way of storing or organizing data in a
computer’s memory.
• The power of the array is performing computations on collections of data
share some common attribute.
• Example: There is a street with row of houses. The address is made up of two
parts, the street name and number of house.
• The street name is array name and house number is suffix/index.
• 49 Keara (street)
• Keara [49] (array)
Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025
PVPSIT (Autonomous)
• Problem
• Rearrange the elements in an array so that they appear in reverse order.
• Algorithm development
• The problem of reversing the order of an array of numbers appears to be
completely straightforward.
• We can start the design of this algorithm by careful examination of the
elements of an array before and after it has been reversed;
• For example
• Pseudo-code:
• initialize an array a[1..n] of n elements to be reversed
• begin
r := n div 2
for i := 1 to r do
begin
t := a[i];
a[i] := a[n-i+1]
a[n-i+1] := t
end
•end
Algorithm description
Algorithm
• Step 0: Start
• Step 1: Read N
• Step 2: Initialize suffix/index I to 1
• Step 3: Repeat Step 4, 5 and 6 till I<=N
• Step 4: Read Element, E
• Step 5: Store E in A[I] (A[I] = E)
• Step 6: Increment Array Index, I (I = I+1)
• Step 7: Reset Index, I (I :=1)
• Step 8: Compute R ( R = N/2)
• Notes on design
1. To reverse an array of n elements exchanges are required.
2. An example makes it easier to establish how many pairs of exchanges are
required in general for both even and odd array sizes.
3. There is a simpler algorithm for an array reversal that starts with two indices,
i=0 and j=n+1. With each iteration I is increased and j is decreased for i<j.
• Applications
Vector and matrix processing
Solutions
4.1.2 Implement the array reversal algorithm starts out with two indices.
• Algorithm description
1. Establish the array a[1..n] of n elements to be reversed.
2. Set lower index i to 1 and upper index j to n.
3. Compute r the number of exchanges needed to reverse the array.
4. While i is less than j do
(a) exchange the ith element with the jth element.
• Pseudo-code:
• initialize an array a[1..n] of n elements to be reversed
• begin
i := 1;
j := n;
while i < j do
begin
t := a[i];
a[i] := a[j]
a[j] := t
end
•end
4.1.3 Design an algorithm that places the kth element of an array in position 1,
the (k+1)th element in position 2, etc. the original 1st element is placed at (n-
k+1) and so on.
• Algorithm description