SlideShare a Scribd company logo
1 of 29
Download to read offline
Chapter-3
Linear Data Structure - Arrays
MRS.SOWMYA JYOTHI, SDMCBM, MANGALORE
Reference: Data Structures with C by Seymour Lipschutz,
Schaum’s Outlines Series.
INTRODUCTION
• Data structures are classified as either linear or nonlinear.
• A data structure is said to be linear, if its elements form a sequence.
There are two basic ways of representing linear structures in memory.
1. Using arrays.
2. Using linked lists.
• LINEAR ARRAYS
A linear array is a list of a finite number N of homogeneous data elements (i.e.
data elements of the same type) such that:
a) The elements of the array are referred respectively by an index set consisting
of n consecutive numbers
b) The elements of the array are stored respectively in successive memory
locations.
The number n of elements is called the length or size of the array. If not explicitly
stated, we will assume the index set consists of the integers 1, 2, …. N.
In general, the length or the number of data elements of the array can be
obtained from the index set by the formula
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 array. (Length= UB when LB=1).
The elements of an array A may be denoted by the subscript notation
A1, A2, A3, ….. An
OR by the bracket notation A[1], A[2], A[3], ….. A[n]
The number K in A[K] is called a subscript or an index and A[K] is called
a subscripted variable.
Example
• Let DATA be a 6-element linear array of integers such that DATA[1]=247
DATA[2]=56 DATA[3]=429 DAT[4]=135 DATA[5]=87 DATA[6]=156.
• We will denote such an array by DATA: 247, 56, 429, 135, 87, 156
Problems:
1) Consider the linear array LA(1:10).
Find the number of elements in an array.
Ans: The number of elements is equal to the length; hence the
formula
Length= UB – LB + 1
Length(LA) = 10 – 1 + 1
= 10
2) Consider the linear array AA(5:50), BB(-5:10) and CC(18). Find
the number of elements in each array.
Ans:
Length= UB – LB + 1
a) Length(AA) = 50 – 5 + 1
= 46
b) Length(BB) = 10 – (-5) + 1
= 16
c) Length(CC) = 18 – 1 + 1
= 18
Abstract Data Types (ADT)
• An abstract data type refers to a set of data values and associated
operations that are specified accurately, independent of
implementation.
• With an ADT, it is possible to know what a data type does, but how it
does is hidden.
• An ADT can further be defined as a data declaration packaged
together with the operations that are meaningful for the data. User
may not have the knowledge of data structure.
ARRAY AS ADT
• An array is a fundamental Abstract Data Type. An array is pre-
defined set which has a sequence of cells where we can store
different types of elements.
• Consider the following example: object (A, N). Here an array A is
created which can store N number of items in it.
• A[i] is an item in the array stored in its ith position.
• REPRESENTATION OF LINEAR ARRAYS IN MEMORY
• Let LA be a linear array in the memory of the computer. The notation
LOC(LA[K])=address of the element LA[K] of the array LA.
• Let LA[10]={5, 6, 12, 4, 15, 45, 87, 1, 9, 13} and Base address be 100.
• The elements of linear array LA are stored in successive
memory locations.
• Computer does not need to keep track of the address of
every element of LA, but it needs the base address Base(LA).
• Using the address of Base(LA), the computer calculates the
address of any element of LA by the following formula:
• LOC(LA[K]) = Base(LA) + w(K – Lower Bound)
Where w is number of words per memory cell for the array LA
Problems:
1) Consider the linear array AA(1:10).
Suppose Base(AA)=100 and w=4 words per memory cell
for AA.
Find the address of LA[5].
Ans:
LOC(AA[K]) = Base(AA) + w(K-LB)
LOC(AA[5]) = 100 + 4 (5-1)
= 100 + 16
= 116
2) Consider the linear array AA(5:50).
Suppose Base(AA)=300 and w=4 words per memory cell for AA.
Find the address of a) AA[15], b) AA[35] and c) AA[55].
LOC(AA[K]) = Base(AA) + w(K-LB)
a) LOC(AA[15]) = 300 + 4 (15-5)
= 300 + 40
= 340
b) LOC(AA[35]) = 300 + 4 (35-5)
= 300 + 120
= 420
c) AA[55] is not an element of AA, since 55 exceeds UB=50.
TRAVERSING A LINEAR ARRAY
• Let LA be the collection of data elements stored in the memory of the
computer. Suppose we want to print the contents of each element of
LA or suppose we want to count the number of elements of LA with
each given property.
• This can be accomplished by traversing A, that is, by accessing and
processing each element of the array exactly once is called
traversing.
• Algorithm 4.1: (Traverse a Linear Array) Here LA is a Linear array with
lower boundary LB and upper boundary UB. This algorithm traverses
LA applying an operation Process to each element of LA.
1. [Initialize counter] Set K:=LB.
2. Repeat Steps 3 and 4 while K≤UB.
3. [Visit element] Apply PROCESS to LA[K].
4. [Increase counter] Set K:=K+1.
[End of Step 2 loop]
5. Exit.
The alternate algorithm for traversing (using for loop) is:
Algorithm 4.1: (Traverse a Linear Array) This algorithm traverse a linear
array LA with lower bound LB and upper bound UB.
1. Repeat for K=LB to UB
Apply PROCESS to LA[K].
[End of loop]
2. Exit.
Problems:
1) Suppose a 5-element array A contains the values {25, 32, 65, 43, 78}.
Find the values of the given arrays after each loop.
a) Repeat for K = 1 to 4:
Set AB[K + 1] := A[K]
[End of loop]
Ans:
First AB[2] := A[1] sets AB[2]=25, the value of A[1]
Then AB[3] := A[2] sets AB[3]=32, the value of A[2]
Then AB[4] := A[3] sets AB[4]=65, the value of A[3]
Then AB[5] := A[4] sets AB[5]=43, the value of A[4]
Suppose a 5-element array A contains the values {25, 32, 65, 43, 78}.
b) Repeat for K = 4 to 1 by -1:
Set A[K + 1] := A[K]
[End of loop]
Ans:
First A[5] := A[4] sets A[5]=78
Then A[4] := A[3] sets A[4]=43
Then A[3] := A[2] sets A[3]=65
Then A[2] := A[1] sets A[2]=32
INSERTING
• Let LA be a collection of data elements in the memory of the
computer.
• “Inserting” refers to the operation of adding another
element to the collection LA.
• Always Insertion is done at the last.
• Suppose an element is to be inserted in the middle of the
array. Then, half of the elements must be moved downwards
to new locations to accommodate the new element and
keep the order of the other elements.
Algorithm 4.2: (Inserting into a Linear Array) INSERT (LA, N, K, ITEM) Here
LA is a linear array with N elements and K is a positive integer such that
K<=N. the algorithm inserts an element ITEM into Kth position in LA.
1. [Initialize counter] Set J:=N
2. Repeat Steps 3 and 4 while J>=K
3. [Move Jth element downward] Set LA[J+1]:=LA[J]
4. [Decrease the counter] Set J:=J-1.
[End of Step 2 Loop]
5. [Insert the element] Set LA[K]:=ITEM
6. [Reset N] Set N:=N+1
7. Exit
LA[J+1]:=LA[J]
Set J:=J-1.
5 6
4 5
3 4
J
N is Incremented as an
element is added
DELETING
• Let LA be a collection of data elements in the memory of the
computer.
• “Deleting” refers to the operation of removing one of the
elements from LA.
• Deleting an element at the end of an array presents no
difficulties, but deleting an element somewhere in the
middle of the array would require that each subsequent
element be moved one location upward in order to “fill up”
the array.
• The following algorithm deletes the Kth element from the
linear array LA and assigns it to the variable ITEM.
Algorithm 4.3: (Deleting from a Linear Array)
DELETE (LA, N, K, ITEM). Here LA is a linear array with N
elements and K is a positive integer such that K<=N. The
algorithm deletes Kth element from LA.
1. Set ITEM:=LA[K]
2. Repeat For J=K to N-1
[Move J+1st element upward] Set LA[J]:=LA[J+1]
[End of Loop]
3. [Reset the number N of elements in LA] Set N:=N-1.
4. Exit
LA[J]:=LA[J+1]
3 2
4 3
5 4
6 5
7 6
N is Decremented as an element is removed
REPRESENTATION OF POLYNOMIALS USING ARRAYS
•Sometimes it may be necessary to evaluate several
polynomial expressions to perform basic arithmetic
operations. The polynomial expressions can be stored
in arrays. All the elements of an array has two values
namely coefficient and exponent.
• Once a polynomial is built, it can be used to perform
any arithmetic operations.
• A single dimensional array is used to represent a
single variable polynomial of degree n. the index can
be considered as exponent and the coefficients can be
stored at that particular location.
SPARSE MATRIX
• A matrix with a relatively high proportion of zero entries is called
sparse matrix. Two general types of n-square sparse matrices
which occur in various applications are shown below.
1. The matrix where all the entries above the main diagonal are
zeros, non-zero entries may occur below the main diagonal is
called lower triangular matrix.
2. The matrix where all the entries below the main diagonal are
zeros, non-zero entries may occur above the main diagonal is
called upper triangular matrix.
3. The matrix where non-zero entries occur on the diagonal or on
elements immediately above or below the diagonal, is called
tri-diagonal matrix.
Chapter-3 Linear Data Structure - Arrays

