0% found this document useful (0 votes)
12 views

Lecture #03

my

Uploaded by

mohanned3909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture #03

my

Uploaded by

mohanned3909
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

ARRAYS

ASYMPTOTIC NOTATION
• Understanding how to measure the efficiency of algorithms is crucial
in computer science. Asymptotic Notation in Data Structure helps
describe the running time or space requirements of an algorithm as
the input size grows.
• Let’s learn the basics of asymptotic notation, including Big O, Omega,
and Theta notations. Here, we have provided clear explanations and
practical examples to help you understand how to analyze and
compare the performance of different algorithms effectively.
WHAT IS ASYMPTOTIC NOTATION?
(DEFINITION)
• Asymptotic Notation in Data Structure is a way to describe how the time or
space of an algorithm grows as input size grows. It helps us understand
how efficient an algorithm is when we have very large inputs.
• Imagine you have different ways to solve a problem, like sorting a list of
numbers. Some ways are fast when the list is short, but they might
become very slow when the list is long. Asymptotic notation helps us
compare these methods by focusing on how they perform when the input
size gets really big.
• In simple terms, asymptotic notation helps us predict how an algorithm
will perform and make better choices when writing programs.
TYPES OF ASYMPTOTIC NOTATION
IN DATA STRUCTURE
• 1. Big O Notation (O)
• Big O Notation is used to describe the upper bound of an algorithm's running time. It tells us
the maximum time an algorithm could take to complete, given the size of the input.
• How it Measures the Upper Bound?
• Big O notation focuses on the worst-case scenario. It helps us understand the maximum time
or space an algorithm will require, regardless of the input's specifics.
• Common Examples:
• O(1): Constant time. The algorithm takes the same amount of time regardless of the input
size. Example: Accessing an element in an array.
• O(n): Linear time. The algorithm's running time grows linearly with the input size. Example:
Iterating through a list.
• O(n^2): Quadratic time. The running time grows quadratically as the input size increases.
Example: Bubble sort.
Cont.
• 2. Omega Notation (Ω)
• Omega Notation is used to describe the lower bound of an algorithm's running
time. It tells us the minimum time an algorithm will take to complete, given the
size of the input.
• How it Measures the Lower Bound?
• Omega notation focuses on the best-case scenario. It helps us understand the
least amount of time or space an algorithm will require.
• Common Examples:
• Ω(1): Constant time. The algorithm takes at least a constant amount of time,
regardless of the input size.
• Ω(n): Linear time. The algorithm takes at least linear time as the input size grows.
Cont.
• 3. Theta Notation (Θ)
• Theta Notation is used to describe the exact bound of an algorithm's running time. It
tells us the average-case scenario, where the running time is bound both above and
below by the same function.
• How it Measures the Exact Bound?
• Theta notation focuses on the typical running time of an algorithm. It shows that the
algorithm's running time is both at least and at most a certain function of the input
size.
• Common Examples:
• Θ(1): Constant time. The algorithm takes a constant amount of time, regardless of the
input size.
• Θ(n): Linear time. The algorithm's running time grows linearly with the input size.
LEARNING OUTCOME
CLO_1:
Describe basic ADTs (stack, queue, array, list, node list, priority
queue, tree, map and dictionary) and their related data structure
implementations(array, single linked structure, double linked
structure, heap, hash table, binary search tree, AVL tree).

College of Computer Science & Information Systems, Najran Un 7


iversity, KSA
Arrays
 An array is the simplest type of data structure.
Array consists of an ordered collection of elements all of the same
type.
Each array is given a name and the elements of the array are
accessed with reference to their position.
Each element in the array has unique position which is called
index.
Array name and index directly reference the elements within
array.

College of Computer Science & Information Systems, Najran Un 8


iversity, KSA
Arrays (cont.)
Following notation is used:
array-name[element-index]
For example, the elements of an array A are denoted as:
A1, A2, A3, ………….., An
Or
A(1), A(2), A(3),……. A(n)
Or
A[1], A[2], A[3], …………, A[n]

