SlideShare a Scribd company logo
Department of Computer Science and Engineering (CSE)
ADVANCED DATA STRUCTURES
&
ALGORITHMS
(20CST-624,20CST-633)
Prepared By :DIVYA SINGH
ARRAYS
2
CO
Number
Title Level
CO1 To understand role of algorithms
in science and practice of
computing..
Remem
ber
CO2 To gain familiarization with
different algorithm design
techniques.
Understa
nd
CO3 To apply different algorithm design
techniques for solving engineering
and related problems and study their
performance.
Analysis
and
applicati
on
Course Outcome
Will be covered in
this lecture
Department of Computer Science and Engineering (CSE)
Lecture Outcomes
 To understand the concept of arrays.
 To study the various operations on arrays.
Department of Computer Science and Engineering (CSE)
Syllabus/Topics To be Covered
 Basic terminology
 Linear arrays and their representation, Traversing Linear
Array, Insertion & Deletion in arrays
 Searching – linear search, binary search
 Multi-dimensional arrays and their representation
Department of Computer Science and Engineering (CSE)
Arrays
Linear array (One dimensional array) : A list of finite
number n of similar data elements referenced respectively by a
set of n consecutive numbers, usually 1, 2, 3,…..n. That is a
specific element is accessed by an index.
Let, Array name is A then the elements of A is : a1,a2….. An or by
the bracket notation A[1], A[2], A[3],…………., A[n].The number
k in A[k] is called a subscript and A[k] is called a subscripted
variable.
Department of Computer Science and Engineering (CSE)
Arrays...
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Linear arrays are called one dimensional arrays because each
element in such an array is referenced by one subscript.
(Two dimensional array) : Two dimensional array is a collection of
similar data elements where each element is referenced by two
subscripts.
Such arrays are called matrices in mathematics.
Multidimensional arrays are defined analogously
1
2
1 2 3 4
3
4
MATRICES
Here, MATRICES[3,3]=11
Department of Computer Science and Engineering (CSE)
Array (con…)
Array Data Structure
1. It can hold multiple values of a single type.
2. Elements are referenced by the array name and an ordinal index.
3. Each element is a value
4. Indexing begins at zero in C.
5. The array forms a contiguous list in memory.
6. The name of the array holds the address of the first array element.
7. We specify the array size at compile time, often with a named constant or
at run time using calloc(), malloc() & realloc() in C and new in C++.
Arrays – Advantage & disadvantage
1. Fast element access.
2. Impossible to resize. Many applications require resizing so linked
list was introduced.
Department of Computer Science and Engineering (CSE)
Traversing linear arrays
Print the contents of each element of DATA or Count the number of elements
of DATA with a given property. This can be accomplished by traversing DATA,
That is, by accessing and processing (visiting) each element of DATA exactly
once.
Algorithm: Given DATA is a linear array with lower bound LB and
upper bound UB . This algorithm traverses DATA applying an operation
PROCESS to each element of DATA.
1. Set K : = LB.
2. Repeat steps 3 and 4 while K<=UB:
3. Apply PROCESS to DATA[k]
4. Set K : = K+1.
5. Exit.
Department of Computer Science and Engineering (CSE)
Linear Arrays
A linear array is a list of finite number n of homogeneous data elements(i.e. data
elements of same type)
a) The elements of the array are referenced respectively by an index set
b) The elements of the array are stored respectively in successive memory
locations.
Let, Array name is A then the elements of A is : a1,a2….. an
Or by the bracket notation A[1], A[2], A[3],…………., A[n]
1
2
3
4
5
6
247
56
429
135
87
156
DATA[1] = 247
DATA[2] = 56
DATA[3] = 429
DATA[4] = 135
DATA[5] = 87
DATA[6] = 156
Department of Computer Science and Engineering (CSE)
Representation of linear array in memory
Let LA be a linear array in the memory of the computer. The memory of the
computer is a sequence of addressed locations.
1000
1001
1002
1003
1004
1005
LA
Fig : Computer memory
The computer does not need to keep track of the
address of every element of LA, but needs to keep
track only of the first element of LA, denoted by
Base(LA)
called the base address of LA. Using this address
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 the number of words per memory cell for
the array LA
Department of Computer Science and Engineering (CSE)
Representation of linear array in memory
Example :
An automobile company uses an array AUTO to record
the number of auto mobile sold each year from 1932
through 1984. Suppose AUTO appears in memory as
pictured in fig A . That is Base(AUTO) = 200, and w = 4
words per memory cell for AUTO. Then,
LOC(AUTO[1932]) = 200, LOC(AUTO[1933]) =204
LOC(AUTO[1934]) = 208
the address of the array element for the year K = 1965
can be obtained by using :
LOC(AUTO[1965]) = Base(AUTO) + w(1965 – lower
bound)
=200+4(1965-1932)=332
200
201
202
203
204
205
206
207
208
209
210
211
212
AUTO[1932]
AUTO[1933]
AUTO[1934]
INDEX
Department of Computer Science and Engineering (CSE)
Representation of linear array
in memory(contd.)
200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Example:
LOC(LA[6])=200+2(6-0) = 200+12 = 212
LOC(LA[9])=200+2(9-0) = 200+18 = 218
LOC(LA[15])=200+2(15-0) = 200+30 = 230
Department of Computer Science and Engineering (CSE)
Traversing linear arrays
Example :
An automobile company uses an array AUTO to record the number of auto
mobile sold each year from 1932 through 1984.
a) Find the number NUM of years during which more than 300 automobiles
were sold.
b) Print each year and the number of automobiles sold in that year
1. Set NUM : = 0.
2. Repeat for K = 1932 to 1984:
if AUTO[K]> 300, then : set NUM : = NUM+1
3. Exit.
1. Repeat for K = 1932 to 1984:
Write : K, AUTO[K]
2. Exit.
Department of Computer Science and Engineering (CSE)
Insertion in an array
INSERTING AN ELEMENT INTO AN ARRAY:
Insert (LA, N, K, ITEM)
Here LA is linear array with N elements and K(position) is a positive integer
such that K<=N.This algorithm inserts an element ITEM into the Kth
position in LA.
ALGORITHM
Step 1. [Initialize counter] Set J:=N
Step 2. Repeat Steps 3 and 4 while J>=K
Step 3. [Move Jth element downward] Set LA [J+1]: =LA [J]
Step 4. [Decrease counter] Set J:=J-1
[End of step 2 loop]
Step 5 [Insert element] Set LA [K]: =ITEM
Step 6. [Reset N] Set N:=N+1
Step 7. Exit
Department of Computer Science and Engineering (CSE)
Deletion from an array
DELETING AN ELEMENT 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.
ALGORITHM
Step 1. Set ITEM: = LA [K]
Step 2. Repeat for steps 3&4 for J=K to N-1:
Step 3. [Move J+1st element upward] Set LA [J]: =LA [J+1]
Step4. [Increment counter] J=J+1
[End of step2 loop]
Step 5. [Reset the number N of elements in LA] Set N:=N-1
Step 6. Exit
Department of Computer Science and Engineering (CSE)
Linear Search :
Algorithm : A linear array DATA with N elements and a specific ITEM of
information are given. This algorithm finds the location LOC of ITEM in
the array DATA or sets LOC = 0.
1. Set K : = 1, LOC : =0.
2. Repeat steps 3 and 4 while LOC = 0 and K<=N:
3. If ITEM = DATA[K], then : Set LOC : =K .
4. Set K : = K+1.
[End of step 2 loop]
5. [Successful?]
If LOC = 0, then :
Write : ITEM is not in the array DATA.
Else :
Write : LOC is the location of ITEM.
[End of if structure]
6. Exit.
Searching – Linear search
Department of Computer Science and Engineering (CSE)
Linear Search Program
if(c= =1)
{
cout<<"Number found at position: "<<l
}
else
{
cout<<"Number is not in the list";
}
getch( );
}
#include<iostream.h>
#include<conio.h>
void main( )
{
int a[100],n,i;
clrscr( );
cout << "Enter the size of an array:";
cin >> n;
cout <<"The elemets are:" << endl;
for( i=0; i<n; i++ )
{
cin >> a[i];
}
cout<<"Enter the element you want to search";
cin>>item;
for(i=0;i<n;i++)
{
if(arr[i]==item)
{
c=1;
loc=i;
break;
}
}
Department of Computer Science and Engineering (CSE)
Linear Search complexity
 Linear Search :
 Best Case: Find at first place - one comparison
 Average case: There are n cases that can occur, i.e. find at the
first place, the second place, the third place and so on up to
the nth place.
average = (1+2+3.....+n)/n = n(n+1)/2n=(n+1)/2
where the result was used that 1+2+3 ...+n is equal to
n(n+1)/2.
 Worst case: Find at nth place or not at all - n comparisons
Department of Computer Science and Engineering (CSE)
Searching –Binary Search
 Binary Search:
 Algorithm: Binary(DATA,LB,UB,ITEM): Here data is a
sorted array with lower bound LB and upper bound
UB and ITEM is an element to be searched. The
variables BEG,END and MID denote, resp, the
beginning, end and middle locations of a segment of
elements of DATA. This algorithm find the location
LOC of ITEM in DATA or sets LOC=NULL.
Department of Computer Science and Engineering (CSE)
Binary Search Algorithm
BINARY(DATA, LB, UB, ITEM, LOC)
1. Set BEG=LB; END=UB; and MID=INT((BEG+END)/2).
2. Repeat step 3 and 4 while BEG ≤ END and DATA[MID] ≠ ITEM
3. If ITEM < DATA[MID] then
Set END= MID - 1
Else:
Set BEG= MID+1
[end of if structure]
4. Set MID= INT((BEG+END)/2)
[End of step 2 loop]
5. If ITEM = DATA[MID] then
Set LOC= MID
Else:
Set LOC= NULL
[end of if structure]
6. Exit.
Department of Computer Science and Engineering (CSE)
Binary Search example (Seek for 123)
Department of Computer Science and Engineering (CSE)
Binary Search - Complexity
 O(log2 n) . Each comparison reduces the sample size in half. Hence we
require at most log2 n comparisons to locate ITEM. For Ex, there are 100
elements, so maximum number of comparisons to find the element will be:
log2 n = log2 100 => 2? =100
=> 27 =100
i.e. 7(6.643856) comparisons on 100 elements
 Limitations:
 The list must be sorted
Department of Computer Science and Engineering (CSE)
Binary Search Program
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<abstarry.h>
void main()
{
int a[20],n,beg,end,mid,f=0,element;
cout<<"nEnter number of elements:";
cin>>n;
cout<<"nEnter elements in sorted order:n";
for(int i=0;i<n;i++)
cin>>a[i];
cout<<"n You have entered:";
for(i=0;i<n;i++)
cout<<a[i]<<" ";
cout<<"nEnter element you want to search:";
cin>>element;
beg=0;
end=n-1;
while(beg<=end)
{
mid=(beg+end)/2;
if(element<a[mid])
beg=mid-1;
else if (element>a[mid])
beg=mid+1;
else if(element==a[mid])
{
cout<<"!..................!";
cout<<"nposition is:"<<mid;
cout<<"n!..................!";
f=1;
break;
}
}
if(f==0)
cout<<"nElement does not exist";
getch();
}
Department of Computer Science and Engineering (CSE)
Multidimensional arrays
Two – dimensional Arrays
 Array having more than one subscript variable is called Multi-Dimensional array.
 A Two – dimensional Arrays m x n array A is a collection of m . n data elements such that each
element is specified by a pair of integers (such as I, J), called subscripts.
 The element of A with first subscript I and second subscript J will be denoted by A[I,J]
A[3, 1] A[3, 2] A[3, 3]
A[1, 1] A[1, 2] A[1, 3]
A[2, 2]
A[2, 1] A[2, 3]
1
2
3
1 2 3
Rows
Columns
Fig: Two dimensional 3 x 3 array A
Department of Computer Science and Engineering (CSE)
Declaration and Use of Two
Dimensional Array
 Declaration :
int a[3][4];
 Use :
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cout<<a[i][j]<<“ “;
}
cout<<“n”;
}
Meaning of Two Dimensional Array :
1. Matrix is having 3 rows ( i takes value
from 0 to 2 )
2. Matrix is having 4 Columns ( j takes
value from 0 to 3 )
3. Above Matrix 3×4 matrix will have 12
blocks having 3 rows & 4 columns.
4. Name of 2-D array is ’a’ and each block is
identified by the row & column number.
5. Row number and column number starts
from 0.
Department of Computer Science and Engineering (CSE)
Memory Representation of 2-D
array
 2-D arrays are Stored in contiguous memory location row