More Related Content

What's hot

Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stackvaibhav2910
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structurekalyanineve
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data StructureMeghaj Mallick
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structureMAHALAKSHMI P
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)Elavarasi K
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examplesgreatqadirgee4u
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)eShikshak
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure pptNalinNishant3
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structureMeherul1234
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsDrishti Bhalla
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 

What's hot (20)

Introduction to stack
Introduction to stackIntroduction to stack
Introduction to stack
 
stack presentation
stack presentationstack presentation
stack presentation
 
deque and it applications
deque and it applicationsdeque and it applications
deque and it applications
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
Priority Queue in Data Structure
Priority Queue in Data StructurePriority Queue in Data Structure
Priority Queue in Data Structure
 
Big o notation
Big o notationBig o notation
Big o notation
 
sparse matrix in data structure
sparse matrix in data structuresparse matrix in data structure
sparse matrix in data structure
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Stacks Implementation and Examples
Stacks Implementation and ExamplesStacks Implementation and Examples
Stacks Implementation and Examples
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Sorting ppt
Sorting pptSorting ppt
Sorting ppt
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 
Binary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of AlgorithmsBinary Search - Design & Analysis of Algorithms
Binary Search - Design & Analysis of Algorithms
 
Time and Space Complexity
Time and Space ComplexityTime and Space Complexity
Time and Space Complexity
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
 
