Associate Professor of Computer Science 18SCS3101A DESIGN AND ANALYSIS OF ALGORITHMS III M Sc MCA ARRAY STRUCTURE ARRAY STRUCTURE What is an Array Group of elements are represented in a ID: 933365
Download Presentation The PPT/PDF document "Prof. V. S. Joe Irudayaraj" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Prof. V. S. Joe Irudayaraj
Associate Professor of Computer Science
18SCS3101A
DESIGN AND ANALYSIS OF ALGORITHMS
III
M Sc/ MCA
ARRAY STRUCTURE
Slide2ARRAY STRUCTURE
What is an Array? Group of elements are represented in a single name called an Array. Array is a set of pairs, index and value. Array has consecutive memory locations. Array is a random access structure. Array is a homogeneous structure, that is it consists same type of data.
Slide3ARRAY STRUCTURE
Operations on Array
1. Creation
2. Display
3. Updating4. Copying5. Joining
6. Concatenation7. Reversing8. Insertion
9. Deletion
10. Searching
11. Sorting
12. Merging
g
Slide4DECLARATION AND INITIALIZATION
DECLARATION IN ‘C’
int a[10], char c[5], char * name[10];
INITIALIZATION IN ‘C’ 1. int a[ ] = {10,20,30,40,50};2. char c[ ] = {‘A’,’B’,’C’};
2. char * name = {“RAJA”,”RANI”};
g
Slide5ARRAY CREATION
CREATE AN ARRAY OF ‘n’ ELEMENTS
ALGORITHM:
1. read n2. for i= 1 to n do
read a[i]
C PROGRAM:
1.
scanf
(“%
d”,&n);2. for(i
=1;i<=n;++i) scanf
(“%d”,&a[i] );
g
Slide6ARRAY DISPLAY
DISPLAY AN ARRAY OF ‘n’ ELEMENTS
ALGORITHM:
for i= 1 to n do write a[i]
C PROGRAM:
for(
i
=1;i<=n;++
i
) printf(“%d ”,a[i] );
g
Slide7ARRAY DISPLAY
Give number of elements
4
Give data one by one
10203040Given Data
10 20 30 40
g
Slide8ARRAY DISPLAY IN REVERSE ORDER
DISPLAY AN ARRAY IN REVERSE ORDER
ALGORITHM:
for i= n downto 1 do
write a[i]
C PROGRAM:
for(
i
=
n;i>=1;--i)
printf(“%d ”,a[i] );
g
Slide9ARRAY DISPLAY
Give number of elements
4
Give data one by one
10203040Given Data in reverse order
40 30 20 10
g
Slide10ARRAY UPDATION
REPLACE DATA ‘x’ AT LOCATION ’j’ WHERE 1<=j<=n
(GIVEN : ARRAY OF ‘N’ ELEMENTS)
1. read x,j2. a[j]=x
g
Slide11ARRAY DISPLAY
Given Array
10 20 30 40
Give new data to replace
80Give position3New Array10 20 80 40
g
Slide12COPYING
COPY ARRAY A OF ‘m’ elements TO ARRAY B
for i= 1 to m do b[i] = a[i]
g
Slide13JOINING
JOIN
ARRAY B OF ‘n’ elements
TO
ARRAY A of ‘m’ elements 1. for j= 1 to n do a[m+j
] = b[j]2. m = m+n
;
g
Slide14REVERSING
REVERSE AN ARRAY OF ‘n’ ELEMENTS
for
i= 1 to n/2 do a[i] ↔ a[n-i+1]
g
Slide15ARRAY CREATION and DISPLAY
CREATE and DISPLAY AN ARRAY OF ‘n’ ELEMENTS
ALGORITHM:
1. read n2. for i= 1 to n do
read a[i]
3. for i
= 1 to n do
write a[
i
]
g
Slide16ARRAY DISPLAY
Give number of elements
4
Give data one by one
10203040Given Array
10 20 30 40
g
Slide17REVERSING
REVERSE AN ARRAY OF ‘n’ ELEMENTS
for
i= 1 to n/2 do a[i] ↔ a[n-i+1]
g
Slide18ARRAY DISPLAY
Given Array
10 20 30 40
Reversed Array
40 30 20 10
g
Slide19ARRAY INSERTION
INSERT DATA ‘x’ AT LOCATION ’j’ WHERE 1<=j<=n
(GIVEN : ARRAY OF ‘n’ ELEMENTS)
1. read x2. read j3. for i
= n downto j do
a[i+1] = a[i]
4. a[j] = x
5. n = n+1
g
Slide20ARRAY INSERTION
Given Array
10 20 30 40
Give new data to insert
100Give its position2New Array after insertion
10 100 20 30 40
g
Slide21ARRAY INSERTION
Given Array
10 20 30 40
Give new data to insert
100Give its position1New Array after insertion
100 10 20 30 40
g
Slide22ARRAY INSERTION
Given Array
10 20 30 40
Give new data to insert
100Give its position5New Array after insertion
10 20 30 40 100
g
Slide23ARRAY INSERTION
INSERT DATA ‘x’ AT THE END OF AN ARRAY
(GIVEN : ARRAY OF ‘n’ ELEMENTS)
1. read x2. a[ n + 1] = x3. n = n+1
g
Slide24ARRAY INSERTION
Given Array
10 20 30 40
Give new data to insert at rear
100New Array after insertion10 20 30 40 100
g
Slide25ARRAY DELETION
DELETE DATA AT LOCATION ’j’ WHERE 1<=j<=n
(GIVEN : ARRAY OF ‘n’ ELEMENTS)
1. read j2. for i = j+1 to n do a[i-1] = a[
i] 3. n = n-1
g
Slide26ARRAY DELETION
Given Array
10 20 30 40
Give data to delete
30New Array after deletion10 20 40
g
Slide27ARRAY DELETION
Given Array
10 20 30 40
Give data to delete
40New Array after deletion10 20 30
g
Slide28ARRAY DELETION
Given Array
10 20 30 40
Give data to delete
10New Array after deletion20 30 40
g
Slide29ARRAY DELETION
DELETE DATA ‘x’ FROM AN ARRAY OF ‘n’ ELEMENTS
1. read x
2. for i = 1 to n do if (a[i]=x) break 3. if (
i=n+1) then {write(‘Not Found’); return;} else { for j= i+1 to n do
a[j-1] = a[j] n=n-1 }
g
Slide30MERGING
MERGE TWO SORTED ARRAYS
ARRAY A OF ‘m’ ELEMENTS
WITH
ARRAY B OF ‘n’ ELEMENTS i=1; j=1a[m+1] = ∞ ; a[n+1]= ∞
for k= 1 to m+n do { if (a[
i] <= b[j]) then {c[k]=a[i]; i
=i+1}
else {c[k]=b[j]; j=j+1}
}
g
Slide31MATRIX: MAGIC SQUARE
(4i, i = 1, 2, 3, …) n (n2
+1)Sum = -------------- 2Try for Singly Even Magic Square (4i+2, i =1, 2, 3, …)
162*
3*135*11108*9*
76
12*4
14*
15*
1
Slide328
16
35
7
ODD MAGIC SQUARE (2i, i = 1, 2, 3, …)492