wise.
 Consider 3×3 Array is stored in contiguous memory
location which starts from some base address(e.g.4000) .
 Array element a[1][1] will be stored at address 4000, again
a[1][2]will be stored to next memory location i.e elements
stored row-wise.
 After Elements of First Row are stored in appropriate
memory location, then second row and so on.
 If we are taking integer array , then each element will
require 2 bytes of memory.
Department of Computer Science and Engineering (CSE)
Array Representation of 2-D
array
 Column-major
 Row-major
 Arrays may be represented in Row-major form or Column-
major form.
 In Row-major form, all the elements of the first row are
printed, then the elements of the second row and so on up
to the last row.
 In Column-major form, all the elements of the first column
are printed, then the elements of the second column and so
on up to the last column.
Department of Computer Science and Engineering (CSE)
Array Representation(contd..)
Index Memory Location Element
a[1][1] 4000 1
a[1][2] 4002 2
a[1][3] 4004 3
a[2][1] 4006 4
a[2][2] 4008 5
a[2][3] 4010 6
a[3][1] 4012 7
a[3][2] 4014 8
a[3][3] 4016 9
Index Memory Location Element
a[1][1] 4000 1
a[2][1] 4002 4
a[3][1] 4004 7
a[1][2] 4006 2
a[2][2] 4008 5
a[3][2] 4010 8
A[1][3] 4012 3
a[2][3] 4014 6
a[3][3] 4016 9
ARRAY
1 2 3
1
2
3
ROW-MAJOR
COLUMN-MAJOR
Ad

