Thursday, June 29, 2023

Mergesort algorithm in python

Mergesort algorithm is a divide and conquer algorithm. Its not efficient as Quicksort algorithm but in some cases Mergesort is better than Quicksort. For example if the data to be sorted is in a linked list then Quicksort is costly since in a linkedlist accessing random elements is costly and in Quicksort we have to access data randomly.



Asymptotic Complexity
SomethingTime ComplexitySpace Complexity
Worst Case PerformanceO (n log n)
Best Case PerformanceO (n log n )
Average Case PerformanceO(n log n)
Worst Case space Complexity
O (n)




Monday, February 28, 2022

Friday, April 30, 2021

Dutch Flag problem

Dutch flag is a notable problem wherein you have a array representing three colors . Red(R), Green(G) and Blue(B) which needs to be sorted with Reds coming first followed by Green and then Blue. This is achieved by lumoto partitioning scheme.

Tuesday, April 20, 2021

Printing out all combinations of string

Printing out all combinations of a string

Printing out all the permutations of string

How to generate permutation of a given string where digits are kept as it is, but alphabets are replaced by lower and upper case characters The above solution uses immutable strings so its not efficient time complexity-wise below solution replaces string with list

Monday, April 19, 2021

Palindrome Linked List

Given the head of a singly linked list, return true if it is a palindrome. 

 Example 1: Input: head = [1,2,2,1] 
Output: true 

Example 2: Input: head = [1,2]
 Output: false 

 Constraints: The number of nodes in the list is in the range [1, 105]. 0 <= Node.val <= 9 
 Follow up: Could you do it in O(n) time and O(1) space?