SlideShare une entreprise Scribd logo
1  sur  49
Data Structure and Algorithm
CS-102
Ashok K Turuk

1
Data Structure
vs
Storage Structure
• Data Structure : The logical or mathematical
model of a particular organization of data
• Storage Structure : Representation of a
particular data structure in the memory of a
computer
• There are many possible storage structure to a
particular data structure

• Ex: there are a number of storage structure for a data
structure such as array

– It is possible for two DS to represented by the
same storage structure

2
Classification
• Data Structure
– Linear
– Non-Linear

• A Data structure is said to be linear if
its elements from a sequence or in other
words form a linear list

3
Representation in Memory
• Two basic representation in memory
– Have a linear relationship between the
elements represented by means of
sequential memory locations [ Arrays]
– Have the linear relationship between the
elements represented by means of pointer
or links [ Linked List]

4
Operation on Linear Structure
• Traversal : Processing each element in the list
• Search : Finding the location of the element
with a given value or the record with a given
key
• Insertion: Adding a new element to the list
• Deletion: Removing an elements from the list
• Sorting : Arranging the elements in some type
of order
• Merging : Combining two list into a single list

5
Array

6
Linear Arrays
• A linear array is a list of a finite
number of n homogeneous data
elements ( that is data elements of the
same type) such that
– The elements are of the arrays are
referenced respectively by an index set
consisting of n consecutive numbers
– The elements of the arrays are stored
respectively in successive memory
locations

7
Linear Arrays
• The number n of elements is called the
length or size of the array.
• The index set consists of the integer 1,
2, … n
• Length or the number of data elements of
the array can be obtained from the index
set by
Length = UB – LB + 1 where UB is the
largest index called the upper bound and
LB is the smallest index called the lower
bound of the arrays
8
Linear Arrays
• Element of an array A may be denoted
by

– Subscript notation A1, A2, , …. , An
– Parenthesis notation A(1), A(2), …. , A(n)
– Bracket notation A[1], A[2], ….. , A[n]

• The number K in A[K] is called subscript
or an index and A[K] is called a
subscripted variable
9
Representation of Linear Array in Memory

1000
1001
1002
1003
1004

1005

:
Computer Memory

10
Representation of Linear Array in Memory
• Let LA be a linear array in the memory of
the computer
• LOC(LA[K]) = address of the element
LA[K] of the array LA
• The element of LA are stored in the
successive memory cells
• Computer does not need to keep track of
the address of every element of LA, but
need to track only the address of the first
element of the array denoted by Base(LA)
called the base address of LA
11
Representation of Linear Array in Memory
• LOC(LA[K]) = Base(LA) + w(K – lower
bound) where w is the number of words
per memory cell of the array LA [w is
aka size of the data type]

12
Example 1
LA[1]

201

LA[2]

202

LA[3]

203

LA[4]

204

LA[5]

205

LA[6]

206

LA[7]

207

Find the address for LA[6]
Each element of the array
occupy 1 byte

200

LA[8]

LOC(LA[K]) = Base(LA) + w(K – lower bound)

:

LOC(LA[6]) = 200 + 1(6 – 1) = 205
13
Example 2

Find the address for LA[16]
Each element of the array
occupy 2 byte

200

LA[1]

201
202

LA[2]

203
204

LA[3]

205
206
207
LOC(LA[K]) = Base(LA) + w(K – lower bound)

LA[4]

:

LOC(LA[16]) = 200 + 2(16 – 1) = 230
14
Representation of Linear Array in Memory
• Given any value of K, time to calculate
LOC(LA[K]) is same
• Given any subscript K one can access and
locate the content of LA[K] without
scanning any other element of LA
• A collection A of data element is said to be
index if any element of A called Ak can be
located and processed in time that is
independent of K
15
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••

16
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••

17
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••

18
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••

19
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••

20
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••

21
Traversing Linear Arrays
• Traversing is accessing and processing
(aka visiting ) each element of the data
structure exactly ones
Linear Array
•••

