Posts
Insertion Sort
def insertion_sort(arr): n = len(arr) for i in range(1, n): key = arr[i] j = i - 1 # Move elements of arr[0..i-1], that are greater than key, to one position ahead of their current position while j >= 0 and key < arr[j]: arr[j + 1] = arr[j] j -= 1 arr[j + 1] = key return arr # Example usage: arr = [64, 25, 12, 22, 11] sorted_arr = insertion_sort(arr) print("Sorted array:", sorted_arr) # Sorted array: [11, 12, 22, 25, 64] Complexity Type Complexity Time Worst: O(n^2) Space Worst: O(1) Stability Stable
read morePosts
Selection Sort
def selection_sort(arr): n = len(arr) for i in range(n): # Find the minimum element in the unsorted portion min_idx = i for j in range(i + 1, n): if arr[j] < arr[min_idx]: min_idx = j # Swap the found minimum element with the first element arr[i], arr[min_idx] = arr[min_idx], arr[i] return arr # Example usage: arr = [64, 25, 12, 22, 11] sorted_arr = selection_sort(arr) print("Sorted array:", sorted_arr) # Sorted array: [11, 12, 22, 25, 64] Complexity Type Complexity Time Worst: O(n^2) Space Worst: O(1) Stability Not stable
read morePosts
Bubble Sort
def bubble_sort(arr): n = len(arr) # walk through array for len() times for i in range(n): # each time the biggest one will swap to the end of the list # only 'n-i' times afterward needed for j in range(0, n - i - 1): # compare and swap if arr[j] > arr[j + 1]: arr[j], arr[j + 1] = arr[j + 1], arr[j] if __name__ == "__main__": import random arr = [random.
read morePosts
Reverse Singly Linked List
Given the head of a singly linked list, reverse the list, and return the reversed list.
class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next def reverse_linked_list_recursive(head): # Base case: if the list is empty or has only one node if head is None or head.next is None: return head # Reverse the rest of the list recursively reversed_head = reverse_linked_list_recursive(head.next) # Reverse the pointers head.next.next = head head.
read morePosts
Recursion
The trick is that each time a recursive function calls itself, it reduces the given problem into subproblems. The recursion call continues until it reaches a point where the subproblem can be solved without further recursion.
Reverse a string:
void printReverse(const char *str) { if (!*str) return; printReverse(str + 1); putchar(*str); } Fibonacci:
def fibonacci_recursive(n): if n <= 1: return n else: return fibonacci_recursive(n - 1) + fibonacci_recursive(n - 2) def fibonacci_tail_recursive(n, a=0, b=1): if n == 0: return a elif n == 1: return b else: return fibonacci_tail_recursive(n - 1, b, a + b) Divide and Conquer.
read morePosts
Depth-First Search
Given an m x n grid of characters board and a string word, return true if word exists in the grid.
The word can be constructed from letters of sequentially adjacent cells, where adjacent cells are horizontally or vertically neighboring. The same letter cell may not be used more than once.
Example 1:
Input: board = [[“A”,“B”,“C”,“E”],[“S”,“F”,“C”,“S”],[“A”,“D”,“E”,“E”]], word = “ABCCED” Output: true
Solution:
depth-first search (DFS) to explore all possible paths in the grid to find the word:
read morePosts
Dependency Inversion Principle
DIP The Dependency Inversion Principle (DIP) is a design principle in object-oriented programming that states that high-level modules should not depend on low-level modules. Both should depend on abstractions. This principle aims to decouple modules, making the system more flexible, extensible, and easier to maintain.
It consists of two key points:
High-level modules should not depend on low-level modules. Both should depend on abstractions. Abstractions should not depend on details. Details should depend on abstractions.
read morePosts
Emergent Capability
In terms of biological intelligence or artificial intelligence, individual bees or individual ants have no intelligence in terms of their material basis and spiritual structure. However, when a group of bees or a group of ants gather together, they can produce intelligent behaviors such as constructing sophisticated beehives and cooperating to transport large objects.
In contrast, individual humans already possess intelligence, and when humans gather together, they should be able to generate behaviors of an intelligent order of magnitude.
read morePosts
Object Oriented
Jeff Goodell: Would you explain, in simple terms, exactly what object-oriented software is?
Steve Jobs: Objects are like people. They’re living, breathing things that have knowledge inside them about how to do things and have memory inside them so they can remember things. And rather than interacting with them at a very low level, you interact with them at a very high level of abstraction, like we’re doing right here.
read morePosts
Moderate gaming benefits the brain!
After long working hours, proper gaming will help the brain recover quickly.
Life is a marathon, don’t worry about temporary gains and losses.
Life is a long journey, keep calm and relex, enjoy the scenary during the process.
Enjoy life!
read morePosts
MacOS - Productivity
MacOS allows users to focus on the specific tasks rather than the devices themselves, with a superb user experience akin to a true piece of art.
Really like it ;)
read more