/
Prof. V. S. Joe Irudayaraj Prof. V. S. Joe Irudayaraj

Prof. V. S. Joe Irudayaraj - PowerPoint Presentation

KissableLips
KissableLips . @KissableLips
Follow
346 views
Uploaded On 2022-08-03

Prof. V. S. Joe Irudayaraj - PPT Presentation

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

data array give elements array data elements give display read insertion insert deletion delete structure reverse algorithm write 100

Share:

Link:

Embed:

Download Presentation from below link

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.


Presentation Transcript

Slide1

Prof. V. S. Joe Irudayaraj

Associate Professor of Computer Science

18SCS3101A

DESIGN AND ANALYSIS OF ALGORITHMS

III

M Sc/ MCA

ARRAY STRUCTURE

Slide2

ARRAY 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.

Slide3

ARRAY 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

Slide4

DECLARATION 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

Slide5

ARRAY 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

Slide6

ARRAY 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

Slide7

ARRAY DISPLAY

Give number of elements

4

Give data one by one

10203040Given Data

10 20 30 40

g

Slide8

ARRAY 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

Slide9

ARRAY DISPLAY

Give number of elements

4

Give data one by one

10203040Given Data in reverse order

40 30 20 10

g

Slide10

ARRAY UPDATION

REPLACE DATA ‘x’ AT LOCATION ’j’ WHERE 1<=j<=n

(GIVEN : ARRAY OF ‘N’ ELEMENTS)

1. read x,j2. a[j]=x

g

Slide11

ARRAY DISPLAY

Given Array

10 20 30 40

Give new data to replace

80Give position3New Array10 20 80 40

g

Slide12

COPYING

COPY ARRAY A OF ‘m’ elements TO ARRAY B

for i= 1 to m do b[i] = a[i]

g

Slide13

JOINING

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

Slide14

REVERSING

REVERSE AN ARRAY OF ‘n’ ELEMENTS

for

i= 1 to n/2 do a[i] ↔ a[n-i+1]

g

Slide15

ARRAY 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

Slide16

ARRAY DISPLAY

Give number of elements

4

Give data one by one

10203040Given Array

10 20 30 40

g

Slide17

REVERSING

REVERSE AN ARRAY OF ‘n’ ELEMENTS

for

i= 1 to n/2 do a[i] ↔ a[n-i+1]

g

Slide18

ARRAY DISPLAY

Given Array

10 20 30 40

Reversed Array

40 30 20 10

g

Slide19

ARRAY 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

Slide20

ARRAY INSERTION

Given Array

10 20 30 40

Give new data to insert

100Give its position2New Array after insertion

10 100 20 30 40

g

Slide21

ARRAY INSERTION

Given Array

10 20 30 40

Give new data to insert

100Give its position1New Array after insertion

100 10 20 30 40

g

Slide22

ARRAY INSERTION

Given Array

10 20 30 40

Give new data to insert

100Give its position5New Array after insertion

10 20 30 40 100

g

Slide23

ARRAY 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

Slide24

ARRAY INSERTION

Given Array

10 20 30 40

Give new data to insert at rear

100New Array after insertion10 20 30 40 100

g

Slide25

ARRAY 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

Slide26

ARRAY DELETION

Given Array

10 20 30 40

Give data to delete

30New Array after deletion10 20 40

g

Slide27

ARRAY DELETION

Given Array

10 20 30 40

Give data to delete

40New Array after deletion10 20 30

g

Slide28

ARRAY DELETION

Given Array

10 20 30 40

Give data to delete

10New Array after deletion20 30 40

g

Slide29

ARRAY 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

Slide30

MERGING

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

Slide31

MATRIX: 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

Slide32

8

16

35

7

ODD MAGIC SQUARE (2i, i = 1, 2, 3, …)492