Recommended

PPT Lecture 2.2.1 onn c++ data structures
PPT Lecture 2.2.1 onn c++ data structures
midtushar
 
DSA.pdf
DSA.pdf
AkashSaha75835
 
Data structures (Array 1 dimensional).pptx
Data structures (Array 1 dimensional).pptx
itzsomeone50
 
Updated Lab3.docx
Updated Lab3.docx
AleezaAnjum
 
arrays1.ppt python programme arrays insertion
arrays1.ppt python programme arrays insertion
bushraashraf639
 
arrays
arrays
Enakshi Chanda
 
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
BCA DATA STRUCTURES LINEAR ARRAYS MRS.SOWMYA JYOTHI
Sowmya Jyothi
 
03 Linear Arrays Memory Representations .pdf
03 Linear Arrays Memory Representations .pdf
KkSingh64
 
2.DS Array
2.DS Array
Chandan Singh
 
ARRAY in python and c with examples .pptx
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
Arrays
Arrays
DebiPrasadSen
 
Arrays Data Structure
Arrays Data Structure
student
 
arrays in data structure.pptx
arrays in data structure.pptx
KasthuriKAssistantPr
 
ds 2Arrays.ppt
ds 2Arrays.ppt
AlliVinay1
 
Data structure using c module 1
Data structure using c module 1
smruti sarangi
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 
Data Structures Chapter-4
Data Structures Chapter-4
priyavanimurugarajan
 