Linear search-and-binary-search
Linear search-and-binary-searchLinear search-and-binary-search
Linear search-and-binary-search
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 
Arrays
ArraysArrays
Arrays
 

Similar to Chapter-3 Linear Data Structure - Arrays

Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data StructureMandeep Singh
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structurestudent
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1smruti sarangi
 
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 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
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1blessyboban92
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxcoc7987515756
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTSwapnil Mishra
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringRAJASEKHARV8
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonchanna basava
 

Similar to Chapter-3 Linear Data Structure - Arrays (20)

Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
2. Array in Data Structure
2. Array in Data Structure2. Array in Data Structure
2. Array in Data Structure
 
DSA.pdf
DSA.pdfDSA.pdf
DSA.pdf
 
Arrays Data Structure
Arrays Data StructureArrays Data Structure
Arrays Data Structure
 
Arrays
ArraysArrays
Arrays
 
Data structure using c module 1
Data structure using c module 1Data structure using c module 1
Data structure using c module 1
 
2.DS Array
2.DS Array2.DS Array
2.DS Array
 
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 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
 
Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1Ist year Msc,2nd sem module1
Ist year Msc,2nd sem module1
 
CSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptxCSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptx
 
cluod.pdf
cluod.pdfcluod.pdf
cluod.pdf
 
data structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptxdata structure unit -1_170434dd7400.pptx
data structure unit -1_170434dd7400.pptx
 
Arrays.pptx
Arrays.pptxArrays.pptx
Arrays.pptx
 
Arrays in C.pptx
Arrays in C.pptxArrays in C.pptx
Arrays in C.pptx
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGESTDATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
 
Unit ii data structure-converted
Unit  ii data structure-convertedUnit  ii data structure-converted
Unit ii data structure-converted
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and EngineeringDS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
 
arrays in data structure.pptx
arrays in data structure.pptxarrays in data structure.pptx
arrays in data structure.pptx
 
List , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in pythonList , tuples, dictionaries and regular expressions in python
List , tuples, dictionaries and regular expressions in python
 

More from Sowmya Jyothi

Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESSowmya Jyothi
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiSowmya Jyothi
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiSowmya Jyothi
 
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothiArrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothiSowmya Jyothi
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiSowmya Jyothi
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHISowmya Jyothi
 
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHIBCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHISowmya Jyothi
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHISowmya Jyothi
 
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...Sowmya Jyothi
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in CSowmya Jyothi
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CSowmya Jyothi
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cSowmya Jyothi
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiSowmya Jyothi
 
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHINETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHISowmya Jyothi
 
Introduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHIIntroduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHISowmya Jyothi
 
Introduction to graphics
Introduction to graphicsIntroduction to graphics
Introduction to graphicsSowmya Jyothi
 
Inter Process Communication PPT
Inter Process Communication PPTInter Process Communication PPT
Inter Process Communication PPTSowmya Jyothi
 
Internal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya JyothiInternal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya JyothiSowmya Jyothi
 
Buffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiBuffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiSowmya Jyothi
 
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel  Chapter 2 Mrs.Sowmya JyothiIntroduction to the Kernel  Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel Chapter 2 Mrs.Sowmya JyothiSowmya Jyothi
 

More from Sowmya Jyothi (20)

Stacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURESStacks IN DATA STRUCTURES
Stacks IN DATA STRUCTURES
 
Functions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothiFunctions in c mrs.sowmya jyothi
Functions in c mrs.sowmya jyothi
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothiArrays in c unit iii chapter 1 mrs.sowmya jyothi
Arrays in c unit iii chapter 1 mrs.sowmya jyothi
 
Bca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothiBca data structures linked list mrs.sowmya jyothi
Bca data structures linked list mrs.sowmya jyothi
 
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHIBCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES SEARCHING AND SORTING MRS.SOWMYA JYOTHI
 
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHIBCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
BCA DATA STRUCTURES ALGORITHMS AND PRELIMINARIES MRS SOWMYA JYOTHI
 
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHIBCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
BCA DATA STRUCTURES INTRODUCTION AND OVERVIEW SOWMYA JYOTHI
 
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
HARDWARE AND PC MAINTENANCE -THE COMPLETE PC-MRS SOWMYA JYOTHI REFERENCE-MIKE...
 
Unit II chapter 4 Loops in C
Unit II chapter 4 Loops in CUnit II chapter 4 Loops in C
Unit II chapter 4 Loops in C
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Unit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in cUnit ii chapter 1 operator and expressions in c
Unit ii chapter 1 operator and expressions in c
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
 
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHINETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
NETWORK AND DATABASE CONCEPTS UNIT 1 CHAPTER 2 MRS.SOWMYA JYOTHI
 
Introduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHIIntroduction to computers MRS. SOWMYA JYOTHI
Introduction to computers MRS. SOWMYA JYOTHI
 
Introduction to graphics
Introduction to graphicsIntroduction to graphics
Introduction to graphics
 
Inter Process Communication PPT
Inter Process Communication PPTInter Process Communication PPT
Inter Process Communication PPT
 
Internal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya JyothiInternal representation of file chapter 4 Sowmya Jyothi
Internal representation of file chapter 4 Sowmya Jyothi
 
Buffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya JyothiBuffer cache unix ppt Mrs.Sowmya Jyothi
Buffer cache unix ppt Mrs.Sowmya Jyothi
 
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel  Chapter 2 Mrs.Sowmya JyothiIntroduction to the Kernel  Chapter 2 Mrs.Sowmya Jyothi
Introduction to the Kernel Chapter 2 Mrs.Sowmya Jyothi
 

Recently uploaded

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxUnboundStockton
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxAvyJaneVismanos
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 

Recently uploaded (20)

Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
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🔝
 
Blooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docxBlooming Together_ Growing a Community Garden Worksheet.docx
Blooming Together_ Growing a Community Garden Worksheet.docx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Final demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptxFinal demo Grade 9 for demo Plan dessert.pptx
Final demo Grade 9 for demo Plan dessert.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
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
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 

