2. 1. Linear Data Structures
• A type of data structure that arranges the
data items in an orderly manner where
the elements are either physically or
logically attached adjacently
• Items are traversed sequentially
• Single-level
• Easier to implement
2
4. Physically vs. Logically Linear
• The linear data structure whose successive
components occupy consecutive memory
locations are called physically linear data
structures. Memory utilization is inefficient.
• Example: Arrays
• The linear data structure whose components are
accessed in some sequence but are not
necessarily placed in consecutive memory
locations are called logically linear data
structures.
• Example: Linked Lists
• Queues and Stacks can be either physical or logical depending
upon the data structure being used for implementation.
4
5. 2. Arrays
• An Array is a finite sequence of storage cells, for which the
following operations are defined:
• Create an Array A with a storage for N items
– A[i]=item; stores an item in the ith position of
the Array A
– A[i] returns the value of the item stored in the
ith position of the Array A
5
6. Array as an ADT
• Data: A fixed-size sequence of
elements, all of the same type
• Operations:
Direct access to each element in the
array by specifying its position so
that values can be retrieved from or
stored in that position.
6
10. Static Arrays
10
const int size=5;
int arr[size];
for(int i=0;i<size;i++)
{
cin>>arr[i];
}
int arr[5];
for(int i=0;i<5;i++)
{
cin>>arr[i];
}
int arr[]={1,2,3,4,5}; int size = 5;
int arr[size];
int size;
cin>>size;
int arr[size];
11. Dynamic Arrays
11
int *a;
int size=5;
a = new int[size];
for(int i=0;i<size;i++)
{
cin>>a[i];
}
int *a;
int size;
cin>>size;
a = new int[size];
for(int i=0;i<size;i++)
{
cin>>a[i];
}
delete []a;
12. Accessing Array Elements
10/24/2023 12
int main()
{
int a[] = {1, 2, 3, 4, 5};
int *p;
p=a;
for(int i=0; i<5; i++)
{
//Subscript notation with array name
cout<<a[i];
//Subscript notation with pointer name
cout<<p[i];
//Offset notation using array name
cout<<*(a+i);
//Offset notation using pointer name
cout<<*(p+i);
return(0);
13. 3. Arrays as Data Structures
(Applications)
• Sorting items
– Ascending order
– Descending order
– Lexicographical order
• Searching items
– Linear Search (Unsorted Arrays)
– Binary Search (Sorted Arrays)
13
18. Linear Search
18
Work for both sorted and unsorted array.
For example; We have given an array (1to6) and
indexes are (0to5). Need to search element (4).
21. Example 1: Binary Search
21
Work only for sorted array.
For example; We have given an array (1to6) and indexes are
(0to5). Need to search element (4).
Medium = low + high /2
= 0+5/2 = 2
2
1 4
3 6
5
2
1 4
3 6
5
0 1 2 3 4 5
0 1 2 3 4 5
Element 3 < 4. Therefore delete
(0, 1, 2)
22. Example 1: Binary Search
22
Work only for sorted array.
For example; We have given an array (1to6) and indexes are
(0to5). Need to search element (4).
= 3+5/2 = 4
4 6
5
3 4 5
4 6
5
3 4 5
Element 5 > 4. Therefore delete
(4, 5)
4
3
29. 4. Greatest common divisor
• The greatest common divisor (GCD) of two or
more numbers is the greatest common factor
number that divides them, exactly. It is also
called the highest common factor (HCF). For
example, the greatest common factor of 15
and 10 is 5, since both the numbers can be
divided by 5.
• 15/5 = 3
• 10/5 = 2
30. Greatest common divisor
• Suppose, 4, 8 and 16 are three numbers. Then
the factors of 4, 8 and 16 are:
• 4 → 1,2,4
• 8 → 1,2,4,8
• 16 → 1,2,4,8,16
34. GCD Algorithm (Pseudocode)
• The Euclidean Algorithm for calculating GCD of two numbers A and B can be
given as follows:
• If A=0 then GCD(A, B)=B since the Greatest Common Divisor of 0 and B is B.
• If B=0 then GCD(a,b)=a since the Greates Common Divisor of 0 and a is a.
• Let R be the remainder of dividing A by B assuming A > B. (R = A % B)
• Find GCD( B, R ) because GCD( A, B ) = GCD( B, R ). Use the above steps again.
37. Asymptotic Analysis
• Asymptotic Notations: Asymptotic notations are the
mathematical notations used to describe the running
time of an algorithm when the input tends towards a
particular value or a limiting value.
• For example: In bubble sort, when the input array is
already sorted, the time taken by the algorithm is
linear i.e. the best case.
• But, when the input array is in reverse condition, the
algorithm takes the maximum time (quadratic) to sort
the elements i.e. the worst case.
38. Asymptotic Analysis
• When the input array is neither sorted nor in
reverse order, then it takes average time.
These durations are denoted using asymptotic
notations.
• There are mainly three asymptotic notations:
• Big-O notation
• Omega notation
• Theta notation
39. Big-O Notation (O-notation)
• Big-O notation
represents the
upper bound of
the running time
of an algorithm.
Thus, it gives the
worst-case
complexity of an
algorithm.
40. Omega Notation (Ω-notation)
• Omega notation
represents the
lower bound of
the running time
of an algorithm.
Thus, it provides
the best case
complexity of an
algorithm.
41. Theta Notation (Θ-notation)
• Theta notation
encloses the function
from above and below.
Since it represents the
upper and the lower
bound of the running
time of an algorithm, it
is used for analyzing
the average-case
complexity of an
algorithm.
44. Code Example: C++ program to find
time complexity for single for loop
Output
Editor's Notes
Index = element
https://www.youtube.com/watch?v=qTb1sZX74K0
Static Array work as stack. [Delete at runtime]
Dynamic Array work as Heap. [Donot delete easily. Switch-off PC]
Heap: Heap storage is used to allocate storage that has a lifetime that is not related to the execution of the current routine.