Array 2
Array 2
Abbott
 
Data-Structure-using-C-Rajesh-Pandey.pdf
Data-Structure-using-C-Rajesh-Pandey.pdf
mohanaprakasht
 
Arrays
Arrays
SARITHA REDDY
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
DEEPAK948083
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
Array data structure
Array data structure
maamir farooq
 
CSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptx
MamunurRasidAsif
 
Data structure ppt
Data structure ppt
Prof. Dr. K. Adisesha
 
Array data structure
Array data structure
harsh112327
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
Using LinkedIn for Your Job Search June 2025
Using LinkedIn for Your Job Search June 2025
Bruce Bennett
 
Ruchi Bhatia HRGurukul - The Roads less travelled
Ruchi Bhatia HRGurukul - The Roads less travelled
Ruchi Bhatia
 

More Related Content

Similar to data structure and algorithm Array.pptx btech 2nd year (20)

2.DS Array
2.DS Array
Chandan Singh
 
ARRAY in python and c with examples .pptx
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
Arrays
Arrays
DebiPrasadSen
 
Arrays Data Structure
Arrays Data Structure
student
 
arrays in data structure.pptx
arrays in data structure.pptx
KasthuriKAssistantPr
 
ds 2Arrays.ppt
ds 2Arrays.ppt
AlliVinay1
 
Data structure using c module 1
Data structure using c module 1
smruti sarangi
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 
Data Structures Chapter-4
Data Structures Chapter-4
priyavanimurugarajan
 
Array 2
Array 2
Abbott
 
Data-Structure-using-C-Rajesh-Pandey.pdf
Data-Structure-using-C-Rajesh-Pandey.pdf
mohanaprakasht
 
Arrays
Arrays
SARITHA REDDY
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
DEEPAK948083
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
Array data structure
Array data structure
maamir farooq
 
CSE225_LEC3 (1).pptx
CSE225_LEC3 (1).pptx
MamunurRasidAsif
 
Data structure ppt
Data structure ppt
Prof. Dr. K. Adisesha
 
Array data structure
Array data structure
harsh112327
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 
ARRAY in python and c with examples .pptx
ARRAY in python and c with examples .pptx
abhishekmaurya102515
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
Aakash deep Singhal
 
Arrays Data Structure
Arrays Data Structure
student
 
ds 2Arrays.ppt
ds 2Arrays.ppt
AlliVinay1
 
Data structure using c module 1
Data structure using c module 1
smruti sarangi
 
Array linear data_structure_2 (1)
Array linear data_structure_2 (1)
eShikshak
 
Array 2
Array 2
Abbott
 
Data-Structure-using-C-Rajesh-Pandey.pdf
Data-Structure-using-C-Rajesh-Pandey.pdf
mohanaprakasht
 
datastructureppt-190327174340 (1).pptx
datastructureppt-190327174340 (1).pptx
DEEPAK948083
 
358 33 powerpoint-slides_5-arrays_chapter-5
358 33 powerpoint-slides_5-arrays_chapter-5
sumitbardhan
 
Array data structure
Array data structure
maamir farooq
 
Array data structure
Array data structure
harsh112327
 
DS Complete notes for Computer science and Engineering
DS Complete notes for Computer science and Engineering
RAJASEKHARV8
 

Recently uploaded (20)

Using LinkedIn for Your Job Search June 2025
Using LinkedIn for Your Job Search June 2025
Bruce Bennett
 
Ruchi Bhatia HRGurukul - The Roads less travelled
Ruchi Bhatia HRGurukul - The Roads less travelled
Ruchi Bhatia
 
ACEN presentation / huddle from June 2025
ACEN presentation / huddle from June 2025
Mark Rauterkus
 
Final year project presentation mod.pptx
Final year project presentation mod.pptx
AdityaKesarwani13
 
