(BASIC)
DAY 2
TOPICS TO BE COVERED
● Day 2 :
○ Arrays
○ Time and Space Complexity
○ Sorting - Bubble Sort
○ Searching
○ Function
○ Recursion
Arrays
Arrays are used to store multiple
values in a single variable, instead of
declaring separate variables for each
value.
To declare an array, define the
variable type, specify the name of the
array followed by square brackets and
specify the number of elements it
should store:
Array Declaration and Initialization
Why does Array indexing start from 0
We can also use loop to input in array
Asymptotic Notations
● Big O Notation : Worst Case
● Theta Notation ⊖ : Average Case
● Big Omega Notation : Best Case
How to Calculate Time Complexity ?
Time Complexity - O(n); Time Complexity - O(n2);
In this case value of n is 10
What will be the time
complexity if we have to
calculate sum of first n
natural numbers ?
(If O(n) then is there a way to reduce it ?)
Yes there is !!
Just use the Sum of Natural Number Formula
Now the time complexity
is reduced to -
O(1)
Sorting
A Sorting Algorithm is used to rearrange a given array or list of elements in an
order.
● Bubble Sort
● Selection Sort
● Insertion Sort
● Merge Sort
● Quick Sort
● Heap Sort
● Radix Sort
● In-built Sort Function
BUBBLE SORT
- Concept: Compares adjacent elements and swaps them if they’re in the wrong
order, repeatedly passing through the list.
- Repetition: Each pass "bubbles" the largest unsorted element to its correct
position.
- Stable: Maintains the relative order of equal elements.
Time Complexity:
- Best Case: O(n)
- Average/Worst Case: O(n²)
Space Complexity:
- O(1)
Bubble Sort
Linear Search
Binary Search
Repeatedly divides the array in half and checks
the middle
element.
Prerequisite: Array must be sorted.
https://binary-search-visualization.netlify.app/
What is the time
complexity of
Binary Search ?
In binary search, we know that the search space is reduced by half at each step and this
guides us in computing the time complexity.
For an array with n elements, we check if the middle-most element matches the target. If
so, we return True and terminate the search.
But if the middle element does not match the target, we perform binary search on a
subarray of size at most n/2. In the next step, we have to search through an array of size at
most n/4. And we continue this recursively until we can make a decision in a constant time
(when the subarray is empty).
At step k, we need to search through an array of size at most n/(2^k). And we need to find
the smallest such k for which we have no subarray to search through.
Mathematically:
FUNCTION - A function is a block
Of code which only runs when
it is called.
// DEFINE ANY TASK THAT YOU
WANT TO DO MANY TIMES
AND STORE IT.
Types of Functions in C++
1. Library Functions – Predefined functions like sqrt(),
pow(), abs()
from standard libraries.
2. User-Defined Functions – Functions created by the
programmer to
perform specific tasks.
FUNCTION CALLING
Parameters (or arguments) are values passed to functions to perform
operations. They can be classified as follows:
Call by Value
● A copy of the argument is passed to the function.
● Changes inside the function do not affect the original variable.
Pass by Reference
● The function receives a reference to the original variable.
● Changes inside the function reflect on the original variable.
Call by Value
Pass by Reference
Pass by Pointer
● The function receives a pointer to the original variable.
● It allows modifying the original value using dereferencing.
Default Parameters
● Assigns default values to function parameters.
● If no argument is passed, the default value is used.
Pass by Pointer Default Parameters
Ques : Print the table from 5 to 10.
RECURSION
A function is a block of code which only runs when it is called.
THANK YOU ALL!