Sie sind auf Seite 1von 10

Algorithm

An algorithm is a step by step method of solving a problem. It is commonly

used for data processing, calculation and other related computer and

mathematical operations.

An algorithm is also used to manipulate data in various ways, such as


inserting a new data item, searching for a particular item or sorting an item.

Bubble Sort
Bubble Sort is the simplest sorting algorithm that works by repeatedly
swapping the adjacent elements if they are in wrong order.

How Bubble Sort Works?


We take an unsorted array for our example. Bubble sort takes Ο(n2) time so
we're keeping it short and precise.

Bubble sort starts with very first two elements, comparing them to check
which one is greater.

In this case, value 33 is greater than 14, so it is already in sorted locations.


Next, we compare 33 with 27.
We find that 27 is smaller than 33 and these two values must be swapped.

The new array should look like this −

Next we compare 33 and 35. We find that both are in already sorted
positions.

Then we move to the next two values, 35 and 10.

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.

Now we should look into some practical aspects of bubble sort.

Merge Sort:

Merge sort is a sorting technique based on divide and conquer technique.


With worst-case time complexity being Ο(n log n), it is one of the most
respected algorithms.

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.

How Merge Sort Works?


To understand merge sort, we take an unsorted array as the following −

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

order. In insertion sort, algorithm, every iteration moves an element from

unsorted portion to sorted portion until all the elements are sorted in the

list.
How insertion sort Algorithm works?

Explanation

Suppose, you want to sort elements in ascending as in above figure. Then,

1. Step 1: The second element of an array is compared with


the elements that appears before it (only first element in
this case). If the second element is smaller than first
element, second element is inserted in the position of first
element. After first step, first two elements of an array will
be sorted.
2. Step 2: The third element of an array is compared with the
elements that appears before it (first and second element).
If third element is smaller than first element, it is inserted in
the position of first element. If third element is larger than
first element but, smaller than second element, it is inserted
in the position of second element. If third element is larger
than both the elements, it is kept in the position as it is.
After second step, first three elements of an array will be
sorted.

3. Step 3: Similary, the fourth element of an array is compared


with the elements that appears before it (first, second and
third element) and the same procedure is applied and that
element is inserted in the proper position. After third step,
first four elements of an array will be sorted.

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

How Dijkstra's Algorithm works?


Dijkstra's Algorithm works on the basis that any subpath B -> D of the
shortest path A -> D between vertices A and D is also the shortest
path between vertices B and D.
Djikstra used this property in the opposite direction i.e we
overestimate the distance of each vertex from the starting vertex.
Then we visit each node and its neighbours to find the shortest
subpath to those neighbours.

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.

Dijkstra's Shortest Path Algorithm:


Dijkstra's Shortest Path Algorithm is a well known solution to the Shortest

Paths problem, which consists in finding the shortest path (in terms of arc

weights) from an initial vertex r to each other vertex in a directed weighted

graph with nonnegative weights. This implementation shows the step-by-

step progress of the algorithm.

Das könnte Ihnen auch gefallen