https://leetcode.com/problems/sort-an-array/
Divide and Conquer Algorithm.
In-Place Sorting Algorithm (No extra auxilary space needed)
It picks an element as pivot and partitions the given array around the picked pivot.
The optimised Partition() always tries to divide the partition in two equal halfes.
Time Complexity
Best Case : O(NLogN) The best case occurs when the partition process always picks the middle element as pivot.
Worst Case : O(N^2) The worst case occurs when the partition process always picks smallest/largest as pivot.Use the
optimise()
method to avoid it.
Avg Case : O(NLogN) The worst case occurs when the partition process always picks smallest/largest as pivot.Use the
optimise()
method to avoid it.
Divide and Conquer Algorithm.
Not a In-Place Sorting Algorithm.
It divides the input array into two halves, calls itself for the two halves, and then merges the two sorted halves.
Time Complexity
Time complexity of Merge Sort is O(NLogN) in all 3 cases (worst, average and best) as merge sort always divides the array into two halves and takes linear time to merge two halves.
Space Complexity : O(N)
Applications:
Based on Binary Heap data structure.
A Binary Heap is a Complete Binary Tree where items are stored in a special order such that the value in a parent node is greater(or smaller) than the values in its two children nodes. The former is called max heap and the latter is called min-heap. The heap can be represented by a binary tree or array. A complete binary tree is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
Since a Binary Heap is a Complete Binary Tree, it can be easily represented as an array and the array-based representation is space-efficient. If the parent node is stored at index I, the left child can be calculated by 2 * I + 1 and the right child by 2 * I + 2 (assuming the indexing starts at 0).
In-Place Sorting Algorithm,no auxilary memory is needed.
Time Complexity
Time complexity of Heap Sort is O(NLogN) and Time Complexity of buildHeap is O(N)
Space Complexity : O(1)
Applications of Heap Data Structure:
Only for Directed Acyclic Graph (DAG)
DFS Traversal
In topological sorting, we use a temporary stack. We don’t print the vertex immediately, we first recursively call topological sorting for all its adjacent vertices, then push it to a stack. Finally, print contents of the stack. Note that a vertex is pushed to stack only when all of its adjacent vertices (and their adjacent vertices and so on) are already in the stack.
There can be multiple correct order
Following are the dependencies of the tools/packages/services:
What will be order in which we can install these dependencies ?
One Possible Solution can be
[ 'nodejs', 'npm', 'pm2', 'git', 'rabbitMq', 'apiService', 'efs', 'csvService' ]
https://leetcode.com/problems/course-schedule-ii/
Time Complexity : O(V+E). The above algorithm is simply DFS with an extra stack.
Space Complexity : O(V)
Applications:
Topological Sorting is mainly used for scheduling jobs from the given dependencies among jobs.
Only for Directed Acyclic Graph (DAG)
BFS Traversal
We calculate the indegree of all the vertexes and keep pushing vertexes in the queue when its in-degree = 0.
There can be multiple correct order possible.
Algorithm
https://leetcode.com/problems/course-schedule-ii/
Time Complexity : O(V+E).
Space Complexity : O(V) The extra space is needed for the Queue.
Try LongestIncreasingPathInMatrix problem with BFS.