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

DSA_Unit-1_-_Keypoints[1]

The document outlines key concepts in data structures, including their classification into primitive and non-primitive types, as well as linear and non-linear structures. It also discusses algorithm analysis, emphasizing the importance of understanding time and space complexity through asymptotic notation (Big-O, Omega, and Theta). Additionally, it compares linear and binary search algorithms, highlighting their differences in complexity and requirements for sorted data.

Uploaded by

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

DSA_Unit-1_-_Keypoints[1]

The document outlines key concepts in data structures, including their classification into primitive and non-primitive types, as well as linear and non-linear structures. It also discusses algorithm analysis, emphasizing the importance of understanding time and space complexity through asymptotic notation (Big-O, Omega, and Theta). Additionally, it compares linear and binary search algorithms, highlighting their differences in complexity and requirements for sorted data.

Uploaded by

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

DSA Unit-1 - Keypoints

Data-Structures
Data can be stored in different ways. Data structure is the logical or mathematical model of a
particular organization of data.

Choice of a particular data structure depends on following considerations :-

1. Should be suitable enough to mimic the actual relationships of data in the real world. e.g.
Graphs : Friend requests in Social media, Trees : hierarchical data like storing roles
2. Should be simple enough that one can effectively process the data however necessary.
3. Each data-structure has it's pros and cons, time complexity and space complexity. So
everything should be considered before using it.

Basic Terminologies : Elementary Data organization


Data: Data are simple values or sets of values.
Data items: refers to a single unit of values.
Elementary items: Data items that cannot be divided further into sub-items.
Entity: An entity is something that has certain attributes or properties which may be
assigned values. E.g. Student can be an Entity with further attributes such as name, roll,
sex.

Classification of Data Structures

Data Structures are generally classified into

Primitive Data Structures: Fundamental data types which is defined within the
programming language itself. These data types cannot be further divided down, hence the
name Simple data type. E.g. int, float, char, boolean etc.
Non-Primitive Data Structures: User-defined data types which are created using primitive
data structures. E.g. Linked-List, Stack, Queue, Trees and Graphs.

On the basis of structure and arrangement of data, Non-Primitive Data structure further
classified into :-

Linear Data Structure


Non-Linear Data Structure
1. Linear Data Structure :- A data structure is said to be linear if it's elements form a
sequence. There are basically two ways of representing such linear structure in memory :-
Sequential memory allocation : Arrays
Pointers/links : Linked List
E.g. Arrays, Stacks, Queues, Linked List
2. Non-Linear Data Structure :- A data structure is said to be non-linear if the data is not
arranged in a sequence or a linear manner. Insertion and deletion of data is not possible in
linear fashion.
Mainly used to represent hierarchical relationship.
E.g. Trees, Graphs

Analysis of Algorithms
Algorithm analysis is an important part of development, which provides theoretical estimation
for the required resources both time and space. It helps us compare between 2-3 different
choices of algorithms to choose whichever one is suitable for our use-case.

Importance of Algorithm analysis :-


Prediction of behaviour of an algorithm without actual implementation.
Impossible to predict the exact behaviour of an algorithm, too many influencing factors. But
provides good enough rough estimation.
By analyzing different algorithms, we can compare them to determine the best one for our
use-case.

We usually talk about Best case, Worst case and Average case while discussing about an
algorithm.

Asymptotic Analysis : Advantages & Disadvantages


Advantages:

1. Provides a high-level understanding of how an algorithm performs with respect to input


size.
2. Useful tool for comparing the efficiency of different algo's and selecting the best one for
use-case.
3. Prediction of performance on larger input sizes.
4. Relatively easy to perform and requires only basic mathematical skills.

Disadvantages:

1. Doesn't provide an accurate running time(or space usage).


2. Can sometimes be misleading, as two algo's having same asymptotic complexity may have
different running time.
3. Not always straightforward to determine the best asymptotic complexity, as there maybe
trade-offs between time and special complexity.

Asymptotic Analysis : Big-O, Omega(Ω) and Theta(θ)


Notation
Mathematical notations used to describe the running time of an algorithm when the input tends
towards a limiting value.

We evaluate the performance of an algorithm in terms of input size.


We don't actually measure the actual running time.
We Calculate how the time (or space) taken by an algorithm increases with input size.

1. Omega(Ω) Notation(Best Case) : Provides a lower bound on the growth rate of an


algorithm's running time (or space usage). Represents the best-case scenario.

Let function be f(n), for any value of n, the minimum time required by the
algorithm is given as :-

Ω(g(n)) = { f(n): there exist positive constants c and n0


such that 0 ≤ cg(n) ≤ f(n) for all n ≥ n0 }

2. Big-O Notation(Worst Case) : Provides an upper bound on the growth rate of an


algorithm's running time. Represents the worst-case scenario.

Let the function be f(n) belongs to the set O(g(n)), for any value of n running
time of an algorithm doesn't cross the time provided by O(g(n)) as per:-

O(g(n)) = { f(n): there exist positive constants c and n0


such that 0 ≤ f(n) ≤ cg(n) for all n ≥ n0 }

3. Theta(θ) Notation(Average Case) : Provides both an upper and lower bound on the growth
rate of an algorithm's running time. Represents average-case scenario.

For a function g(n), Θ(g(n)) is give as per :-

Θ(g(n)) = { f(n): there exist positive constants c1, c2 and n0


such that 0 ≤ c1g(n) ≤ f(n) ≤ c2g(n) for all n ≥ n0 }

Searching Algorithms :- Linear & Binary Search


1. Linear Search: Sequential search algorithm that starts at one end and goes through each
element of a list until the desired element is found.
Array is traversed and each element is considered as a potential match for the key(no.
to find) and checked for the same.
If element is found equal to the key, the search is successful and the index of that
element is returned.
If no match is found, the search outputs -1, usually meaning Element not found!
No need to sort the array first.
Big-O Complexity : O(n)

int LinearSearch(int key, int arr[], int length){


for(int i=0; i<length; i++){
if (arr[i]==key) return i;
}
return -1;
}

2. Binary Search: Divide and Conquer algorithm used in a sorted array by repeatedly
dividing the search interval in half.
Data structure must be sorted.
Divide the search space into two halves by finding the middle index mid.
Compare the element at mid with key value if equal then return otherwise delete the
half where value cannot be (like greater or smaller than key value).
If no match is found, return -1, again signifying absence of target value in structure.
Big-O Complexity : O(log n)

int BinarySearch(int arr[], int low, int high, int key){


if(low>high) return -1;
int mid = (low+high)/2;

if (key==arr[mid]) return mid;


else if (key<arr[mid]) return BinarySearch(arr, low, mid-1, key);
else return BinarySearch(arr, mid+1, high, key);
}

Difference between Linear & Binary Search


Linear Search Binary Search
Data need not be sorted Data need to be sorted
Sequential Search Half-Interval Search
O(n) O(log n)

Performs Equality comparisons Performs Ordering comparisons


Less Complex More Complex
Very slow, viable only for small dataset Very Fast, even for large dataset

You might also like