PPT Lecture 2.2.1 Array, Searching
PPT Lecture 2.2.1 Array, Searching
Course Objectives
3
Scheme of Evaluation
Direct Evaluation Weightage of actual Final Weightage in Mapping with SIs Remarks
Sl No. Frequency of Task BT Levels CO Mapping Mapping with PIs
Instruments conduct Internal Assessment (ABET) (Graded/Non-Graded)
SO1 1a,1b,1c
10 marks for each
1 Assignment One per unit 10 Hard CO4,CO5 Graded
assignment
SO6
2 Exam 20 marks for one MST 2 per semester 20 Medium CO1,CO2,CO3,CO4 6a,6b Graded
3 Case Study 8 marks 1 per unit 8 Easy CO2 SO1, SO6 1c,6b Graded
NA NA NA NA
One per lecture
4 Homework NA topic (of 2 NA Non-Graded
questions)
NA NA NA NA
5 Discussion Forum NA One per unit NA Non-Graded
NA NA NA NA
6 Presentation NA NA NA Non-Graded
7 Attendance NA NA 2 NA NA NA NA Graded
Remarks
Direct Evaluation Final Weightage in Mapping with SIs
S No. Weightage of actual conduct Frequency of Task BT Levels CO Mapping Mapping with PIs (Graded/Non-
Instruments Internal Assessment (ABET)
Graded)
Unit wise Practical 1a, 1b, 1c, 6a, 6b SO1, SO6
1 15 marks 3 45 Medium 1,2,3,4,5 Graded
Evaluation
1a, 1b, 1c, 6a, 6b
2 Exam 15 marks for one MST 1 per semester 15 Medium 1,2,3 SO1, SO6 Graded
3 Attendance NA NA 2 NA NA NA NA Graded
4
Arrays
• An array is a collection of items
stored at contiguous memory
locations. The idea is to store multiple
items of the same type together.
• This makes it easier to calculate the
position of each element by simply
adding an offset to a base value, i.e.,
the memory location of the first
element of the array (generally
denoted by the name of the array).
http://www.mathcs.emory.edu/~cheung/Courses/171/
Syllabus/1-intro/rev=arrays.html
5
Arrays
• 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
• Array Data Structure
• It can hold multiple values of a single type.
• Elements are referenced by the array name and
an ordinal index. https://www.chegg.com/homework-help/questions-and-answers
• Each element is a value. /matrix-array-vector-rows-data-frame-table-columns-lists-r-data-
types-true-true-true-true-f-q28713018
• Indexing begins at zero in C.
• The array forms a contiguous list in memory.
6
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.
• Traversing a linear structure means moving
through it sequentially, node by node.
Processing the data element of a node may
be complex, but the general pattern is as
follows:
• Begin at the first node.
https://www.sqa.org.uk/e-learning/LinkedDS02CD/page_46.htm
• Repeat until there are no more nodes.
• Process the current node
• Move to the next node.
7
Representation of
linear array in memory
• To implement array data structure,
memory bytes must be reserved and the
accessing functions must be coded.
• In case of linear arrays, the declaration
statements tell how many cells are needed to
store the array. Each box represents the
amount of memory needed to hold
one array element.
• Pointers hold the memory address of other
data and are represented by a black disk with
an arrow pointing to the data it references.
https://slideplayer.com/slide/7853049/
• The actual array variable, LA in this example,
is a pointer to the memory for all of its
elements.
8
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.
9
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
10
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
11
Linear & Binary Search
• 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
• 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.
12
Searching – Linear Search Algorithm
• 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.
Set K : = 1, LOC : =0.
1. Repeat steps 3 and 4 while LOC = 0 and K<=N:
2. If ITEM = DATA[K], then : Set LOC : =K .
3. 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.
13
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.
14
Applications
• Array can be used for sorting elements, can perform matrix operation
& can be used in CPU scheduling.
• Stack is used in Expression evaluation.
• Disk Scheduling.
15
• Application:
Two of the most essential applications performs on an array or a record
are searching and sorting.
• Searching: Each index in a parallel array corresponds to the data
belonging to the same entity in a record. Thus, for searching an entity
based on a specific value of an attribute, e.g. we need to find the name
of a person having height >200 cms in the above example. Thus, we
search for the index in the height array having value greater than 200.
Now, once we have obtained the index, we print the values of the index
in the first_name and last_name arrays. This way searching becomes an
easy task in parallel array.
• Sorting: Now, using the same fact that each index corresponds to data
items in different arrays corresponding to the same entity. Thus, we sort
all arrays based on the same criteria. For example, in the above-displayed
example, we need to sort the people in increasing order of their
respective heights. Thus, when we swap the two heights, we even swap
the corresponding values in other arrays using the same index. 16
REFERENCES
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Goodrich, Michael T., Tamassia, Roberto, and Mount, David M., “Data Structures and Algorithms in C++”, Wiley Student
Edition.
• https://www.tutorialspoint.com/data_structures_algorithms/algorithms_basics.htm
• https://www.cs.utexas.edu/users/djimenez/utsa/cs1723/lecture2.html
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Lipschutz, Seymour, “Data Structures”, Schaum's Outline Series, Tata McGraw Hill.
• Gilberg/Forouzan,” Data Structure with C ,Cengage Learning.
• Augenstein,Moshe J , Tanenbaum, Aaron M, “Data Structures using C and C++”, Prentice Hall of India
17
THANK YOU