1. Repeat for K = LB to UB
Apply PROCESS to LA[K]
[End of Loop]
2. Exit

22
Inserting and Deleting
• Insertion: Adding an element
– Beginning
– Middle
– End

• Deletion: Removing an element
– Beginning
– Middle
– End
23
Insertion
1

Brown

1

Brown

2

Davis

2

Davis

3

Johnson

3

Johnson

4

Smith

4

Smith

5

Wagner

5

Wagner

6

6

Ford

7

7

8

8

Insert Ford at the End of Array

24
Insertion
1

Brown

1

Brown

2

Davis

2

Davis

3

Johnson

3

Johnson

4

Smith

4

Smith

5

Wagner

5

6

6

7

7

8

8

Wagner

1

Brown

1

Brown

2

Davis

2

Davis

3

Johnson

3

Ford

4

Johnson

4
5

Smith

5

Smith

6

Wagner

6

Wagner

7

7

8

8

Insert Ford as the 3rd Element of Array

Insertion is not Possible without loss
of data if the array is FULL
25
Deletion
1

Brown

1

Brown

2

Davis

2

Davis

3

Ford

3

Ford

4

Johnson

4

Johnson

5

Smith

5

Smith

6

Taylor

6

Taylor

7

Wagner

7

8

8

Deletion of Wagner at the End of Array

26
Deletion
1

Brown

1

2

Davis

2

3

Ford

3

Ford

3

4

Johnson

4

Johnson

4

Johnson

5

Smith

5

Smith

5

Smith

6

Taylor

6

Taylor

6

Taylor

7

Wagner

7

Wagner

7

Wagner

8

8

Brown

1

Brown

2

Ford

8

1

Brown

2

Ford

3

Johnson

4

5

Smith

6

Taylor

7

Wagner

8

Deletion of Davis from the Array

27
Deletion
1

Brown

2

Ford

3

Johnson

4

Smith

5

Taylor

6

Wagner

7
8

No data item can be deleted from an empty
array
28
Insertion Algorithm
• INSERT (LA, N , K , ITEM)

[LA is a
linear array with N elements and K is a positive
integers such that K ≤ N. This algorithm insert an
element ITEM into the Kth position in LA ]
1. [Initialize Counter] Set J := N
2. Repeat Steps 3 and 4 while J ≥ K
3. [Move the Jth element downward ] Set LA[J + 1]
:= LA[J]
4. [Decrease Counter] Set J := J -1
5 [Insert Element] Set LA[K] := ITEM
6. [Reset N] Set N := N +1;
7. Exit
29
Deletion Algorithm
• DELETE (LA, N , K , ITEM)

[LA is a
linear array with N elements and K is a positive
integers such that K ≤ N. This algorithm deletes Kth
element from LA ]
1. Set ITEM := LA[K]
2. Repeat for J = K to N -1:
[Move the J + 1st element upward] Set LA[J]
:= LA[J + 1]
3. [Reset the number N of elements] Set N := N - 1;
4. Exit

30
Multidimensional Array
•
•
•
•

One-Dimensional Array
Two-Dimensional Array
Three-Dimensional Array
Some programming Language allows as
many as 7 dimension

31
Two-Dimensional Array
• A Two-Dimensional m x n array A is a
collection of m . n data elements such
that each element is specified by a pair
of integer (such as J, K) called
subscript with property that
1 ≤ J ≤ m and 1 ≤ K ≤ n
The element of A with first subscript J
and second subscript K will be denoted
by AJ,K or A[J][K]
32
2D Arrays
The elements of a 2-dimensional array a
is shown as below

a[0][0]
a[1][0]
a[2][0]

a[0][1]
a[1][1]
a[2][1]

a[0][2]
a[1][2]
a[2][2]

a[0][3]
a[1][3]
a[2][3]
Rows Of A 2D Array
a[0][0]
a[1][0]
a[2][0]

a[0][1]
a[1][1]
a[2][1]

