Back to basics
Learning Binary Search Tree and algorithms for Binary Tree
https://leetcode.com/submissions/detail/193276741/
Big O notation
O(1) - exactly 1 computation to do the operation
O(n) - computation time is equal to the size of array
O(n square) - computation to nested for-each
O(log (n)) - divide and conquer algorithms
O(n log(n)) -
Insertion Sort - iterate each element , check if it is lower than any of the element at the left and insert in between and move other elements greater than it.
O(n square) , best O(n) -
Bubble Sort - keep comparing side by side each element and swap
O(n square) , best O(n) - if no swap at the first iteration
Selection Sort - assume the first is smallest and find the smallest at first position, increment each position and check if anything is smaller.
O(n square) , best O(n2)
Heap Sort - elements are stored in binary tree.
root node will be greater than child node.swap root and child , finally swap the last and first node , this way largest element goes to last.
now repeat same for remaining n-1 elements and finally swap first and n-1 element and so on
Learning Binary Search Tree and algorithms for Binary Tree
https://leetcode.com/submissions/detail/193276741/
Big O notation
O(1) - exactly 1 computation to do the operation
O(n) - computation time is equal to the size of array
O(n square) - computation to nested for-each
O(log (n)) - divide and conquer algorithms
O(n log(n)) -
Insertion Sort - iterate each element , check if it is lower than any of the element at the left and insert in between and move other elements greater than it.
O(n square) , best O(n) -
Bubble Sort - keep comparing side by side each element and swap
O(n square) , best O(n) - if no swap at the first iteration
Selection Sort - assume the first is smallest and find the smallest at first position, increment each position and check if anything is smaller.
O(n square) , best O(n2)
Heap Sort - elements are stored in binary tree.
root node will be greater than child node.swap root and child , finally swap the last and first node , this way largest element goes to last.
now repeat same for remaining n-1 elements and finally swap first and n-1 element and so on
Comments
Post a Comment