Asymptotic Analysis (Big-O Notation) : Big O Notation Is Used in Computer Science To Describe The Performance
Asymptotic Analysis (Big-O Notation) : Big O Notation Is Used in Computer Science To Describe The Performance
Asymptotic Analysis (Big-O Notation) : Big O Notation Is Used in Computer Science To Describe The Performance
● Basic
● Particularly for Big-O notation - 3. Analysis — Problem Solving with Algorithms and
Data Structures
O(1)
O(1) describes an algorithm that will always execute in the same time (or
space) regardless of the size of the input data set.
O(N)
O(N²)
O(2^N)
Logarithms
In conclusion
Hopefully this has helped remove some of the mystery around Big O
notation and many of the common growth functions. A grasp of Big O is an
important tool when dealing with algorithms that need to operate at scale,
allowing you to make the correct choices and acknowledge trade-offs
when working with different data sets.
● Time and space complexity analysis of recursive programs - using factorial - Time
and space complexity analysis of recursive programs - using factorial
Practice Problems
● Check some MCQs on space and time complexity here.
● You can see some problems with solutions here: Time Complexity of an algorithm.
Arrays
● Data structure Tutorial: Array
Array is a collection of homogeneous data elements. It is a very simple data
structure. The elements of an array are stored in successive memory location. Array
is referred by a name and index number. Array is nice because of their simplicity and
well suited for situations where the number is known. Array operation :
Traverse
Insert
Delete
Sort
Search
There are two types of array. One dimensional array and multi dimensional
array.One dimensional array This type of array represents and stores in linear form.
Array index start with zero.
We can use store integer type of data to the array arr using above segment.
----------
C++ implement :
void traverse(int arr[])
{
for(int i=0; i<10; i++) cout<<arr[i];
}
Insertion : Inserting an element at the end of a linear array can be easily done
provided the memory space allocated for the array is large enough to accommodate
the additional element. Inserting an element in the middle . . Algorithm :
Insertion(arr[], n, k, item) here arr is a linear array with n elements and k is index we
item insert. This algorithm inserts an item to kth in index in arr.
Step 1:Start
Step 2: Repeat for i=n-1 down to k(index)
Shift the element dawn by one position] arr[i+1]=arr[i];
[End of the loop]
Step 3: set arr[k] = item
Step 4: n++; Step 5 : Exit.
C++ implement :
void insert(int arr[], int n, int k, int item)
{
for(int i=n-1; i>=k; i--)
{
arr[i]=arr[i+1];
}
arr[k] = item;
n++;
}
C++ implementation :
Searching : Searching means find out a particular element in linear array. Linear
search and binary search are common algorithms for linear arrays. We discuss linear
search and binary search.
Linear search Algorithm : Linear search is a simple search algorithm that checks
every record until it finds the target value
Algorithm: LinearSeach(arr, n, item)
Step 1:Start.
Step 2: Initialize loc=0;
Step 3: Repeat for i=0 upto n-1 if(arr[i]==item) loc++; [End of the loop]
Step 4: if loc is not zero then print found otherwise print not found.
Step 5 : Exit.
C++ implementation :
if(loc) cout<<"Found"<<endl;
else cout<<"Not found"<<endl
}
Binary search : Binary search is available for sorted array. It compares the target
value to the middle element of the array; if they are unequal, the half in which the
target cannot lie is eliminated and the search continues on the remaining half until it
is successful.
C++ implementation :
int binarySearch(int[] a, int n, int item)
{
int low = 0;
int high = n - 1;
while(low<=high){
{
int mid = (low + high)/2;
if (a[mid] == item) return mid;
else if (a[mid] < item) low = mid + 1;
else high = mid - 1;
}
return -1;
}
Sorting : There are various sorting algorithm in linear array. We discuss bubble sort
and quick sort in this post.
Bubble Sort: Bubble sort is a example of sorting algorithm . In this method we at first
compare the data element in the first position with the second position and arrange
them in desired order.Then we compare the data element with with third data
element and arrange them in desired order. The same process continuous until the
data element at second last and last position.
Algorithm : BubbleSort(arr,n)
Step 1:Start
Step 2: Repeats i=0 to n
Step 3: Repeats j=0 to n
if(arr[j]>arr[j+1]) then interchange arr[j] and arr[j+1]
[End of inner loop]
[End of outer loop]
Step 4: Exit.
C++ implement :
void BubbleSort(int arr, int n)
{
for(int i=0; i<n-1; i++)
{
for(int j=0; j<n-1; j++)
{
if(arr[j]>arr[j+1]) swap(arr[j],arr[j+1]);
}
}
}
Quick Sort:
Quick sort is a divide and conquer paradism. In this paradism one element is to be
chosen as partitioning element .
We divide the whole list array into two parts with respect to the partitiong elemnt .
The data which are similar than or equal to the partitining element remain in
the first part and data data which are greater than the partitioning element
remain in the second part. If we find any data which is greater than the partitioning
value that will be transfered to the second part., If we find any data whichis smaller
than the partitioning element that will be transferred to first part.
Transferring the data have been done by exchanging the position of the the data
found in first and second part. By repeating this process ,
we can sort the whole list of data.
Algorithm: QUICKSORT(arr, l, h)
if l<h then pi ← PARTITION(A, l, h)
QUICKSORT(A, l, pi–1)
QUICKSORT(A, pi+1, h)
C++ implementation :
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n, arr[100];
cin >> n;
for(int i=0; i<n; i++)
{
cin>>arr[i];
}
return 0;
}