a[0][2]
a[1][2]
a[2][2]

a[0][3]
a[1][3]
a[2][3]

row 0
row 1
row 2
Columns Of A 2D Array
a[0][0]
a[1][0]
a[2][0]

a[0][1]
a[1][1]
a[2][1]

a[0][2]
a[1][2]
a[2][2]

a[0][3]
a[1][3]
a[2][3]

column 0

column 1

column 2 column 3
2D Array
• Let A be a two-dimensional array m x n
• The array A will be represented in the
memory by a block of m x n sequential
memory location
• Programming language will store array A
either

– Column by Column (Called Column-Major
Order) Ex: Fortran, MATLAB
– Row by Row (Called Row-Major Order) Ex: C,
C++ , Java
36
2D Array in Memory
A

Subscript
(1,1)
(2,1)

(3,1)

A

Subscript
(1,1)

Column 1

(1,2)
(2,2) Column 2
(3,2)
(1,3)
(2,3) Column 3
(3,3)
(1,4)

(2,4) Column 4
(3,4)
Column-Major Order

(1,2)
(1,3)

Row 1

(1,4)
(2,1)
(2,2)
(2,3)

Row 2

(2,4)
(3,1)
(3,2)
(3,3)

Row 3

(3,4)
Row-Major Order

37
2D Array
• LOC(LA[K]) = Base(LA) + w(K -1)
• LOC(A[J,K]) of A[J,K]
Column-Major Order
LOC(A[J,K]) = Base(A) + w[m(K-1) + (J-1)]

Row-Major Order
LOC(A[J,K]) = Base(A) + w[n(J-1) + (K-1)]

38
2D Array Example
• Consider a 25 x 4 array A. Suppose the
Base(A) = 200 and w =4. Suppose the
programming store 2D array using rowmajor. Compute LOC(A[12,3])
• LOC(A[J,K]) = Base(A) + w[n(J-1) + (K-1)]

• LOC(A[12,3]) = 200 + 4[4(12-1) + (3 -1)]
= 384

39
Multidimensional Array
• An n-dimensional m1 x m2 x …. X mn array B
is a collection of m1.m2…mn data elements
in which each element is specified by a list
of n integers – such as K1, K2, …., Kn – called
subscript with the property that
1≤K1≤m1, 1≤K2≤m2, …. 1≤Kn≤mn
The Element B with subscript K1, K2, …,Kn will
be denoted by
BK1,K2, …,Kn
or B[K1,K2,….,Kn]
40
Multidimensional Array
• Let C be a n-dimensional array
• Length Li of dimension i of C is the
number of elements in the index set
Li = UB – LB + 1
• For a given subscript Ki, the effective
index Ei of Li is the number of indices
preceding Ki in the index set
Ei = Ki – LB
41
Multidimensional Array
• Address LOC(C[K1,K2, …., Kn]) of an
arbitrary element of C can be obtained as
Column-Major Order
Base( C) + w[((( … (ENLN-1 + EN-1)LN-2)
+ ….. +E3)L2+E2)L1+E1]
Row-Major Order
Base( C) + w[(… ((E1L2 + E2)L3 + E3)L4
+ ….. +EN-1)LN +EN]
42
Example
• MAZE(2:8, -4:1, 6:10)
• Calculate the address of MAZE[5,-1,8]
• Given: Base(MAZE) = 200, w = 4, MAZE
is stored in Row-Major order
• L1 = 8-2+1 = 7, L2 = 6, L3 = 5
• E1 = 5 -2 = 3, E2 = 3, E3 = 2

43
Example Contd ..
• Base( C) + w[(… ((E1L2 + E2)L3 + E3)L4 +
….. +EN-1)LN +EN]
• E1L2 = 3 . 6 = 18
• E1L2 + E2 = 18 + 3 = 21
• (E1L2 + E2)L3 = 21 . 5 = 105
• (E1L2+E2)L3 + E3 = 105 + 2 = 107
• MAZE[5,-1,8] = 200 + 4(107) = 200 +
248 = 628
44
Pointer, Pointer Array
• Let DATA be any array
• A variable P is called a pointer if P
points to an element in DATA i.e if P
contains the address of an element in
DATA
• An array PTR is called a pointer array if
each element of PTR is a pointer

