DAY-2 Basic Programming Workshop
DAY-2 Basic Programming Workshop
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
● 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:
Space Complexity:
- O(1)
Bubble Sort
Linear Search
Binary Search
Repeatedly divides the array in half and checks
the middle
element.
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.
Call by Value
Pass by Reference
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!