Algo Notes
Algo Notes
used for data processing, calculation and other related computer and
mathematical operations.
Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in wrong order.
Bubble sort starts with very first two elements, comparing them to check
which one is greater.
Next we compare 33 and 35. We find that both are in already sorted
positions.
We know then that 10 is smaller 35. Hence they are not sorted.
We swap these values. We find that we have reached the end of the array.
After one iteration, the array should look like this −
To be precise, we are now showing how an array should look like after each
iteration. After the second iteration, it should look like this −
Notice that after each iteration, at least one value moves at the end.
And when there's no swap required, bubble sorts learns that an array is
completely sorted.
Merge Sort:
Divide/Break
This step involves breaking the problem into smaller sub-problems. Sub-
problems should represent a part of the original problem. This step
generally takes a recursive approach to divide the problem until no sub-
problem is further divisible. At this stage, sub-problems become atomic in
nature but still represent some part of the actual problem.
Conquer/Solve
This step receives a lot of smaller sub-problems to be solved. Generally, at
this level, the problems are considered 'solved' on their own.
Merge/Combine
When the smaller sub-problems are solved, this stage recursively combines
them until they formulate a solution of the original problem. This
algorithmic approach works recursively and conquer & merge steps works
so close that they appear as one.
Merge sort first divides the array into equal halves and then combines them
in a sorted manner.
We know that merge sort first divides the whole array iteratively into equal
halves unless the atomic values are achieved. We see here that an array of
8 items is divided into two arrays of size 4.
This does not change the sequence of appearance of items in the original.
Now we divide these two arrays into halves.
We further divide these arrays and we achieve atomic value which can no
more be divided.
Now, we combine them in exactly the same manner as they were broken
down. Please note the color codes given to these lists.
We first compare the element for each list and then combine them into
another list in a sorted manner. We see that 14 and 33 are in sorted
positions. We compare 27 and 10 and in the target list of 2 values we put
10 first, followed by 27. We change the order of 19 and 35 whereas 42 and
44 are placed sequentially.
In the next iteration of the combining phase, we compare lists of two data
values, and merge them into a list of found data values placing all in a
sorted order.
After the final merging, the list should look like this –
Insertion sort:
Insertion sort, is an algorithm that arranges a list of elements in a particular
unsorted portion to sorted portion until all the elements are sorted in the
list.
How insertion sort Algorithm works?
Explanation
Dijkstra's Algorithm:
Dijkstra's algorithm is an algorithm for finding the shortest
paths between nodes in a graph. i.e., the shortest path between two graph
vertices in a graph. It functions by constructing a shortest-path tree from the initial
vertex to every other vertex in the graph
The algorithm uses a greedy approach in the sense that we find the
next best solution hoping that the end result is the best solution for the
whole problem.
Paths problem, which consists in finding the shortest path (in terms of arc