45
Group 1

Group 2

Group 3 Group 4

Evan

Conrad

Davis

Baker

Harris

Felt

Segal

Cooper

Lewis

Glass

Ford

Shaw

Hill

Gray

Penn

Jones

Silver

Reed

Two Dimensional
4x9 or 9x4 array

Troy
Wagner
King

46
1

Evan

2

Harris

3

Lewis

4

Shaw

5

Conrad

:

:

13

Wagner

14

Davis

15

Segal

16

Baker

:

:

21

Reed

Group 1

Group 2
Group 3
Group 4

47
1

Evan

:

:

4

Shaw

5

$$$

6

Conrad

:

:

14

Wagner

15

$$$

16

Davis

17

Segal

18

$$$

19

Baker

:

:

24

Reed

Group 1

Group are not index
Group 2 in this
representation
Group 3

Group 4
48
1

Evan

2

Harris

3

Lewis

1 1

4

Shaw

2 5

5

Conrad

:

:

Group

3 14
4 16
5 22

Group 1

Group 2

13 Wagner
14 Davis
15 Segal

Group 3

16 Baker
:

:

Group 4

21 Reed
22 $$$

49

Contenu connexe

Tendances

Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | EdurekaEdureka!
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresPriyanka Rana
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)Elavarasi K
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursionAbdullah Al-hazmy
 
PRIM’S AND KRUSKAL’S ALGORITHM
PRIM’S AND KRUSKAL’S  ALGORITHMPRIM’S AND KRUSKAL’S  ALGORITHM
PRIM’S AND KRUSKAL’S ALGORITHMJaydeepDesai10
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexityAnkit Katiyar
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithmsJulie Iskander
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsMohamed Loey
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 
Adjacency list
Adjacency listAdjacency list
Adjacency listStefi Yu
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its typesRameesha Sadaqat
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithmAamir Sohail
 

Tendances (20)

Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
Java Linked List Tutorial | Edureka
Java Linked List Tutorial |  EdurekaJava Linked List Tutorial |  Edureka
Java Linked List Tutorial | Edureka
 
Recursion - Algorithms and Data Structures
Recursion - Algorithms and Data StructuresRecursion - Algorithms and Data Structures
Recursion - Algorithms and Data Structures
 
Insertion sort algorithm power point presentation
Insertion  sort algorithm power point presentation Insertion  sort algorithm power point presentation
Insertion sort algorithm power point presentation
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Big o notation
Big o notationBig o notation
Big o notation
 
Data Structures- Part5 recursion
Data Structures- Part5 recursionData Structures- Part5 recursion
Data Structures- Part5 recursion
 
Tree in data structure
Tree in data structureTree in data structure
Tree in data structure
 
PRIM’S AND KRUSKAL’S ALGORITHM
PRIM’S AND KRUSKAL’S  ALGORITHMPRIM’S AND KRUSKAL’S  ALGORITHM
PRIM’S AND KRUSKAL’S ALGORITHM
 
Time and space complexity
Time and space complexityTime and space complexity
Time and space complexity
 
Searching
SearchingSearching
Searching
 
Data structures and algorithms
Data structures and algorithmsData structures and algorithms
Data structures and algorithms
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 
Data structure ppt
Data structure pptData structure ppt
Data structure ppt
 
Analysis of algorithm
Analysis of algorithmAnalysis of algorithm
Analysis of algorithm
 
Array data structure
Array data structureArray data structure
Array data structure
 
Adjacency list
Adjacency listAdjacency list
Adjacency list
 
Binary search
Binary searchBinary search
Binary search
 
Data structure & its types
Data structure & its typesData structure & its types
Data structure & its types
 