一比一原版(UPV毕业证书)巴斯克大学毕业证如何办理
一比一原版(UPV毕业证书)巴斯克大学毕业证如何办理
taqyed
 
suruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn bvcbvc
suruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn bvcbvc
dineshkumarengg
 
physical_hazard.ppt-, sixjirixiifrr8fifk4kfickfkjk
physical_hazard.ppt-, sixjirixiifrr8fifk4kfickfkjk
ShrawanKumarTiwari
 
一比一原版(UTS毕业证)悉尼科技大学毕业证如何办理
一比一原版(UTS毕业证)悉尼科技大学毕业证如何办理
taqyed
 
Twilio Jobs Ghaziabad 673 Part Time Job in Uttar Pradesh.pdf
Twilio Jobs Ghaziabad 673 Part Time Job in Uttar Pradesh.pdf
infonaukridekhe
 
Yellow and Purple Doodle Startup Pitch Deck Presentation.pdf.pdf
Yellow and Purple Doodle Startup Pitch Deck Presentation.pdf.pdf
kharadeshreya2210
 
SOC_Incident_Report_Process_Presentation.pptx
SOC_Incident_Report_Process_Presentation.pptx
rtanvi1518
 
CISA Certification And Training online..
CISA Certification And Training online..
Streling next pvt ltd
 
Quarter 3 Program Implementation Review and Performance Assessment
Quarter 3 Program Implementation Review and Performance Assessment
LaMariaAngelicaPunay
 
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
halderdhrubo6
 
t20 world cup journey OF INDIA IN WEST INDIES
t20 world cup journey OF INDIA IN WEST INDIES
manpreetkaur3469
 
Cost_Effective_Multi_Source_Energy_Harvesting_System1 final 5.pptx
Cost_Effective_Multi_Source_Energy_Harvesting_System1 final 5.pptx
AnishNaskar4
 
Gives a structured overview of the skills measured in the DP-700 exam
Gives a structured overview of the skills measured in the DP-700 exam
thehulk1299
 
Detection to Communicationnnnnnnnnnn.pptx
Detection to Communicationnnnnnnnnnn.pptx
rtanvi1518
 
From reducing flood risks to conserving water
From reducing flood risks to conserving water
Ali Ata
 
What is UPSC job in India ? , Details about UPSC
What is UPSC job in India ? , Details about UPSC
prathamdigitalmarket
 
Using LinkedIn for Your Job Search June 2025
Using LinkedIn for Your Job Search June 2025
Bruce Bennett
 
Ruchi Bhatia HRGurukul - The Roads less travelled
Ruchi Bhatia HRGurukul - The Roads less travelled
Ruchi Bhatia
 
ACEN presentation / huddle from June 2025
ACEN presentation / huddle from June 2025
Mark Rauterkus
 
Final year project presentation mod.pptx
Final year project presentation mod.pptx
AdityaKesarwani13
 
一比一原版(UPV毕业证书)巴斯克大学毕业证如何办理
一比一原版(UPV毕业证书)巴斯克大学毕业证如何办理
taqyed
 
suruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn bvcbvc
suruuuuuuuuxdvvvvvvvvvvvvvv ssssssrnbn bvcbvc
dineshkumarengg
 
physical_hazard.ppt-, sixjirixiifrr8fifk4kfickfkjk
physical_hazard.ppt-, sixjirixiifrr8fifk4kfickfkjk
ShrawanKumarTiwari
 
一比一原版(UTS毕业证)悉尼科技大学毕业证如何办理
一比一原版(UTS毕业证)悉尼科技大学毕业证如何办理
taqyed
 
Twilio Jobs Ghaziabad 673 Part Time Job in Uttar Pradesh.pdf
Twilio Jobs Ghaziabad 673 Part Time Job in Uttar Pradesh.pdf
infonaukridekhe
 
Yellow and Purple Doodle Startup Pitch Deck Presentation.pdf.pdf
Yellow and Purple Doodle Startup Pitch Deck Presentation.pdf.pdf
kharadeshreya2210
 
SOC_Incident_Report_Process_Presentation.pptx
SOC_Incident_Report_Process_Presentation.pptx
rtanvi1518
 
CISA Certification And Training online..
CISA Certification And Training online..
Streling next pvt ltd
 
Quarter 3 Program Implementation Review and Performance Assessment
Quarter 3 Program Implementation Review and Performance Assessment
LaMariaAngelicaPunay
 
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
halderdhrubo6
 
t20 world cup journey OF INDIA IN WEST INDIES
t20 world cup journey OF INDIA IN WEST INDIES
manpreetkaur3469
 
Cost_Effective_Multi_Source_Energy_Harvesting_System1 final 5.pptx
Cost_Effective_Multi_Source_Energy_Harvesting_System1 final 5.pptx
AnishNaskar4
 
Gives a structured overview of the skills measured in the DP-700 exam
Gives a structured overview of the skills measured in the DP-700 exam
thehulk1299
 