Chapter-3 Linear Data Structure - Arrays

  • 1. Chapter-3 Linear Data Structure - Arrays MRS.SOWMYA JYOTHI, SDMCBM, MANGALORE Reference: Data Structures with C by Seymour Lipschutz, Schaum’s Outlines Series.
  • 2. INTRODUCTION • Data structures are classified as either linear or nonlinear. • A data structure is said to be linear, if its elements form a sequence. There are two basic ways of representing linear structures in memory. 1. Using arrays. 2. Using linked lists.
  • 3. • LINEAR ARRAYS A linear array is a list of a finite number N of homogeneous data elements (i.e. data elements of the same type) such that: a) The elements of the array are referred respectively by an index set consisting of n consecutive numbers b) The elements of the array are stored respectively in successive memory locations. The number n of elements is called the length or size of the array. If not explicitly stated, we will assume the index set consists of the integers 1, 2, …. N. In general, the length or the number of data elements of the array can be obtained from the index set by the formula 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 array. (Length= UB when LB=1).
  • 4. The elements of an array A may be denoted by the subscript notation A1, A2, A3, ….. An OR by the bracket notation A[1], A[2], A[3], ….. A[n] The number K in A[K] is called a subscript or an index and A[K] is called a subscripted variable.
  • 5. Example • Let DATA be a 6-element linear array of integers such that DATA[1]=247 DATA[2]=56 DATA[3]=429 DAT[4]=135 DATA[5]=87 DATA[6]=156. • We will denote such an array by DATA: 247, 56, 429, 135, 87, 156
  • 6. Problems: 1) Consider the linear array LA(1:10). Find the number of elements in an array. Ans: The number of elements is equal to the length; hence the formula Length= UB – LB + 1 Length(LA) = 10 – 1 + 1 = 10
  • 7. 2) Consider the linear array AA(5:50), BB(-5:10) and CC(18). Find the number of elements in each array. Ans: Length= UB – LB + 1 a) Length(AA) = 50 – 5 + 1 = 46 b) Length(BB) = 10 – (-5) + 1 = 16 c) Length(CC) = 18 – 1 + 1 = 18
  • 8. Abstract Data Types (ADT) • An abstract data type refers to a set of data values and associated operations that are specified accurately, independent of implementation. • With an ADT, it is possible to know what a data type does, but how it does is hidden. • An ADT can further be defined as a data declaration packaged together with the operations that are meaningful for the data. User may not have the knowledge of data structure.
  • 9. ARRAY AS ADT • An array is a fundamental Abstract Data Type. An array is pre- defined set which has a sequence of cells where we can store different types of elements. • Consider the following example: object (A, N). Here an array A is created which can store N number of items in it. • A[i] is an item in the array stored in its ith position.
  • 10. • REPRESENTATION OF LINEAR ARRAYS IN MEMORY • Let LA be a linear array in the memory of the computer. The notation LOC(LA[K])=address of the element LA[K] of the array LA. • Let LA[10]={5, 6, 12, 4, 15, 45, 87, 1, 9, 13} and Base address be 100.
  • 11. • The elements of linear array LA are stored in successive memory locations. • Computer does not need to keep track of the address of every element of LA, but it needs the base address Base(LA). • Using the address of Base(LA), the computer calculates the address of any element of LA by the following formula: • LOC(LA[K]) = Base(LA) + w(K – Lower Bound) Where w is number of words per memory cell for the array LA
  • 12. Problems: 1) Consider the linear array AA(1:10). Suppose Base(AA)=100 and w=4 words per memory cell for AA. Find the address of LA[5]. Ans: LOC(AA[K]) = Base(AA) + w(K-LB) LOC(AA[5]) = 100 + 4 (5-1) = 100 + 16 = 116
  • 13. 2) Consider the linear array AA(5:50). Suppose Base(AA)=300 and w=4 words per memory cell for AA. Find the address of a) AA[15], b) AA[35] and c) AA[55]. LOC(AA[K]) = Base(AA) + w(K-LB) a) LOC(AA[15]) = 300 + 4 (15-5) = 300 + 40 = 340 b) LOC(AA[35]) = 300 + 4 (35-5) = 300 + 120 = 420 c) AA[55] is not an element of AA, since 55 exceeds UB=50.
  • 14. TRAVERSING A LINEAR ARRAY • Let LA be the collection of data elements stored in the memory of the computer. Suppose we want to print the contents of each element of LA or suppose we want to count the number of elements of LA with each given property. • This can be accomplished by traversing A, that is, by accessing and processing each element of the array exactly once is called traversing.
  • 15. • Algorithm 4.1: (Traverse a Linear Array) Here LA is a Linear array with lower boundary LB and upper boundary UB. This algorithm traverses LA applying an operation Process to each element of LA. 1. [Initialize counter] Set K:=LB. 2. Repeat Steps 3 and 4 while K≤UB. 3. [Visit element] Apply PROCESS to LA[K]. 4. [Increase counter] Set K:=K+1. [End of Step 2 loop] 5. Exit.
  • 16. The alternate algorithm for traversing (using for loop) is: Algorithm 4.1: (Traverse a Linear Array) This algorithm traverse a linear array LA with lower bound LB and upper bound UB. 1. Repeat for K=LB to UB Apply PROCESS to LA[K]. [End of loop] 2. Exit.
  • 17. Problems: 1) Suppose a 5-element array A contains the values {25, 32, 65, 43, 78}. Find the values of the given arrays after each loop. a) Repeat for K = 1 to 4: Set AB[K + 1] := A[K] [End of loop] Ans: First AB[2] := A[1] sets AB[2]=25, the value of A[1] Then AB[3] := A[2] sets AB[3]=32, the value of A[2] Then AB[4] := A[3] sets AB[4]=65, the value of A[3] Then AB[5] := A[4] sets AB[5]=43, the value of A[4]
  • 18. Suppose a 5-element array A contains the values {25, 32, 65, 43, 78}. b) Repeat for K = 4 to 1 by -1: Set A[K + 1] := A[K] [End of loop] Ans: First A[5] := A[4] sets A[5]=78 Then A[4] := A[3] sets A[4]=43 Then A[3] := A[2] sets A[3]=65 Then A[2] := A[1] sets A[2]=32
  • 19. INSERTING • Let LA be a collection of data elements in the memory of the computer. • “Inserting” refers to the operation of adding another element to the collection LA. • Always Insertion is done at the last. • Suppose an element is to be inserted in the middle of the array. Then, half of the elements must be moved downwards to new locations to accommodate the new element and keep the order of the other elements.
  • 20. Algorithm 4.2: (Inserting into a Linear Array) INSERT (LA, N, K, ITEM) Here LA is a linear array with N elements and K is a positive integer such that K<=N. the algorithm inserts an element ITEM into Kth position in LA. 1. [Initialize counter] Set J:=N 2. Repeat Steps 3 and 4 while J>=K 3. [Move Jth element downward] Set LA[J+1]:=LA[J] 4. [Decrease the counter] Set J:=J-1. [End of Step 2 Loop] 5. [Insert the element] Set LA[K]:=ITEM 6. [Reset N] Set N:=N+1 7. Exit
  • 21. LA[J+1]:=LA[J] Set J:=J-1. 5 6 4 5 3 4 J N is Incremented as an element is added
  • 22. DELETING • Let LA be a collection of data elements in the memory of the computer. • “Deleting” refers to the operation of removing one of the elements from LA. • Deleting an element at the end of an array presents no difficulties, but deleting an element somewhere in the middle of the array would require that each subsequent element be moved one location upward in order to “fill up” the array. • The following algorithm deletes the Kth element from the linear array LA and assigns it to the variable ITEM.
  • 23. Algorithm 4.3: (Deleting from a Linear Array) DELETE (LA, N, K, ITEM). Here LA is a linear array with N elements and K is a positive integer such that K<=N. The algorithm deletes Kth element from LA. 1. Set ITEM:=LA[K] 2. Repeat For J=K to N-1 [Move J+1st element upward] Set LA[J]:=LA[J+1] [End of Loop] 3. [Reset the number N of elements in LA] Set N:=N-1. 4. Exit
  • 24. LA[J]:=LA[J+1] 3 2 4 3 5 4 6 5 7 6 N is Decremented as an element is removed
  • 25. REPRESENTATION OF POLYNOMIALS USING ARRAYS •Sometimes it may be necessary to evaluate several polynomial expressions to perform basic arithmetic operations. The polynomial expressions can be stored in arrays. All the elements of an array has two values namely coefficient and exponent. • Once a polynomial is built, it can be used to perform any arithmetic operations. • A single dimensional array is used to represent a single variable polynomial of degree n. the index can be considered as exponent and the coefficients can be stored at that particular location.
  • 26.
  • 27.
  • 28. SPARSE MATRIX • A matrix with a relatively high proportion of zero entries is called sparse matrix. Two general types of n-square sparse matrices which occur in various applications are shown below. 1. The matrix where all the entries above the main diagonal are zeros, non-zero entries may occur below the main diagonal is called lower triangular matrix. 2. The matrix where all the entries below the main diagonal are zeros, non-zero entries may occur above the main diagonal is called upper triangular matrix. 3. The matrix where non-zero entries occur on the diagonal or on elements immediately above or below the diagonal, is called tri-diagonal matrix.