Hash table in data structure and algorithm
Hash table in data structure and algorithmHash table in data structure and algorithm
Hash table in data structure and algorithm
 

En vedette

Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsAakash deep Singhal
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsAakash deep Singhal
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsAakash deep Singhal
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURESbca2010
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsAakash deep Singhal
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsAakash deep Singhal
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsAakash deep Singhal
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory MappingQundeel
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsAakash deep Singhal
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsAakash deep Singhal
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsAakash deep Singhal
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsAbdullah Al-hazmy
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programmingSachin Sharma
 
មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++Ngeam Soly
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 

En vedette (19)

Lecture 2 data structures and algorithms
Lecture 2 data structures and algorithmsLecture 2 data structures and algorithms
Lecture 2 data structures and algorithms
 
Lecture 4 data structures and algorithms
Lecture 4 data structures and algorithmsLecture 4 data structures and algorithms
Lecture 4 data structures and algorithms
 
Lecture 5 data structures and algorithms
Lecture 5 data structures and algorithmsLecture 5 data structures and algorithms
Lecture 5 data structures and algorithms
 
DATA STRUCTURES
DATA STRUCTURESDATA STRUCTURES
DATA STRUCTURES
 
Lecture 6 data structures and algorithms
Lecture 6 data structures and algorithmsLecture 6 data structures and algorithms
Lecture 6 data structures and algorithms
 
12111 data structure
12111 data structure12111 data structure
12111 data structure
 
Lecture 16 data structures and algorithms
Lecture 16 data structures and algorithmsLecture 16 data structures and algorithms
Lecture 16 data structures and algorithms
 
Lecture 15 data structures and algorithms
Lecture 15 data structures and algorithmsLecture 15 data structures and algorithms
Lecture 15 data structures and algorithms
 
02 Arrays And Memory Mapping
02 Arrays And Memory Mapping02 Arrays And Memory Mapping
02 Arrays And Memory Mapping
 
Lecture 9 data structures and algorithms
Lecture 9 data structures and algorithmsLecture 9 data structures and algorithms
Lecture 9 data structures and algorithms
 
Lecture 13 data structures and algorithms
Lecture 13 data structures and algorithmsLecture 13 data structures and algorithms
Lecture 13 data structures and algorithms
 
Lecture 14 data structures and algorithms
Lecture 14 data structures and algorithmsLecture 14 data structures and algorithms
Lecture 14 data structures and algorithms
 
Arrays C#
Arrays C#Arrays C#
Arrays C#
 
Subsidence in coal mines
Subsidence in coal minesSubsidence in coal mines
Subsidence in coal mines
 
Data Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithmsData Structures- Part3 arrays and searching algorithms
Data Structures- Part3 arrays and searching algorithms
 
Basic concepts of object oriented programming
Basic concepts of object oriented programmingBasic concepts of object oriented programming
Basic concepts of object oriented programming
 
Oops ppt
Oops pptOops ppt
Oops ppt
 
មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++មេរៀនៈ Data Structure and Algorithm in C/C++
មេរៀនៈ Data Structure and Algorithm in C/C++
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 

Similaire à Lecture 3 data structures and algorithms

2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHISowmya Jyothi
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...nsitlokeshjain
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structurekalyanineve
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in Onejehan1987
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresmidtushar
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxcoc7987515756
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd yearpalhimanshi999
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyMalikireddy Bramhananda Reddy
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTSwapnil Mishra
 
COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5Vishal Patil
 

Similaire à Lecture 3 data structures and algorithms (20)

Arrays
ArraysArrays
Arrays
 
2.DS Array
2.DS Array2.DS Array
2.DS Array
 
arrays in data structure.pptx
arrays in data structure.pptxarrays in data structure.pptx
arrays in data structure.pptx
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHIBCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
cluod.pdf
cluod.pdfcluod.pdf
cluod.pdf
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...Basic of array and data structure, data structure basics, array, address calc...
Basic of array and data structure, data structure basics, array, address calc...
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structuresPPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptx
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm  Array.pptx btech 2nd yeardata structure and algorithm  Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
DSA.pdf
DSA.pdfDSA.pdf
DSA.pdf
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
 
COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5COMPUTER PROGRAMMING UNIT 1 Lecture 5
COMPUTER PROGRAMMING UNIT 1 Lecture 5
 
M v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notesM v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notes
 

Dernier

Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)cama23
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfphamnguyenenglishnb
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 

Dernier (20)

Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)Global Lehigh Strategic Initiatives (without descriptions)
Global Lehigh Strategic Initiatives (without descriptions)
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdfAMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
AMERICAN LANGUAGE HUB_Level2_Student'sBook_Answerkey.pdf
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 

Lecture 3 data structures and algorithms

  • 1. Data Structure and Algorithm CS-102 Ashok K Turuk 1
  • 2. Data Structure vs Storage Structure • Data Structure : The logical or mathematical model of a particular organization of data • Storage Structure : Representation of a particular data structure in the memory of a computer • There are many possible storage structure to a particular data structure • Ex: there are a number of storage structure for a data structure such as array – It is possible for two DS to represented by the same storage structure 2
  • 3. Classification • Data Structure – Linear – Non-Linear • A Data structure is said to be linear if its elements from a sequence or in other words form a linear list 3
  • 4. Representation in Memory • Two basic representation in memory – Have a linear relationship between the elements represented by means of sequential memory locations [ Arrays] – Have the linear relationship between the elements represented by means of pointer or links [ Linked List] 4
  • 5. Operation on Linear Structure • Traversal : Processing each element in the list • Search : Finding the location of the element with a given value or the record with a given key • Insertion: Adding a new element to the list • Deletion: Removing an elements from the list • Sorting : Arranging the elements in some type of order • Merging : Combining two list into a single list 5
  • 7. Linear Arrays • A linear array is a list of a finite number of n homogeneous data elements ( that is data elements of the same type) such that – The elements are of the arrays are referenced respectively by an index set consisting of n consecutive numbers – The elements of the arrays are stored respectively in successive memory locations 7
  • 8. Linear Arrays • The number n of elements is called the length or size of the array. • The index set consists of the integer 1, 2, … n • Length or the number of data elements of the array can be obtained from the index set by Length = UB – LB + 1 where UB is the largest index called the upper bound and LB is the smallest index called the lower bound of the arrays 8
  • 9. Linear Arrays • Element of an array A may be denoted by – Subscript notation A1, A2, , …. , An – Parenthesis notation A(1), A(2), …. , A(n) – Bracket notation A[1], A[2], ….. , A[n] • The number K in A[K] is called subscript or an index and A[K] is called a subscripted variable 9
  • 10. Representation of Linear Array in Memory 1000 1001 1002 1003 1004 1005 : Computer Memory 10
  • 11. Representation of Linear Array in Memory • Let LA be a linear array in the memory of the computer • LOC(LA[K]) = address of the element LA[K] of the array LA • The element of LA are stored in the successive memory cells • Computer does not need to keep track of the address of every element of LA, but need to track only the address of the first element of the array denoted by Base(LA) called the base address of LA 11
  • 12. Representation of Linear Array in Memory • LOC(LA[K]) = Base(LA) + w(K – lower bound) where w is the number of words per memory cell of the array LA [w is aka size of the data type] 12
  • 13. Example 1 LA[1] 201 LA[2] 202 LA[3] 203 LA[4] 204 LA[5] 205 LA[6] 206 LA[7] 207 Find the address for LA[6] Each element of the array occupy 1 byte 200 LA[8] LOC(LA[K]) = Base(LA) + w(K – lower bound) : LOC(LA[6]) = 200 + 1(6 – 1) = 205 13
  • 14. Example 2 Find the address for LA[16] Each element of the array occupy 2 byte 200 LA[1] 201 202 LA[2] 203 204 LA[3] 205 206 207 LOC(LA[K]) = Base(LA) + w(K – lower bound) LA[4] : LOC(LA[16]) = 200 + 2(16 – 1) = 230 14
  • 15. Representation of Linear Array in Memory • Given any value of K, time to calculate LOC(LA[K]) is same • Given any subscript K one can access and locate the content of LA[K] without scanning any other element of LA • A collection A of data element is said to be index if any element of A called Ak can be located and processed in time that is independent of K 15
  • 16. Traversing Linear Arrays • Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones Linear Array ••• 16
  • 17. Traversing Linear Arrays • Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones Linear Array ••• 17
  • 18. Traversing Linear Arrays • Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones Linear Array ••• 18
  • 19. Traversing Linear Arrays • Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones Linear Array ••• 19
  • 20. Traversing Linear Arrays • Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones Linear Array ••• 20
  • 21. Traversing Linear Arrays • Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones Linear Array ••• 21
  • 22. Traversing Linear Arrays • Traversing is accessing and processing (aka visiting ) each element of the data structure exactly ones Linear Array ••• 1. Repeat for K = LB to UB Apply PROCESS to LA[K] [End of Loop] 2. Exit 22
  • 23. Inserting and Deleting • Insertion: Adding an element – Beginning – Middle – End • Deletion: Removing an element – Beginning – Middle – End 23
  • 29. Insertion Algorithm • INSERT (LA, N , K , ITEM) [LA is a linear array with N elements and K is a positive integers such that K ≤ N. This algorithm insert an element ITEM into the Kth position in LA ] 1. [Initialize Counter] Set J := N 2. Repeat Steps 3 and 4 while J ≥ K 3. [Move the Jth element downward ] Set LA[J + 1] := LA[J] 4. [Decrease Counter] Set J := J -1 5 [Insert Element] Set LA[K] := ITEM 6. [Reset N] Set N := N +1; 7. Exit 29
  • 30. Deletion Algorithm • DELETE (LA, N , K , ITEM) [LA is a linear array with N elements and K is a positive integers such that K ≤ N. This algorithm deletes Kth element from LA ] 1. Set ITEM := LA[K] 2. Repeat for J = K to N -1: [Move the J + 1st element upward] Set LA[J] := LA[J + 1] 3. [Reset the number N of elements] Set N := N - 1; 4. Exit 30
  • 31. Multidimensional Array • • • • One-Dimensional Array Two-Dimensional Array Three-Dimensional Array Some programming Language allows as many as 7 dimension 31
  • 32. Two-Dimensional Array • A Two-Dimensional m x n array A is a collection of m . n data elements such that each element is specified by a pair of integer (such as J, K) called subscript with property that 1 ≤ J ≤ m and 1 ≤ K ≤ n The element of A with first subscript J and second subscript K will be denoted by AJ,K or A[J][K] 32
  • 33. 2D Arrays The elements of a 2-dimensional array a is shown as below a[0][0] a[1][0] a[2][0] a[0][1] a[1][1] a[2][1] a[0][2] a[1][2] a[2][2] a[0][3] a[1][3] a[2][3]
  • 34. Rows Of A 2D Array a[0][0] a[1][0] a[2][0] a[0][1] a[1][1] a[2][1] a[0][2] a[1][2] a[2][2] a[0][3] a[1][3] a[2][3] row 0 row 1 row 2
  • 35. Columns Of A 2D Array a[0][0] a[1][0] a[2][0] a[0][1] a[1][1] a[2][1] a[0][2] a[1][2] a[2][2] a[0][3] a[1][3] a[2][3] column 0 column 1 column 2 column 3
  • 36. 2D Array • Let A be a two-dimensional array m x n • The array A will be represented in the memory by a block of m x n sequential memory location • Programming language will store array A either – Column by Column (Called Column-Major Order) Ex: Fortran, MATLAB – Row by Row (Called Row-Major Order) Ex: C, C++ , Java 36
  • 37. 2D Array in Memory A Subscript (1,1) (2,1) (3,1) A Subscript (1,1) Column 1 (1,2) (2,2) Column 2 (3,2) (1,3) (2,3) Column 3 (3,3) (1,4) (2,4) Column 4 (3,4) Column-Major Order (1,2) (1,3) Row 1 (1,4) (2,1) (2,2) (2,3) Row 2 (2,4) (3,1) (3,2) (3,3) Row 3 (3,4) Row-Major Order 37
  • 38. 2D Array • LOC(LA[K]) = Base(LA) + w(K -1) • LOC(A[J,K]) of A[J,K] Column-Major Order LOC(A[J,K]) = Base(A) + w[m(K-1) + (J-1)] Row-Major Order LOC(A[J,K]) = Base(A) + w[n(J-1) + (K-1)] 38
  • 39. 2D Array Example • Consider a 25 x 4 array A. Suppose the Base(A) = 200 and w =4. Suppose the programming store 2D array using rowmajor. Compute LOC(A[12,3]) • LOC(A[J,K]) = Base(A) + w[n(J-1) + (K-1)] • LOC(A[12,3]) = 200 + 4[4(12-1) + (3 -1)] = 384 39
  • 40. Multidimensional Array • An n-dimensional m1 x m2 x …. X mn array B is a collection of m1.m2…mn data elements in which each element is specified by a list of n integers – such as K1, K2, …., Kn – called subscript with the property that 1≤K1≤m1, 1≤K2≤m2, …. 1≤Kn≤mn The Element B with subscript K1, K2, …,Kn will be denoted by BK1,K2, …,Kn or B[K1,K2,….,Kn] 40
  • 41. Multidimensional Array • Let C be a n-dimensional array • Length Li of dimension i of C is the number of elements in the index set Li = UB – LB + 1 • For a given subscript Ki, the effective index Ei of Li is the number of indices preceding Ki in the index set Ei = Ki – LB 41
  • 42. Multidimensional Array • Address LOC(C[K1,K2, …., Kn]) of an arbitrary element of C can be obtained as Column-Major Order Base( C) + w[((( … (ENLN-1 + EN-1)LN-2) + ….. +E3)L2+E2)L1+E1] Row-Major Order Base( C) + w[(… ((E1L2 + E2)L3 + E3)L4 + ….. +EN-1)LN +EN] 42
  • 43. Example • MAZE(2:8, -4:1, 6:10) • Calculate the address of MAZE[5,-1,8] • Given: Base(MAZE) = 200, w = 4, MAZE is stored in Row-Major order • L1 = 8-2+1 = 7, L2 = 6, L3 = 5 • E1 = 5 -2 = 3, E2 = 3, E3 = 2 43
  • 44. Example Contd .. • Base( C) + w[(… ((E1L2 + E2)L3 + E3)L4 + ….. +EN-1)LN +EN] • E1L2 = 3 . 6 = 18 • E1L2 + E2 = 18 + 3 = 21 • (E1L2 + E2)L3 = 21 . 5 = 105 • (E1L2+E2)L3 + E3 = 105 + 2 = 107 • MAZE[5,-1,8] = 200 + 4(107) = 200 + 248 = 628 44
  • 45. Pointer, Pointer Array • Let DATA be any array • A variable P is called a pointer if P points to an element in DATA i.e if P contains the address of an element in DATA • An array PTR is called a pointer array if each element of PTR is a pointer 45
  • 46. Group 1 Group 2 Group 3 Group 4 Evan Conrad Davis Baker Harris Felt Segal Cooper Lewis Glass Ford Shaw Hill Gray Penn Jones Silver Reed Two Dimensional 4x9 or 9x4 array Troy Wagner King 46
  • 49. 1 Evan 2 Harris 3 Lewis 1 1 4 Shaw 2 5 5 Conrad : : Group 3 14 4 16 5 22 Group 1 Group 2 13 Wagner 14 Davis 15 Segal Group 3 16 Baker : : Group 4 21 Reed 22 $$$ 49