0% found this document useful (0 votes)
2 views16 pages

1 Array Order Reversal

The document discusses array techniques, emphasizing their importance in data organization and manipulation in computing. It details the algorithm for reversing an array, including pseudo-code and step-by-step instructions for implementation. Additionally, it presents supplementary problems related to array manipulation and their solutions.

Uploaded by

clanguage9999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views16 pages

1 Array Order Reversal

The document discusses array techniques, emphasizing their importance in data organization and manipulation in computing. It details the algorithm for reversing an array, including pseudo-code and step-by-step instructions for implementation. Additionally, it presents supplementary problems related to array manipulation and their solutions.

Uploaded by

clanguage9999
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

PRASAD V.

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)

• The concept of a one-dimensional array which we have been considering


extends in a natural way to multidimensional arrays.
• Array location can be accessed by its index value, i.e., a[49].
• The most important basic ways in which we change the contents of an array
location are by direct computation and assignment, by exchanging of
contents of two array locations, and by counting.
• Arrays are used to simulate stacks and queues and other important data
structures.
• Non-linear data structures such as trees and graphs can also be maintained
using one-dimensional and two-dimensional arrays.

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


ARRAY ORDER REVERSAL 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

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

• First element exchange/swap with last element, second element


exchange/swap with second last element and so on.

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

• After step [3] the array is completely reversed.


• Suffixes on the left are increasing by one while at the same time the suffixes
on the right are decreasing by one.
• In setting up our algorithm we need a pair of suffixes that model this
increasing-decreasing behavior.
• The suffix [n-i+1] can be used for decreasing suffix.

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

• Each exchange can be achieved by a mechanism of the form


t := a[i];
a[i] := a[n-i+1];
a[n-i+1] := t
• Here we need to find out “r” the number of exchanges needed to
reverse the order of elements in an array.
• r := floor(n div 2)

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

• 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

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

Algorithm description

1. Establish the array a[1..n] of n elements to be reversed.


2. Compute r the number of exchanges needed to reverse the array.
3. While there are still pairs of array elements to be exchanged
(a) exchange the ith element with the [n-i+1]th element.

4. Return the reversed array

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

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)

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

• Step 9: Repeat Step 10, 11, 12 and 13 till I<=R


• Step 10: T := A[I]
• Step 11: A[I] := A[N-I+1]
• Step 12: A[N-I+1]:=T
• Step 13: I := I+1
• Step 14: Reset I (I:=1)
• Step 15: Repeat Step 16 and 17 till I <= N
• Step 16: Display A[I]
• Step 17: I := I+1
• Step 18: Stop

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

• 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

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

• Supplementary problems (4.1.2, 4.1.3)


• 4.1.2 Implement the array reversal algorithm starts out with two
indices.
• 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.

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

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.

5. Return the reversed array

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

• 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

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025


PVPSIT (Autonomous)

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

Problem Solving Techniques B. Vinay Kumar Friday, June 27, 2025

You might also like