How To Calculate Big O
How To Calculate Big O
As we can see, the tab.length is our n (or input). Now, the main idea behind the BigO is to find the
worst-case scenario. Returning to our example, the worst-case scenario is that we browse the whole
array and we do not find the searched element. So our complexity is O(n)
Let's take a second example. This time we will have a nested loop.
FUNCTION contain_dup(tab : ARRAY_OF INTEGER) : BOOLEAN
VAR
i,j : INTEGER;
BEGIN
FOR i FROM 0 TO tab.length-1 STEP 1 DO
FOR j FROM 0 TO tab.length-1 STEP 1 DO
IF (i<>j AND tab[i] = tab[j]) THEN
RETURN TRUE
END_IF
END_FOR
END_FOR
RETURN FALSE ;
END
in the example above, we have a nested loop with each one of these loops has a complexity of O(n).
So the whole complexity of our algorithm will be O(n * n ) which equal to O(n²)
Our third example will be the same algorithm as before but with a little change. We'll make the
second loop not starting from the beginning of the array, but it will start from the i position.
FUNCTION contain_dup(tab : ARRAY_OF INTEGER) : BOOLEAN
VAR
i,j : INTEGER;
BEGIN
FOR i FROM 0 TO tab.length-1 STEP 1 DO
FOR j FROM i+1 TO tab.length-1 STEP 1 DO
IF (i<>j AND tab[i] = tab[j]) THEN
RETURN TRUE
END_IF
END_FOR
END_FOR
RETURN FALSE ;
END
With this change, the first loop will keep the complexity of O(n), while the second loop will get a
complexity of O( n - i ).
This is what we call the logarithmic format. So the complexity of this algorithm will be O( n log n )
Simple Sorting
Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent
elements if they are in wrong order. The pass through the list is repeated until the list is sorted. The
algorithm, which is a comparison sort, is named for the way larger elements "bubble" to the top of
the list
Simple Sorting
Selection sort algorithm sorts an array by repeatedly finding the minimum element (considering
ascending order) from unsorted part and putting it at the beginning. The algorithm maintains two
subarrays in a given array.
1. The subarray which is already sorted.
2. Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element from the unsorted subarray is picked up
and moved to the sorted subarray.
Simple Sort Algorithms
Let’s see the example below.
We are going to implement the algorithm of the simple sort and bubble sort.
PROCEDURE swap(VAR xp, VAR yp : INTEGER)
VAR
tmp : INTEGER;
BEGIN
tmp := xp;
xp := yp;
yp := tmp;
END
/* *** Bubble sort *** */
PROCEDURE bubble_sort(VAR tab : ARRAY_OF INTEGER)
VAR
i,j,n : INTEGER;
BEGIN
n := tab.length;
FOR i FROM 0 TO n-2 STEP 1 DO
// Last i elements are already in place
FOR j FROM 0 TO n-i-2 STEP 1 DO
IF (tab[j]>tab[j+1]) THEN
swap(tab[j], tab[j+1])
END_IF
END_FOR
END_FOR
END
5 and 4
Quick Sorting
Merge sort is one of the most efficient sorting algorithms. It works on the principle of Divide and
Conquer. Merge sort repeatedly breaks down a list into several sublists until each sublist consists of
a single element and merging those sublists in a manner that results into a sorted list.
Quick Sorting
Like Merge Sort, QuickSort is a Divide and Conquer algorithm, but it is more efficient as we do not
use extra space to work with. Is the most algorithm build in programming languages. It picks an
element what we call a pivot and then we arrange the items such that all the items smaller than the
pivot are on the left and all the greatest items are on the right, this what we call partitioning, so we
divided the array into two partitions separated by the pivot. I does not matter the order of the
elements in each partition, what only matter that are all smaller or all greater than the pivot. A
partition with only one element considered as sorted partition.
There are many different versions of quickSort that pick pivot in different ways.
1. Always pick last element as a pivot (shown in the example)
2. Always pick first element as a pivot.
3. Pick a random element as a pivot.
4. Pick median as a pivot.
END
END
/*
This function takes the last element as a pivot,
places its element at its correct position in sorted
array, and places all smaller (than the pivot) to left
of pivot and all the greater elements to right of pivot
*/
RETURN b+1 ;
END
Find the pivot element from the given input using median partitioning method. 8, 1, 4, 9, 6, 3, 5, 2,
7, 0.
6
What is the average running time of a quick sort algorithm?
O(N log N)
Linear search
Linear search is a very basic and simple search algorithm. In linear search, we search an element or
value in a given array by traversing the array from the starting, till the desired element or value is
found.
It compares the element to be searched with all the elements present in the array and when the
element is matched successfully, it returns the index of the element in the array, else it return -1.
Linear Search is applied on unsorted or unordered lists, when there are fewer elements in a list.
Features of Linear Search Algorithm
1. It is used for unsorted and unordered small list of elements.
2. It has a time complexity of O(n), which means the time is linearly dependent on the number
of elements, which is not bad, but not that good either.
3. It has a very simple implementation.
Linear search
The time complexity of the linear search algorithm is O(n).
This is its implementation:
FUNCTION linear_search(tab : ARRAY_OF INTEGER, elt : INTEGER) : INTEGER
VAR
j : INTEGER;
BEGIN
j := 0;
WHILE (j< tab.length) DO
IF (tab[j] = elt) THEN
RETURN j; // element is found let's break the loop and return the
index
END_IF
j := j+1; // update the index
END_WHILE
// we reached the end of array without finding the element
RETURN -1 ;// means that we did not find the element
END
Binary Search
Binary Search is used with sorted array or list. In binary search, we follow the following steps:
1. We start by comparing the element to be searched with the element in the middle of the
list/array.
2. If we get a match, we return the index of the middle element.
3. If we do not get a match, we check whether the element to be searched is less or greater than
in value than the middle element.
4. If the element/number to be searched has a greater in value than the middle number, then we
pick the elements on the right side of the middle element(as the list/array is sorted, hence on
the right, we will have all the numbers greater than the middle number), and start again from
the step 1.
5. If the element/number to be searched is lesser in value than the middle number, then we pick
the elements on the left side of the middle element, and start again from the step 1.
Binary Search is useful when there are large number of elements in an array and they are
sorted. So a necessary condition for the Binary search to work is that the list/array should be
sorted.
Binary Search
Binary search is In this example we are looking for the value 6 in a array of integer.
eatures of Binary Search Algorithm
1. It is great to search through large sorted arrays.
2. It has a time complexity of O(log n) which is a very good time complexity. We will discuss
this in detail in the Binary Search tutorial.
3. It has a simple implementation.
Binary search
This is the iterative version of the binary search:
FUNCTION binary_search(arr : ARRAY_OF INTEGER) : INTEGER
VAR
left, right, mid : INTEGER;
BEGIN
left := 0;
right := arr.length-1;
WHILE (left < right) DO
mid := left + (right - left)/2;
// check if x is present in the mid
IF (arr[mid] = x) THEN
RETURN mid;
END_IF
// if x grater, ignore the left half
IF (arr[mid] < x) THEN
left := mid+1;
ELSE
// if x is smaller, ignore the right half
right := mid-1;
END_IF
END_WHILE
// if we reached here then the element is not present
RETURN -1 ;
END
Which of the following is not an application of binary search?To search in unordered listWhat is the
time complexity of binary search with iteration?O(logn)
Given an array arr = {5,6,77,88,99} and key = 88; How many iterations are done until the element
is found?