College of Computer Science & Information Systems, Najran Un 9


iversity, KSA
Arrays Size
Maximum number of elements in the array is the size of the array.
 Index of first element of array is called its lower bound(LB) and
index of the last element is called its upper bound (UB).
Index of array can begin at 0 or 1. #########
Maximum number of elements in a one-dimensional array is
computed as:
Maximum number of elements = UB – LB + 1

College of Computer Science & Information Systems, Najran Un 10


iversity, KSA
Arrays Size (cont.)
Example:
In C++ the index of the first element is 0, Thus an array “abc” of
integer type having 10 elements is declared as:
int abc[10]; (In Java we will see in labs)

LB UB
9 8 7 6 5 4 3 2 1 0

In above example index of first element is 0 and that of the last element index is 9. Thus,
Number of elements in the array = UB – LB +1
=9–0+1
= 10

College of Computer Science & Information Systems, Najran Un 11


iversity, KSA
Representation of Linear Array in Memory
An array occupies a continuous block of memory.
This memory block is divided into equal parts.
Each part of the block represents one element of the array.
In C++, and java these elements are numbered from 0 to n-1,
where n is the total number of elements of the array.
Each element of the array has a unique memory address.
Starting address of array is called base address of the array.
 Elements of an array occupy a continues block of memory, if
base address of the array is known, the address of any element of
array can be easily computed.
College of Computer Science & Information Systems, Najran Un 12
iversity, KSA
Calculating the address of an array
element
Address of an element is determined arithmetically by adding a suitable offset to
the base address of the array.
Suppose X is an array and index for its first element is 1. the base address of array
is D0 and C represents the memory size of each element in bytes. Then memory
address of an element with index K is computed as:
D (X [k]) = D0 + C * ( k - 1)
For example:
if an array X has 5 elements, each of 2 bytes length, then to find out the address of
third element of X when the base address of array is D0 = 200
D (X [ 3]) = 200 + 2 * ( 3 -1 )
= 200 + 4
= 204
College of Computer Science & Information Systems, Najran Un 13
iversity, KSA
Operations on Linear Arrays
 Several operations can be performed on linear arrays. The
most important operations are:
•Traversing. O(n)
•Inserting. O(1)
•Deleting.
•Searching O(n)
•Sorting

College of Computer Science & Information Systems, Najran Un 14


iversity, KSA
Traversing Operation
Algorithm for traversing:
 In traversing, each element of array is
accessed exactly one time for processing. 1. Input values in array ABC

Also called visiting of the array. 2. SUM = 0 [initialize 0 to variable SUM]


An array is traversed to process its data.
3. [Compute sum of array ABC]
For example: REPEAT FOR C = 0 to 9
SUM = SUM + ABC[C]
•to compute the sum of values of each element of an [END of Loop]
array, all elements of the array are accessed exactly
once and their values are added. 4. PRINT SUM
•To print values of each elements of an array, each
element of the array is accessed exactly one and 5. EXIT
values are printed.

College of Computer Science & Information Systems, Najran Un 15


iversity, KSA
Inserting Operation
 In inserting operation, new items are added into an array.
A new item can be added:
• At the end of an array without disturbing other elements of the array if
there is an empty element.
•At some specified location within the array.

College of Computer Science & Information Systems, Najran Un 16


iversity, KSA
Inserting at the End of Array
 Consider an array “Temp” have 10
elements with its first three elements
having values as shown below:
Temp 36 72 66
Value
9 8 7 6 5 4 3 2 1 0
Index
To insert value 78 at the end of the array, the assignment statement
is written as:
Temp[3] = 78
New value can be inserted in the above
array from fourth to tenth element as
these elements are empty.
College of Computer Science & Information Systems, Najran Un 17
iversity, KSA
Inserting Value at Specified Location in an Array
Algorithm to
forinsert
traversing:
a value at location
 To insert new value , the values of all the existing elements are moved index in array ABC having N elements:
1. Input values in array ABC
begin
one step forward or backward to make space for the new value.
 Example: 2. SUM = 0 [initialize 0 to variable SUM]
N=N+1
Value 18 12 78 26 25 36 72 66
Index 9 8 7 6 5 4 3 2 1 0
3. [Compute
SEEk location index
sum of array ABC]
REPEAT FOR C = 0 to 9
Want to insert value 10 at index 5
Move elements Forward For ALL elements
SUM = SUMA[index]
+ ABC[C]
to A[N]
[END of Loop]
Now there is no value at index 5, Store 10 at index 5 Move to next location
66 72 36 25 26 78 12 18 4. PRINT SUM
0 1 2 3 4 5 6 7 8 9
5. EXIT = New_element
A[index]
66 72 36 25 26 10 78 12 18
End
0 1 2 3 4 5 6 7 8 9

College of Computer Science & Information Systems, Najran Un 18


iversity, KSA
Deleting Operation
 In deleting operation, the element(s) of an array are removed.
 Deletion can be performed at the end of array.
 At any position in the array.

College of Computer Science & Information Systems, Najran Un 19


iversity, KSA
Deleting Items at the End of Array
 To remove the last element from the array, the last element is accessed and deleted by
placing null value or by decreasing the size of array by one element if it is the last element of
the array.
Example: (Consider an array name: Country)
Value Saudi Arabia UAE Pakistan England Bahrain
Index 1 2 3 4 5

To remove the last element of array (i.e. Bahrain) move to the last element
and then delete it by placing the null value.

College of Computer Science & Information Systems, Najran Un 20


iversity, KSA
Deleting Items from specified Location of
Array
 To remove an item from a specified location within an array, the item from that location up
to the end of array are moved one location towards the beginning of the array.
The size of array also decreased.
Example: (Consider an array name : Country)
Value Saudi Arabia UAE India England Bahrain
Index 1 2 3 4 5

To delete India at position 3, the contents of locations 4 and 5 are moved one
step towards the beginning of the array. Thus England will be shifted to
location 3 and Bahrain will be shifted to location 4.

College of Computer Science & Information Systems, Najran Un 21


iversity, KSA
Two dimensional Arrays
Two dimensional arrays consists of rows and columns.
It is also called table or matrix.
The elements of a two dimensional array are referenced by two subscripts or index values.
The elements of array are denoted as:
A [ r, c ]
r – represents the row number
c – represents the column number
A two-dimensional array “abc” having 2 rows and 3 columns is shown below:

(0,0) (0,1) (0,2)


(1,0) (1,1) (1,2)

College of Computer Science & Information Systems, Najran Un 22


iversity, KSA
Two dimensional Arrays (cont.)
The total number of elements of a two-dimensional array having
m rows and n columns is:
m*n.
For example, an array having 2 rows and 4 columns has 2 * 4 = 8
elements.
Each programming language has different rules to declare multi-
dimensional arrays. In C++, a two dimensional array “temp” of
integer data type having 6 rows and 4 columns is declared as:
int temp [6] [4];

College of Computer Science & Information Systems, Najran Un 23


iversity, KSA
Advantages & Disadvantages of Array

Advantages:

It is used to represent multiple data items of same type by using only single name.
 It can be used to implement other data structures like linked lists, stacks, queues, trees,
graphs etc.
2D arrays are used to represent matrices.
Disadvantages:

 We must know in advance that how many elements are to be stored in array.
 Array is static structure. It means that array is of fixed size. The memory which is allocated to
array can not be increased or reduced.
 Since array is of fixed size, if we allocate more memory than requirement then the memory space
will be wasted.
 The elements of array are stored in consecutive memory locations. So insertions and deletions are
very difficult and time consuming.
College of Computer Science & Information Systems, Najran Un 24
iversity, KSA
?
?
?
?
QUESTIONS? ?
?
?
?
?

College of Computer Science & Information Systems, Najran Un 25


iversity, KSA

You might also like