Practical Theory
Practical Theory
Finished?
Selection Sort
Repeatedly selects smallest element from the unsorted part of the array
and swaps it with the first element of the unsorted array
Quick Sort
Based on Divide And Conquer strategy
Picks element as pivot and partitions the given array by placing pivot in
correct position in sorted array which puts smaller elements to left side and
larger elements to right side
Partitions are done recursively on both side of pivot after it has been placed
in correct position in sorted array
Time complexity
Unstable sort
Heap Sort
Comparison based sorting algorithm based on Binary Heap
Min Heap - Binary tree where parent element must be smaller than all
child nodes
Practical Theory 1
We place the smallest element at beginning by removing it from root of the
min heap
We then replace root with last node of heap and heapify root of heap
Unstable Sort
Merge Sort
Based on Divide And Conquer strategy
Recursively divides input array into smaller sub arrays and sorting those
arrays by merging them back together
Divide - Divide list or array recursively into two halves until it can no
more be divided
Merge - Sorted arrays are merged back together to form a sorted array
Stable sort
Insertion Sort
Works by iteratively inserting each element of an unsorted list into correct
position is sorted portion of the array
Topological Sort
For DAG
linear ordering of vertices such that for every directed edge u-v , vertex u
comes before v in ordering
Steps
Practical Theory 2
Create Graph with n nodes and m directed edges
In DFS function mark node as visited and recursively call DFS for all
unvisited neighbors of the vertex
Once all neighbors are visited push the vertex in the stack
Once all vertex are visited pop all elements from the stack and append it
to o/p list which is array
Time Complexity
Regular is N^3
Strassen’s is N^2
Steps
Knapsack(Greedy)
For given N items with in form of (profit, weight) putting these items into
capacity W to get maximum profit
Practical Theory 3
ratio of p/w for each item is found out and the item are sorted on basis of
the ratio and can be added as a whole element or a fraction of it
Knapsack(Dynamic Programming)
Time Complexity (N*W)
LCS
O(m*n)
Steps
Dynamic Programming
Practical Theory 4
Construction Time Complexity N^2
Sum of Subsets(Backtracking)
Time complexity 2^N
BFS
Involves visiting of all connect nodes of a graph level by level
It visits all vertices at current level before moving to the next depth level
DFS
Involves visiting node as far as possible along each branch before
backtracking
Solving in backtracking
Practical Theory 5
If the queen does not attack , we make it part of solution
if it clashes then we return false i.e. it is not part of solution set and
backtrack
Time complexity N!
Hamiltonian Cycle
It is cycle in Graph which visits every vertex ones and returns to starting
vertex
Practical Theory 6