Asymptotic Analysis (Big-O Notation) : Big O Notation Is Used in Computer Science To Describe The Performance

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

Asymptotic analysis (Big-O notation)

● Basic

● Time complexity of a computer program - Time complexity of a computer program

● Big-O notation in 5 minutes - Big-O notation in 5 minutes — The basics

● Definition of Big O notation - Intro to theoretical computer science - Definition Of Big


O Notation - Intro to Theoretical Computer Science

● Algorithms Lecture 1 -- Introduction to asymptotic notations - Algorithms Lecture 1 --


Introduction to asymptotic notations

● Measuring the efficiency of algorithms - Measuring the efficiency of algorithms

● Particularly for Big-O notation - 3. Analysis — Problem Solving with Algorithms and
Data Structures

● A beginner’s guide to Big O notation

Big O notation is used in Computer Science to describe the performance


or complexity of an algorithm. Big O specifically describes the worst-case
scenario, and can be used to describe the execution time required or the
space used (e.g. in memory or on disk) by an algorithm.

Anyone who’s read Programming Pearls or any other Computer Science


books and doesn’t have a grounding in Mathematics will have hit a wall
when they reached chapters that mention O(N log N) or other seemingly
bizarre syntax. Hopefully this article will help you gain an understanding of
the basics of Big O and Logarithms.

As a programmer first and a mathematician second (or maybe third or


fourth) I found the best way to understand Big O thoroughly was to
produce some examples in code. So, below are some common orders of
growth along with descriptions and examples where possible.

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.

bool IsFirstElementNull(IList<String> elements)


{
return elements[0] == null;
}

O(N)

O(N) describes an algorithm whose performance will grow linearly and in


direct proportion to the size of the input data set. The example below also
demonstrates how Big O favours the worst-case performance scenario; a
matching string could be found during any iteration of the for loop and the
function would return early, but Big O notation will always assume the
upper limit where the algorithm will perform the maximum number of
iterations.

bool ContainsValue(IEnumerable<string> elements, string


value)
{
foreach (var element in elements)
{
if (element == value) return true;
}
return false;
}

O(N²)

O(N²) represents an algorithm whose performance is directly proportional


to the square of the size of the input data set. This is common with
algorithms that involve nested iterations over the data set. Deeper nested
iterations will result in O(N³), O(N⁴) etc.

bool ContainsDuplicates(IList<string> elements)


{
for (var outer = 0; outer < elements.Count; outer++)
{
for (var inner = 0; inner < elements.Count; inner+
+)
{
// Don't compare with self
if (outer == inner) continue;

if (elements[outer] == elements[inner]) return


true;
}
}
return false;
}

O(2^N)

O(2^N) denotes an algorithm whose growth doubles with each addition to


the input data set. The growth curve of an O(2^N) function is exponential
— starting off very shallow, then rising meteorically. An example of an
O(2^N) function is the recursive calculation of Fibonacci numbers:

int Fibonacci(int number)


{
if (number <= 1) return number;

return Fibonacci(number - 2) + Fibonacci(number - 1);


}

Logarithms

Logarithms are slightly trickier to explain so I’ll use a common example:

Binary search is a technique used to search sorted data sets. It works by


selecting the middle element of the data set, essentially the median, and
compares it against a target value. If the values match, it will return
success. If the target value is higher than the value of the probe element, it
will take the upper half of the data set and perform the same operation
against it. Likewise, if the target value is lower than the value of the probe
element, it will perform the operation against the lower half. It will continue
to halve the data set with each iteration until the value has been found or
until it can no longer split the data set.

This type of algorithm is described as O(log N). The iterative halving of


data sets described in the binary search example produces a growth curve
that peaks at the beginning and slowly flattens out as the size of the data
sets increase e.g. an input data set containing 10 items takes one second
to complete, a data set containing 100 items takes two seconds, and a
data set containing 1,000 items will take three seconds. Doubling the size
of the input data set has little effect on its growth as after a single iteration
of the algorithm the data set will be halved and therefore on a par with an
input data set half the size. This makes algorithms like binary search
extremely efficient when dealing with large data sets.

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.

● Big O notation, gayle Laakman McDowell - Big O Notation

● Big O notation - Big O notation

● Time and space complexity analysis of recursive programs - using factorial - Time
and space complexity analysis of recursive programs - using factorial

● Asymptotic Notation - Asymptotic notation

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.

Declaration : datatype arrayname[size];


int arr[10];

Input array : for(int i=0; i<10; i++) cin>>arr[i];

We can use store integer type of data to the array arr using above segment.

----------

Traverse : Traversing can easy in linera array. Algorithm:

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++;
}

Deletion : Deletion is very easy on linear array.

Algorithm : Deletion(arr, n, k) Here arr is a linear array with n number of items. K is


position of elememt which can be deleted.
Step 1:Start
Step 2: Repeat for i=k upto n
[Move the element upword] arr[i]=arr[i+1];
[End of the loop]
Step 3: n--;
Step 4 : Exit.

C++ implementation :

void deletion(int arr[], int n, int k)


{
for(int i=k; i<n; i++)
{
arr[i] = arr[i+1];
}
n--;
}

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 :

void linear search(int arr[], int n, item)


{
for(int i=0; i<n-1; i++)
{
if(arr[i]==item) loc++;
}

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.

Algorithm : BinarySeach(arr, n, item)


Step 1:Start
Step 2: Initialize low = 0 and high = n-1;
Step 3: While loop low<=high
mid = (low + high)/2;
if (a[mid] == item) return mid;
else if (a[mid] < item) low = mid + 1;
else high = mid - 1;
Step 4: If item is not found in array return -1. Step 5: End.

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 :

int partition(int arr[], int start, int end)


{
int pivotValue = arr[start];
int pivotPosition = start;
for (int i=start+1; i<=end; i++)
{
if (pivotValue > arr[i])
{
swap(arr[pivotPosition+1], arr[i]);
swap(arr[pivotPosition] , arr[pivotPosition+1]);
pivotPosition++;
}
}
return pivotPosition;
}

void quickSort(int arr[], int low, int high)


{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

C++ example for simple sorting program with stl function:

#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];
}

sort(arr, arr + n);


for (int i=0; i<n; i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;

return 0;
}

● Lecture Notes on Array

● Array Data Structure: GeeksForGeeks

Arrays Practice Problems


● Little Elephant and Candies , Editorial

● Chef and Notebooks , Editorial

● Salary:The Minimum Number Of Moves , Editorial

● CHN15A:Mutated Minions , Editorial

● RAINBOWA:Chef and Rainbow Array, Editorial

● FRGTNLNG:Forgotten Language , Editoria

● Cops and the Thief Devu, Editorial

You might also like