0% found this document useful (0 votes)
6 views30 pages

DSA What Is Datastructures ? Algorithim

The document provides an overview of data structures and algorithms, explaining their definitions, types, and importance in optimizing performance and memory usage. It discusses examples of algorithms, such as sorting and searching, and introduces Big O notation for measuring time complexity. Additionally, it outlines a problem-solving approach and various sorting algorithms while emphasizing the significance of efficient code as input sizes grow.

Uploaded by

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

DSA What Is Datastructures ? Algorithim

The document provides an overview of data structures and algorithms, explaining their definitions, types, and importance in optimizing performance and memory usage. It discusses examples of algorithms, such as sorting and searching, and introduces Big O notation for measuring time complexity. Additionally, it outlines a problem-solving approach and various sorting algorithms while emphasizing the significance of efficient code as input sizes grow.

Uploaded by

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

DSA

What is Datastructures ?
In simple words it is way to organise your data
types of datastructures :
Array,linkedlist,stack,binarytree,Graph,Binary Search tree.,
heap, misc, Matrix, Queue, hashing.

ALGORITHIM
 Way to solve a problem or we can say a
procedure and set of rules to solve a
problem.

 For example we have to fetch 10 post out of


1 million in a particular order to user
preference.

Example of ds and algo >


We have to sort numbers in asecending order
Input -> [4,3,2,1] -> Datastructure
STEPS TO ACHIEVE <- ALGORITHIM
Output -> [1,2,3,4]

Why DATA STRUCTURE


AND ALGORITHIM ?
Suppose you have 1 million post out of which
you need only first 10 post.
You want to show post related to user in a
particular order.
Conditions
1.You want to do this as fast as possible
2.You want to optimise memory consumption

Timing our code

Some problem first

With Loop->

function addupto(n) {
let total = 0;
for (let i = 1; i <= n; i++) {
total += i;
}
return total;
}

console.log(addupto(6));

with formula ->


function adduptoV2(n) {
return (n * (n + 1)) / 2;
}

console.log(addupto(5));

which is better ?
the function of formula is better

What does better mean >


 Faster time
 Less Memory (RAM)
 More readable
 Performance is the king

How could we measure that code is


more effiecient than another piece of
code that that does the same job ?

without sort function :-

Sum zero problem


function getSumPairZero(array) {
for (let number of array) {
console.log(number);

for (let j = 1; j < array.length; j++) {


if (number + array[j] === 0) {
return [number, array[j]];
}
}
}
}

const result = getSumPairZero([-5, -4, -3, -2, -1, 0, 2, 4, 6, 8]);


console.log(result);

Counting Operation on Big O


notation
Lets measure the performance of our code by counting
the operation it has, rather than manually timing.

The more number of operations we have in a function


the more it affect to the performance of code.
1. let total = 0; - This is an assignment operation. This happens once, so it's a
constant time operation. Let's call it 1 operation.
2. for (let i = 1; i <= n; i++) {...} - This loop is performed n times. Inside this
loop there are multiple operations:
- let i = 1; - This is another assignment operation. This also happens once, so let's
call it 1 operation.
- i <= n; - This is a comparison operation that happens n times (once for each
iteration of the loop, including the one where it fails and breaks the loop), so we
have n comparison operations.
- i++ - This is both an addition ( i + 1 ) and an assignment ( i = i + 1 ), so we have 2
operations for each iteration of the loop. Hence, we have 2n operations.
- total += i; - This is another addition and assignment operation (similar to i++ ).
Hence, this is 2n operations.
Now, if we add all of these together, we get 1 (for the initial total assignment)
+ 1 (for the initial i assignment) + n (for the loop comparison) + 2n (for the
i increment) + 2n (for the total increment) = 5n + 2 operations. This is the "as
high as" number.
Performance tracker tool link Big O Introduction
(rithmschool.github.io)

Official intro
Big O Notation
Big O Notation is a way to formalize fuzzy
counting.

It allows us to talk formally about how the


runtime of an algorithm grows as the input
grows.

Its about counting number of operations to


perform for how large N is !

Big 0 Notation also referred as time complexity !

Big o notation gives us precise ,numeric, and


objective way of ju dging the performance of our
code.
Why Big o notation and time complexity
matters ?

 It helps us to write better code and becomes


important as your inputs get bigger(EG users/
posts for an app).

Example 1
Cheat sheet to identify time complexity of
different code
if you have an algorithm with O(n) time complexity and you double the size of the input data,
the time taken to execute the algorithm will also approximately double.
Simplfying Big O
Expression
Big O
Shorthands

ANALYSING PERFORMANCE OF ARRAYS AND
OBJECTS
Problem SOLVING APPROACH
Problem solving pattern
Recursion
Searching algorithim
Bubble Sort
Selection sort
Insertion Sort
Merge sort
Quick sort
Radix sort
Intro to datastructures
SPACE COMPLEXITY
How much more memory use (RAM)
Logarithms
Objects through the
lens of Big O
Arrays
Push nd pop are always faster than shift and
unshift.
Big O of array
Methods
Intro to problem
solving
Algorithm and Problem solving Patterns
Step 1 : Understand the problem

Step 2: Explore concrete problems !


Step 3 : Break It down

Step : 4 -
Step 5 : look back and refactor
Problem solving Patterns

You might also like