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)]

More Related Content

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

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

Recently uploaded (20)

PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
PDF
The Different Types of Non-Experimental Research
Thelma Villaflores
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PDF
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PDF
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PDF
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
PPTX
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
PPTX
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
The Different Types of Non-Experimental Research
Thelma Villaflores
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
Knee Extensor Mechanism Injuries - Orthopedic Radiologic Imaging
Sean M. Fox
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
0725.WHITEPAPER-UNIQUEWAYSOFPROTOTYPINGANDUXNOW.pdf
Thomas GIRARD, MA, CDP
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
Aprendendo Arquitetura Framework Salesforce - Dia 03
Mauricio Alexandre Silva
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Dimensions of Societal Planning in Commonism
StefanMz
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
CATEGORIES OF NURSING PERSONNEL: HOSPITAL & COLLEGE
PRADEEP ABOTHU
 
A PPT on Alfred Lord Tennyson's Ulysses.
Beena E S
 
How to Set Up Tags in Odoo 18 - Odoo Slides
Celine George
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
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