SlideShare a Scribd company logo
Arrays
 The simplest type of data structure is a one-dimensional array or list
or linear array.
 Linear array is a list of finite number (n) of similar data elements
referenced respectively by a set of n consecutive numbers , usually
1,2…..n.
 If we choose a name A for the array , then the elements of A are
denoted by subscript notation
a1,a2,a3……,an
or by the parenthesis notation
A(1),A(2)………. ,A(n)
or by the bracket notation
A[1],A[2]………. , A[n]
 The value K in A[K] is called a subscript and A[K] is called a
subscripted variable.
 The elements of an array are stored in consecutive memory locations.
Linear Array
 A linear array LA can be pictured as
(LB) 1
2 1 2 3 4 5
3
4
(UB) 5
Notations :-
 LB :- Lower Bound
 UB :- Upper Bound
 Length of an array :- UB – LB + 1
 LOC(LA[K]) :- Address of the element LA[K] of the array LA
 Base (LA) :- Address of the first element of LA.
 LOC (LA [K]) =Base (LA) + w (K-lower bound) ,where w is the number of words per memory
cell for the array LA.
Traversing Linear Arrays
Let LA be a collection of elements stored
in memory of the computer. Suppose we
want to print the contents of the array or
to count the number of elements of LA
with a given property. This can be
accomplished by traversing LA ,that is by
accessing and processing each element of
exactly once.
Algorithm
 Here LA is a linear array with lower bound LB
and upper bound UB. This algorithm traverses
LA applying an operation 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. [Increment counter.] K = K+1.
End of step 2 loop.
5. Exit.
Insertion
Suppose LA is an array containing 5
names in alphabetical order
Brown
Davis
Johnson
Smith
Wagner
 Suppose we want to add an element “Ford” into the
array.
 Then Johnson, Smith, and wagner must each be moved
downward one location.
Algorithm
 (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. This algorithm inserts an
element ITEM into the Kth
position inLA.
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 Counter.] Set J = J-1.
[End of step 2 loop.]
5. [Insert element.] set LA[K] = ITEM.
6. [Reset N] Set N= N + 1.
7. Exit
Deletion
Suppose we want to delete an element
“smith” from the array.
Brown
Davis
Johnson
Smith
Wagner
The “wagner” should be moved upwards.
Deletion
 (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. This algorithm
deletes the Kth
element from LA.
 Set ITEM = LA[K]
 Repeat for J=K to N-1:
[Move J+1 th
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 .
Searching
Let A be a collection of data elements in
memory and suppose a specific ITEM of
information is given. Searching refers to the
operation of finding the location LOC of
ITEM in A or printing some message that
ITEM does not appear there.
The search is said to be successful if ITEM
does appear in A and unsuccessful
otherwise.
Searching Techniques
There are different searching techniques
Linear Search
Binary Search
Binary search is more efficient than linear
search. It take lesser time for execution.
The complexity of searching algorithms is
measured in terms of the number of
comparisons required to find ITEM in array
.
Linear Search
 Suppose A is an array with N elements.
 To search for a given item in A , compare ITEM
with each element of A one by one.
 This method , which traverses A sequentially to
locate ITEM , is called linear search or sequential
search.
4 5 6 7 8
4 5 6 7 8 6
1 2 3 4 5 N+1
Step 1: Assign ITEM to N+1
Step 1: Assign ITEM to N+1 th
th
location
location
Algorithm
 (Linear search) LINEAR(A,N,ITEM,LOC)
Here A is a linear array with N elements, and ITEM is a given
item of information. This algorithm finds the location LOC of
ITEM in DATA , or sets LOC=0 if search is unsuccessful.
1. [Insert ITEM at the end of A]. Set A[N+1]=ITEM.
2. [Initialize Counter] Set LOC=1.
3. [Search for ITEM]
Repeat while A[LOC] != ITEM
Set LOC=LOC+1
[End of loop]
4. [Successful?] Print LOC
5. [UnSuccessful?] If LOC=N+1 , then Set LOC=0.
6. Exit
Binary Search
 Suppose A is an array which is sorted in increasing numerical
order or alphabetically.
 Then there is an extremely efficient searching algorithm, called
binary search.
 The algorithm compares ITEM with the middle element
A[MID] where MID is obtained by
MID=((BEG+END)/2)
 If A[MID]=ITEM , then search is successful and set LOC=MID.
Otherwise a new segment of A is obtained as follows:
 If ITEM<A[MID], then ITEM can appear only in the left half of the segment :
A[BEG],A[BEG+1],…….A[MID-1]
• So reset END = MID-1 and search again.
 If ITEM>A[MID], then ITEM can appear only in the right half of the segment :
A[MID+1],A[MID+2]……..A[END].
• So reset BEG=MID+1 and search again.
Algorithm
 (Binary search) BINARY(A,LB,UB,ITEM,LOC)
Here A is a sorted array with a lower bound LB and upper bound UB, and ITEM
is a given ITEM of information. The variables BEG,MID and END denote,
respectively the beginning, end and middle locations of a segment of elements of A.
This algorithm finds the location Loc of ITEM in A or sets LOC=NULL.
1. [Initialize segment variables.]
Set BEG=LB,END=UB AND MID = INT((BEG+END)/2)
2. Repeat Steps 3 and 4 while BEG<= END and A[MID]!=ITEM
3. If ITEM<A[MID], then:
Set END= MID – 1
Else
Set BEG = MID + 1
[End of If.]
4. Set MID = INT((BEG+END)/2)
[End of step2 loop.]
5. If A[MID]=ITEM , then
Set LOC= MID
else
Set LOC=NULL
[End of If]
Example
Consider an array with 5 elements :
Let ITEM= 76
12 , 15 , 20 , 23 , 32, 45 , 54 , 76 , 98 beg=1,end=9 ,mid=5
12 , 15 , 20 , 23 , 32 , 45 ,
12 , 15 , 20 , 23 , 32 , 45 , 54
54 , 76 , 98
, 76 , 98 Beg=6 ,end=9 ,mid=7
12 , 15 , 20 , 23 , 32 , 45 , 54 ,
12 , 15 , 20 , 23 , 32 , 45 , 54 , 76
76 , 98
, 98 Beg=8 ,end= 9 ,mid=8
Left sub array Right sub array
Right sub array
Limitations of Binary Search
 Even if the binary search algorithm is so efficient
it requires two conditions :
The list must be sorted and
One must have direct access to the middle element in
any sub list .
 This means that one must essentially use a
sorted array to hold data.
 But keeping data in sorted array is normally
very expensive when there are many insertions
and deletions.
Multi-Dimensional arrays
Linear arrays in which each element is
referenced by a single subscript is called a
one – dimensional array.
Arrays in which elements are referenced by
more than one subscript is called multi-
dimensional arrays.
A two-dimensional array A is a collection of
m*n elements such that each element is
specified by a pair of subscripts A [i][j]
Representation of 2-D arrays
 A 2-D array is stored in memory by a block of m*n
sequential memory locations.
 The programming language will store the array A
either
1) Column by column (column-major order)
2) Row by row (row-major order)
.
.
.
A(1,1)
A(1,2)
.
.
.
.
A(m,n)
2-D arrays
 For a one-dimensional array computer uses the
