This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Merge Sort | Time Complexity O(NlogN) | |
| def mergeSort(alist): | |
| print("Splitting ", alist) | |
| if len(alist) > 1: | |
| mid = len(alist) // 2 | |
| lefthalf = alist[:mid] | |
| righthalf = alist[mid:] | |
| mergeSort(lefthalf) | |
| print("LeftHalf ", lefthalf) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Fibonacci series using recursion | |
| def fibonacci(n): | |
| if n == 0: | |
| return n | |
| elif n == 1: | |
| return n | |
| else: | |
| return fibonacci(n - 1) + fibonacci(n - 2) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| n = int(input('Enter the number of n: ')) | |
| def fact(n): | |
| # Base Case | |
| if n == 1: | |
| return 1 | |
| # Recursive case | |
| else: | |
| return n * fact(n - 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.*; | |
| class MySingletonClass | |
| { | |
| static MySingletonClass instance = null; | |
| public int x = 10; | |
| // private constructor can't be accessed outside the class | |
| private MySingletonClass() { } | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Insertion Sort Time complexity: Worst Case & Average Case: O(n**n) | |
| # Insertion Sort Time complexity: Best Case: O(n) | |
| def insertionSort(arr): | |
| # Traverse through 1 to len(arr) | |
| for i in range(1, len(arr)): | |
| key = arr[i] |
NewerOlder