Implementing Bubble Sort Algorithm
Implementing Bubble Sort Algorithm
Implementing Bubble Sort Algorithm
2.best case:o(n)
Space complexity:o(1)
1. Starting with the first element(index = 0), compare the current element with the next
element of the array.
2. If the current element is greater than the next element of the array, swap them.
3. If the current element is less than the next element, move to the next element. Repeat
Step 1.
Example:
Selection sort:
Selection sort is an algorithm that selects the smallest element from an unsorted list in each
iteration and places that element at the beginning of the unsorted list.
It is called selection sort because it repeatedly selects the next-smallest element and swaps it into
the right place.
Time complexities:
Space complexity:o(1)
Following are the steps involved in selection sort(for sorting a given array in ascending
order):
1. Starting from the first element, we search the smallest element in the array, and
replace it with the element in the first position.
2. We then move on to the second position, and look for smallest element present in the
subarray, starting from index 1, till the last index.
3. We replace the element at the second position in the original array, or we can say at
the first position in the subarray, with the second smallest element.
4. This is repeated, until the array is completely sorted.
Insertion Sort is basically insertion of an element from a random set of numbers, to its
correct position where it should actually be, by shifting the other elements if required.
How Insertion Sort Works?
Following are the steps involved in insertion sort:
1. We start by making the second element of the given array, i.e. element at index 1, the
key. The key element here is the new card that we need to add to our existing sorted
set of cards(remember the example with cards above).
2. We compare the key element with the element(s) before it, in this case, element at
index 0:
o If the key element is less than the first element, we insert the key element
before the first element.
o If the key element is greater than the first element, then we insert it after the
first element.
3. Then, we make the third element of the array as key and will compare it with
elements to it's left and insert it at the right position.
4. And we go on repeating this, until the array is sorted.
Merge sort:
Merge sort is a sorting technique basd on divide and conquer strategy
Divide:
In this step we divide A[p..r] into 2 sub parts A[p,q] and A[q+1,r]
Conquer:
In this step we try to ssort out the sub arrays A[p,q] and A[q+1,r]
Combine:
In this step we get two sorted arrays A[p,q] and A[q+1,r] and combine them into A[p,r]
After that, the merge function comes into play and combines the sorted arrays into larger arrays
until the whole array is merged.
Heap sort
Heap sort works by visualizing the elements of the array as a special kind of complete binary tree
called heap.
Min-Heap − Where the value of the root node is less than or equal to either of its children.
Max-Heap − Where the value of the root node is greater than or equal to either of its children.
A Graph is a non-linear data structure consisting of nodes and edges. The nodes are
sometimes also referred to as vertices and the edges are lines or arcs that connect any two
nodes in the graph. More formally a Graph can be defined as,
A Graph consists of a finite set of vertices or set of edges which connect a pair of nodes
Graphs are used to solve many real-life problems. Graphs are used to represent networks. The
networks may include paths in a city or telephone network or circuit network. Graphs are also used
in social networks like linkedIn, Facebook. For example, in Facebook, each person is represented
with a vertex(or node). Each node is a structure and contains information like person id, name,
gender, locale etc.
a graph is said to be connected if there exist atleast one path between each and every pair of
vertices in graph G, otherwise it is disconnected.
DFS stands for Depth First Search is a edge based technique. It uses the Stack data
structure, performs two stages, first visited vertices are pushed into stack and second if there
is no vertices then visited vertices are popped.
Ex-
BFS vs DFS
S.NO BFS DFS
1. BFS stands for Breadth First Search. DFS stands for Depth First Search.
BFS(Breadth First Search) uses Queue data DFS(Depth First Search) uses Stack data
2.
structure for finding the shortest path. structure.
BFS is more suitable for searching verteces DFS is more suitable when there are solutions
3.
which are closer to the given source. away from source.
BST :
It is an ordered binary tree in which the nodes are arranged in particular order
In bst,the value of all nodes in the left subtree is less than the value of root
The valure of all nodes in the right subtree is greator than the value of root
It will be recursively applied to right and left sub trees of the root
TYPES:
Full Binary Tree A Binary Tree is full if every node has 0 or 2 children. Following are examples of a
full binary tree. We can also say a full binary tree is a binary tree in which all nodes except leaves
have two children.
Complete Binary Tree: A Binary Tree is complete Binary Tree if all levels are completely filled except
possibly the last level and the last level has all keys as left as possible
Perfect Binary Tree A Binary tree is Perfect Binary Tree in which all internal nodes have two children
and all leaves are at the same level.
Graph vs Tree
No. Graph Tree
It is a collection of vertices/nodes
2 It is a collection of nodes and edges.
and edges.
Applications of Trees
Trees and their variants are an extremely useful data structure with lots of practical
applications.
Binary Search Trees(BSTs) are used to quickly check whether an element is present
in a set or not.
Heap is a kind of tree that is used for heap sort.
A modified version of tree called Tries is used in modern routers to store routing
information.
Most popular databases use B-Trees and T-Trees, which are variants of the tree
structure we learned above to store their data
Compilers use a syntax tree to validate the syntax of every program you write.