formula
LOC(A[K])=Base (A) +w(K-1)
to find the address of A[K].
 For a 2-D array the address of A[J,K] is found
out using the formula
LOC(A[J,K])=Base(A)+w[M(K-1)+(J-1)]
(Column major order)
LOC(A[J,K] = Base (A)+[N(J-1)+(K-1)]
Ad

Recommended

Data structure using c module 1
Data structure using c module 1
smruti sarangi
 
arrays
arrays
Enakshi Chanda
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
Lecture 4_Linear & Binary search from data structure and algorithm
Lecture 4_Linear & Binary search from data structure and algorithm
ArifatunNesa
 
ARRAY in python and c with examples .pptx
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
Data structures arrays
Data structures arrays
maamir farooq
 
data structure and algorithm Array.pptx btech 2nd year
data structure and algorithm Array.pptx btech 2nd year
palhimanshi999
 
DSA.pdf
DSA.pdf
AkashSaha75835
 
Arrays Data Structure
Arrays Data Structure
student
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
midtushar
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Malikireddy Bramhananda Reddy
 
Chap10
Chap10
Terry Yoast
 
Arrays
Arrays
DebiPrasadSen
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
Arrays in C.pptx
Arrays in C.pptx
HarsimranKaur362773
 
cluod.pdf
cluod.pdf
ssuser92d367
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
1 D Arrays in C++
1 D Arrays in C++
poonam.rwalia
 
Searching
Searching
A. S. M. Shafi
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
vanithasivdc
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
Sorting techniques
Sorting techniques
Sukhvinder Singh
 
Array 2
Array 2
Abbott
 
2.DS Array
2.DS Array
Chandan Singh
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 

More Related Content

Similar to arrays1.ppt python programme arrays insertion (20)

Arrays Data Structure
Arrays Data Structure
student
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
midtushar
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Malikireddy Bramhananda Reddy
 
Chap10
Chap10
Terry Yoast
 
Arrays
Arrays
DebiPrasadSen
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
Arrays in C.pptx
Arrays in C.pptx
HarsimranKaur362773
 
cluod.pdf
cluod.pdf
ssuser92d367
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
1 D Arrays in C++
1 D Arrays in C++
poonam.rwalia
 
Searching
Searching
A. S. M. Shafi
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
vanithasivdc
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
Sorting techniques
Sorting techniques
Sukhvinder Singh
 
Array 2
Array 2
Abbott
 
2.DS Array
2.DS Array
Chandan Singh
 
Arrays Data Structure
Arrays Data Structure
student
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
midtushar
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Malikireddy Bramhananda Reddy
 
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
DATA STRUCTURE CLASS 12 COMPUTER SCIENCE
Dev Chauhan
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
kncetaruna
 
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
VISWANATHAN R V
 
data structures and algorithms Unit 3
data structures and algorithms Unit 3
infanciaj
 
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
PYTHON ALGORITHMS, DATA STRUCTURE, SORTING TECHNIQUES
vanithasivdc
 
DATA STRUCTURES USING C -ENGGDIGEST
DATA STRUCTURES USING C -ENGGDIGEST
Swapnil Mishra
 
Array 2
Array 2
Abbott
 

Recently uploaded (20)

How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Chalukyas of Gujrat, Solanki Dynasty NEP.pptx
Dr. Ravi Shankar Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
GlysdiEelesor1
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
THERAPEUTIC COMMUNICATION included definition, characteristics, nurse patient...
parmarjuli1412
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
Revista digital preescolar en transformación
Revista digital preescolar en transformación
guerragallardo26
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
Non-Communicable Diseases and National Health Programs – Unit 10 | B.Sc Nursi...
RAKESH SAJJAN
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Introduction to Generative AI and Copilot.pdf
Introduction to Generative AI and Copilot.pdf
TechSoup
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
What is FIle and explanation of text files.pptx
What is FIle and explanation of text files.pptx
Ramakrishna Reddy Bijjam
 
Publishing Your Memoir with Brooke Warner
Publishing Your Memoir with Brooke Warner
Brooke Warner
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
How to Implement Least Package Removal Strategy in Odoo 18 Inventory
Celine George
 
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
FIRST DAY HIGH orientation for mapeh subject in grade 10.pptx
GlysdiEelesor1
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
BINARY files CSV files JSON files with example.pptx
BINARY files CSV files JSON files with example.pptx
Ramakrishna Reddy Bijjam
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Ad

arrays1.ppt python programme arrays insertion

  • 1. Arrays  The simplest type of data structure is a one-dimensional array or list or linear array.  Linear array is a list of finite number (n) of similar data elements referenced respectively by a set of n consecutive numbers , usually 1,2…..n.  If we choose a name A for the array , then the elements of A are denoted by subscript notation a1,a2,a3……,an or by the parenthesis notation A(1),A(2)………. ,A(n) or by the bracket notation A[1],A[2]………. , A[n]  The value K in A[K] is called a subscript and A[K] is called a subscripted variable.  The elements of an array are stored in consecutive memory locations.
  • 2. Linear Array  A linear array LA can be pictured as (LB) 1 2 1 2 3 4 5 3 4 (UB) 5 Notations :-  LB :- Lower Bound  UB :- Upper Bound  Length of an array :- UB – LB + 1  LOC(LA[K]) :- Address of the element LA[K] of the array LA  Base (LA) :- Address of the first element of LA.  LOC (LA [K]) =Base (LA) + w (K-lower bound) ,where w is the number of words per memory cell for the array LA.
  • 3. Traversing Linear Arrays Let LA be a collection of elements stored in memory of the computer. Suppose we want to print the contents of the array or to count the number of elements of LA with a given property. This can be accomplished by traversing LA ,that is by accessing and processing each element of exactly once.
  • 4. Algorithm  Here LA is a linear array with lower bound LB and upper bound UB. This algorithm traverses LA applying an operation 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. [Increment counter.] K = K+1. End of step 2 loop. 5. Exit.
  • 5. Insertion Suppose LA is an array containing 5 names in alphabetical order Brown Davis Johnson Smith Wagner  Suppose we want to add an element “Ford” into the array.  Then Johnson, Smith, and wagner must each be moved downward one location.
  • 6. Algorithm  (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. This algorithm inserts an element ITEM into the Kth position inLA. 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 Counter.] Set J = J-1. [End of step 2 loop.] 5. [Insert element.] set LA[K] = ITEM. 6. [Reset N] Set N= N + 1. 7. Exit
  • 7. Deletion Suppose we want to delete an element “smith” from the array. Brown Davis Johnson Smith Wagner The “wagner” should be moved upwards.
  • 8. Deletion  (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. This algorithm deletes the Kth element from LA.  Set ITEM = LA[K]  Repeat for J=K to N-1: [Move J+1 th 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 .
  • 9. Searching Let A be a collection of data elements in memory and suppose a specific ITEM of information is given. Searching refers to the operation of finding the location LOC of ITEM in A or printing some message that ITEM does not appear there. The search is said to be successful if ITEM does appear in A and unsuccessful otherwise.
  • 10. Searching Techniques There are different searching techniques Linear Search Binary Search Binary search is more efficient than linear search. It take lesser time for execution. The complexity of searching algorithms is measured in terms of the number of comparisons required to find ITEM in array .
  • 11. Linear Search  Suppose A is an array with N elements.  To search for a given item in A , compare ITEM with each element of A one by one.  This method , which traverses A sequentially to locate ITEM , is called linear search or sequential search. 4 5 6 7 8 4 5 6 7 8 6 1 2 3 4 5 N+1 Step 1: Assign ITEM to N+1 Step 1: Assign ITEM to N+1 th th location location
  • 12. Algorithm  (Linear search) LINEAR(A,N,ITEM,LOC) Here A is a linear array with N elements, and ITEM is a given item of information. This algorithm finds the location LOC of ITEM in DATA , or sets LOC=0 if search is unsuccessful. 1. [Insert ITEM at the end of A]. Set A[N+1]=ITEM. 2. [Initialize Counter] Set LOC=1. 3. [Search for ITEM] Repeat while A[LOC] != ITEM Set LOC=LOC+1 [End of loop] 4. [Successful?] Print LOC 5. [UnSuccessful?] If LOC=N+1 , then Set LOC=0. 6. Exit
  • 13. Binary Search  Suppose A is an array which is sorted in increasing numerical order or alphabetically.  Then there is an extremely efficient searching algorithm, called binary search.  The algorithm compares ITEM with the middle element A[MID] where MID is obtained by MID=((BEG+END)/2)  If A[MID]=ITEM , then search is successful and set LOC=MID. Otherwise a new segment of A is obtained as follows:  If ITEM<A[MID], then ITEM can appear only in the left half of the segment : A[BEG],A[BEG+1],…….A[MID-1] • So reset END = MID-1 and search again.  If ITEM>A[MID], then ITEM can appear only in the right half of the segment : A[MID+1],A[MID+2]……..A[END]. • So reset BEG=MID+1 and search again.
  • 14. Algorithm  (Binary search) BINARY(A,LB,UB,ITEM,LOC) Here A is a sorted array with a lower bound LB and upper bound UB, and ITEM is a given ITEM of information. The variables BEG,MID and END denote, respectively the beginning, end and middle locations of a segment of elements of A. This algorithm finds the location Loc of ITEM in A or sets LOC=NULL. 1. [Initialize segment variables.] Set BEG=LB,END=UB AND MID = INT((BEG+END)/2) 2. Repeat Steps 3 and 4 while BEG<= END and A[MID]!=ITEM 3. If ITEM<A[MID], then: Set END= MID – 1 Else Set BEG = MID + 1 [End of If.] 4. Set MID = INT((BEG+END)/2) [End of step2 loop.] 5. If A[MID]=ITEM , then Set LOC= MID else Set LOC=NULL [End of If]
  • 15. Example Consider an array with 5 elements : Let ITEM= 76 12 , 15 , 20 , 23 , 32, 45 , 54 , 76 , 98 beg=1,end=9 ,mid=5 12 , 15 , 20 , 23 , 32 , 45 , 12 , 15 , 20 , 23 , 32 , 45 , 54 54 , 76 , 98 , 76 , 98 Beg=6 ,end=9 ,mid=7 12 , 15 , 20 , 23 , 32 , 45 , 54 , 12 , 15 , 20 , 23 , 32 , 45 , 54 , 76 76 , 98 , 98 Beg=8 ,end= 9 ,mid=8 Left sub array Right sub array Right sub array
  • 16. Limitations of Binary Search  Even if the binary search algorithm is so efficient it requires two conditions : The list must be sorted and One must have direct access to the middle element in any sub list .  This means that one must essentially use a sorted array to hold data.  But keeping data in sorted array is normally very expensive when there are many insertions and deletions.
  • 17. Multi-Dimensional arrays Linear arrays in which each element is referenced by a single subscript is called a one – dimensional array. Arrays in which elements are referenced by more than one subscript is called multi- dimensional arrays. A two-dimensional array A is a collection of m*n elements such that each element is specified by a pair of subscripts A [i][j]
  • 18. Representation of 2-D arrays  A 2-D array is stored in memory by a block of m*n sequential memory locations.  The programming language will store the array A either 1) Column by column (column-major order) 2) Row by row (row-major order) . . . A(1,1) A(1,2) . . . . A(m,n)
  • 19. 2-D arrays  For a one-dimensional array computer uses the formula LOC(A[K])=Base (A) +w(K-1) to find the address of A[K].  For a 2-D array the address of A[J,K] is found out using the formula LOC(A[J,K])=Base(A)+w[M(K-1)+(J-1)] (Column major order) LOC(A[J,K] = Base (A)+[N(J-1)+(K-1)]

Editor's Notes