Detection to Communicationnnnnnnnnnn.pptx
Detection to Communicationnnnnnnnnnn.pptx
rtanvi1518
 
From reducing flood risks to conserving water
From reducing flood risks to conserving water
Ali Ata
 
What is UPSC job in India ? , Details about UPSC
What is UPSC job in India ? , Details about UPSC
prathamdigitalmarket
 
Ad

data structure and algorithm Array.pptx btech 2nd year

  • 1. Department of Computer Science and Engineering (CSE) ADVANCED DATA STRUCTURES & ALGORITHMS (20CST-624,20CST-633) Prepared By :DIVYA SINGH
  • 2. ARRAYS 2 CO Number Title Level CO1 To understand role of algorithms in science and practice of computing.. Remem ber CO2 To gain familiarization with different algorithm design techniques. Understa nd CO3 To apply different algorithm design techniques for solving engineering and related problems and study their performance. Analysis and applicati on Course Outcome Will be covered in this lecture
  • 3. Department of Computer Science and Engineering (CSE) Lecture Outcomes  To understand the concept of arrays.  To study the various operations on arrays.
  • 4. Department of Computer Science and Engineering (CSE) Syllabus/Topics To be Covered  Basic terminology  Linear arrays and their representation, Traversing Linear Array, Insertion & Deletion in arrays  Searching – linear search, binary search  Multi-dimensional arrays and their representation
  • 5. Department of Computer Science and Engineering (CSE) Arrays Linear array (One dimensional array) : A list of finite number n of similar data elements referenced respectively by a set of n consecutive numbers, usually 1, 2, 3,…..n. That is a specific element is accessed by an index. Let, Array name is A then the elements of A is : a1,a2….. An or by the bracket notation A[1], A[2], A[3],…………., A[n].The number k in A[k] is called a subscript and A[k] is called a subscripted variable.
  • 6. Department of Computer Science and Engineering (CSE) Arrays... 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Linear arrays are called one dimensional arrays because each element in such an array is referenced by one subscript. (Two dimensional array) : Two dimensional array is a collection of similar data elements where each element is referenced by two subscripts. Such arrays are called matrices in mathematics. Multidimensional arrays are defined analogously 1 2 1 2 3 4 3 4 MATRICES Here, MATRICES[3,3]=11
  • 7. Department of Computer Science and Engineering (CSE) Array (con…) Array Data Structure 1. It can hold multiple values of a single type. 2. Elements are referenced by the array name and an ordinal index. 3. Each element is a value 4. Indexing begins at zero in C. 5. The array forms a contiguous list in memory. 6. The name of the array holds the address of the first array element. 7. We specify the array size at compile time, often with a named constant or at run time using calloc(), malloc() & realloc() in C and new in C++. Arrays – Advantage & disadvantage 1. Fast element access. 2. Impossible to resize. Many applications require resizing so linked list was introduced.
  • 8. Department of Computer Science and Engineering (CSE) Traversing linear arrays Print the contents of each element of DATA or Count the number of elements of DATA with a given property. This can be accomplished by traversing DATA, That is, by accessing and processing (visiting) each element of DATA exactly once. Algorithm: Given DATA is a linear array with lower bound LB and upper bound UB . This algorithm traverses DATA applying an operation PROCESS to each element of DATA. 1. Set K : = LB. 2. Repeat steps 3 and 4 while K<=UB: 3. Apply PROCESS to DATA[k] 4. Set K : = K+1. 5. Exit.
  • 9. Department of Computer Science and Engineering (CSE) Linear Arrays A linear array is a list of finite number n of homogeneous data elements(i.e. data elements of same type) a) The elements of the array are referenced respectively by an index set b) The elements of the array are stored respectively in successive memory locations. Let, Array name is A then the elements of A is : a1,a2….. an Or by the bracket notation A[1], A[2], A[3],…………., A[n] 1 2 3 4 5 6 247 56 429 135 87 156 DATA[1] = 247 DATA[2] = 56 DATA[3] = 429 DATA[4] = 135 DATA[5] = 87 DATA[6] = 156
  • 10. Department of Computer Science and Engineering (CSE) Representation of linear array in memory Let LA be a linear array in the memory of the computer. The memory of the computer is a sequence of addressed locations. 1000 1001 1002 1003 1004 1005 LA Fig : Computer memory The computer does not need to keep track of the address of every element of LA, but needs to keep track only of the first element of LA, denoted by Base(LA) called the base address of LA. Using this address 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 the number of words per memory cell for the array LA
  • 11. Department of Computer Science and Engineering (CSE) Representation of linear array in memory Example : An automobile company uses an array AUTO to record the number of auto mobile sold each year from 1932 through 1984. Suppose AUTO appears in memory as pictured in fig A . That is Base(AUTO) = 200, and w = 4 words per memory cell for AUTO. Then, LOC(AUTO[1932]) = 200, LOC(AUTO[1933]) =204 LOC(AUTO[1934]) = 208 the address of the array element for the year K = 1965 can be obtained by using : LOC(AUTO[1965]) = Base(AUTO) + w(1965 – lower bound) =200+4(1965-1932)=332 200 201 202 203 204 205 206 207 208 209 210 211 212 AUTO[1932] AUTO[1933] AUTO[1934] INDEX
  • 12. Department of Computer Science and Engineering (CSE) Representation of linear array in memory(contd.) 200 202 204 206 208 210 212 214 216 218 220 222 224 226 228 230 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Example: LOC(LA[6])=200+2(6-0) = 200+12 = 212 LOC(LA[9])=200+2(9-0) = 200+18 = 218 LOC(LA[15])=200+2(15-0) = 200+30 = 230
  • 13. Department of Computer Science and Engineering (CSE) Traversing linear arrays Example : An automobile company uses an array AUTO to record the number of auto mobile sold each year from 1932 through 1984. a) Find the number NUM of years during which more than 300 automobiles were sold. b) Print each year and the number of automobiles sold in that year 1. Set NUM : = 0. 2. Repeat for K = 1932 to 1984: if AUTO[K]> 300, then : set NUM : = NUM+1 3. Exit. 1. Repeat for K = 1932 to 1984: Write : K, AUTO[K] 2. Exit.
  • 14. Department of Computer Science and Engineering (CSE) Insertion in an array INSERTING AN ELEMENT INTO AN ARRAY: Insert (LA, N, K, ITEM) Here LA is linear array with N elements and K(position) is a positive integer such that K<=N.This algorithm inserts an element ITEM into the Kth position in LA. ALGORITHM Step 1. [Initialize counter] Set J:=N Step 2. Repeat Steps 3 and 4 while J>=K Step 3. [Move Jth element downward] Set LA [J+1]: =LA [J] Step 4. [Decrease counter] Set J:=J-1 [End of step 2 loop] Step 5 [Insert element] Set LA [K]: =ITEM Step 6. [Reset N] Set N:=N+1 Step 7. Exit
  • 15. Department of Computer Science and Engineering (CSE) Deletion from an array DELETING AN ELEMENT 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. ALGORITHM Step 1. Set ITEM: = LA [K] Step 2. Repeat for steps 3&4 for J=K to N-1: Step 3. [Move J+1st element upward] Set LA [J]: =LA [J+1] Step4. [Increment counter] J=J+1 [End of step2 loop] Step 5. [Reset the number N of elements in LA] Set N:=N-1 Step 6. Exit
  • 16. Department of Computer Science and Engineering (CSE) Linear Search : Algorithm : A linear array DATA with N elements and a specific ITEM of information are given. This algorithm finds the location LOC of ITEM in the array DATA or sets LOC = 0. 1. Set K : = 1, LOC : =0. 2. Repeat steps 3 and 4 while LOC = 0 and K<=N: 3. If ITEM = DATA[K], then : Set LOC : =K . 4. Set K : = K+1. [End of step 2 loop] 5. [Successful?] If LOC = 0, then : Write : ITEM is not in the array DATA. Else : Write : LOC is the location of ITEM. [End of if structure] 6. Exit. Searching – Linear search
  • 17. Department of Computer Science and Engineering (CSE) Linear Search Program if(c= =1) { cout<<"Number found at position: "<<l } else { cout<<"Number is not in the list"; } getch( ); } #include<iostream.h> #include<conio.h> void main( ) { int a[100],n,i; clrscr( ); cout << "Enter the size of an array:"; cin >> n; cout <<"The elemets are:" << endl; for( i=0; i<n; i++ ) { cin >> a[i]; } cout<<"Enter the element you want to search"; cin>>item; for(i=0;i<n;i++) { if(arr[i]==item) { c=1; loc=i; break; } }
  • 18. Department of Computer Science and Engineering (CSE) Linear Search complexity  Linear Search :  Best Case: Find at first place - one comparison  Average case: There are n cases that can occur, i.e. find at the first place, the second place, the third place and so on up to the nth place. average = (1+2+3.....+n)/n = n(n+1)/2n=(n+1)/2 where the result was used that 1+2+3 ...+n is equal to n(n+1)/2.  Worst case: Find at nth place or not at all - n comparisons
  • 19. Department of Computer Science and Engineering (CSE) Searching –Binary Search  Binary Search:  Algorithm: Binary(DATA,LB,UB,ITEM): Here data is a sorted array with lower bound LB and upper bound UB and ITEM is an element to be searched. The variables BEG,END and MID denote, resp, the beginning, end and middle locations of a segment of elements of DATA. This algorithm find the location LOC of ITEM in DATA or sets LOC=NULL.
  • 20. Department of Computer Science and Engineering (CSE) Binary Search Algorithm BINARY(DATA, LB, UB, ITEM, LOC) 1. Set BEG=LB; END=UB; and MID=INT((BEG+END)/2). 2. Repeat step 3 and 4 while BEG ≤ END and DATA[MID] ≠ ITEM 3. If ITEM < DATA[MID] then Set END= MID - 1 Else: Set BEG= MID+1 [end of if structure] 4. Set MID= INT((BEG+END)/2) [End of step 2 loop] 5. If ITEM = DATA[MID] then Set LOC= MID Else: Set LOC= NULL [end of if structure] 6. Exit.
  • 21. Department of Computer Science and Engineering (CSE) Binary Search example (Seek for 123)
  • 22. Department of Computer Science and Engineering (CSE) Binary Search - Complexity  O(log2 n) . Each comparison reduces the sample size in half. Hence we require at most log2 n comparisons to locate ITEM. For Ex, there are 100 elements, so maximum number of comparisons to find the element will be: log2 n = log2 100 => 2? =100 => 27 =100 i.e. 7(6.643856) comparisons on 100 elements  Limitations:  The list must be sorted
  • 23. Department of Computer Science and Engineering (CSE) Binary Search Program #include<iostream.h> #include<conio.h> #include<stdlib.h> #include<abstarry.h> void main() { int a[20],n,beg,end,mid,f=0,element; cout<<"nEnter number of elements:"; cin>>n; cout<<"nEnter elements in sorted order:n"; for(int i=0;i<n;i++) cin>>a[i]; cout<<"n You have entered:"; for(i=0;i<n;i++) cout<<a[i]<<" "; cout<<"nEnter element you want to search:"; cin>>element; beg=0; end=n-1; while(beg<=end) { mid=(beg+end)/2; if(element<a[mid]) beg=mid-1; else if (element>a[mid]) beg=mid+1; else if(element==a[mid]) { cout<<"!..................!"; cout<<"nposition is:"<<mid; cout<<"n!..................!"; f=1; break; } } if(f==0) cout<<"nElement does not exist"; getch(); }
  • 24. Department of Computer Science and Engineering (CSE) Multidimensional arrays Two – dimensional Arrays  Array having more than one subscript variable is called Multi-Dimensional array.  A Two – dimensional Arrays m x n array A is a collection of m . n data elements such that each element is specified by a pair of integers (such as I, J), called subscripts.  The element of A with first subscript I and second subscript J will be denoted by A[I,J] A[3, 1] A[3, 2] A[3, 3] A[1, 1] A[1, 2] A[1, 3] A[2, 2] A[2, 1] A[2, 3] 1 2 3 1 2 3 Rows Columns Fig: Two dimensional 3 x 3 array A
  • 25. Department of Computer Science and Engineering (CSE) Declaration and Use of Two Dimensional Array  Declaration : int a[3][4];  Use : for(i=0;i<3;i++) { for(j=0;j<4;j++) { cout<<a[i][j]<<“ “; } cout<<“n”; } Meaning of Two Dimensional Array : 1. Matrix is having 3 rows ( i takes value from 0 to 2 ) 2. Matrix is having 4 Columns ( j takes value from 0 to 3 ) 3. Above Matrix 3×4 matrix will have 12 blocks having 3 rows & 4 columns. 4. Name of 2-D array is ’a’ and each block is identified by the row & column number. 5. Row number and column number starts from 0.
  • 26. Department of Computer Science and Engineering (CSE) Memory Representation of 2-D array  2-D arrays are Stored in contiguous memory location row wise.  Consider 3×3 Array is stored in contiguous memory location which starts from some base address(e.g.4000) .  Array element a[1][1] will be stored at address 4000, again a[1][2]will be stored to next memory location i.e elements stored row-wise.  After Elements of First Row are stored in appropriate memory location, then second row and so on.  If we are taking integer array , then each element will require 2 bytes of memory.
  • 27. Department of Computer Science and Engineering (CSE) Array Representation of 2-D array  Column-major  Row-major  Arrays may be represented in Row-major form or Column- major form.  In Row-major form, all the elements of the first row are printed, then the elements of the second row and so on up to the last row.  In Column-major form, all the elements of the first column are printed, then the elements of the second column and so on up to the last column.
  • 28. Department of Computer Science and Engineering (CSE) Array Representation(contd..) Index Memory Location Element a[1][1] 4000 1 a[1][2] 4002 2 a[1][3] 4004 3 a[2][1] 4006 4 a[2][2] 4008 5 a[2][3] 4010 6 a[3][1] 4012 7 a[3][2] 4014 8 a[3][3] 4016 9 Index Memory Location Element a[1][1] 4000 1 a[2][1] 4002 4 a[3][1] 4004 7 a[1][2] 4006 2 a[2][2] 4008 5 a[3][2] 4010 8 A[1][3] 4012 3 a[2][3] 4014 6 a[3][3] 4016 9 ARRAY 1 2 3 1 2 3 ROW-MAJOR COLUMN-MAJOR