mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-09 22:00:52 +08:00
translation: Add Python and Java code for EN version (#1345)
* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
/**
|
||||
* File: iteration.java
|
||||
* Created Time: 2023-08-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
public class iteration {
|
||||
/* for loop */
|
||||
static int forLoop(int n) {
|
||||
int res = 0;
|
||||
// Loop sum 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; i++) {
|
||||
res += i;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while loop */
|
||||
static int whileLoop(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // Initialize condition variable
|
||||
// Loop sum 1, 2, ..., n-1, n
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
i++; // Update condition variable
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* while loop (two updates) */
|
||||
static int whileLoopII(int n) {
|
||||
int res = 0;
|
||||
int i = 1; // Initialize condition variable
|
||||
// Loop sum 1, 4, 10, ...
|
||||
while (i <= n) {
|
||||
res += i;
|
||||
// Update condition variable
|
||||
i++;
|
||||
i *= 2;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Double for loop */
|
||||
static String nestedForLoop(int n) {
|
||||
StringBuilder res = new StringBuilder();
|
||||
// Loop i = 1, 2, ..., n-1, n
|
||||
for (int i = 1; i <= n; i++) {
|
||||
// Loop j = 1, 2, ..., n-1, n
|
||||
for (int j = 1; j <= n; j++) {
|
||||
res.append("(" + i + ", " + j + "), ");
|
||||
}
|
||||
}
|
||||
return res.toString();
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = forLoop(n);
|
||||
System.out.println("\nSum result of the for loop res = " + res);
|
||||
|
||||
res = whileLoop(n);
|
||||
System.out.println("\nSum result of the while loop res = " + res);
|
||||
|
||||
res = whileLoopII(n);
|
||||
System.out.println("\nSum result of the while loop (with two updates) res = " + res);
|
||||
|
||||
String resStr = nestedForLoop(n);
|
||||
System.out.println("\nResult of the double for loop traversal = " + resStr);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
/**
|
||||
* File: recursion.java
|
||||
* Created Time: 2023-08-24
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class recursion {
|
||||
/* Recursion */
|
||||
static int recur(int n) {
|
||||
// Termination condition
|
||||
if (n == 1)
|
||||
return 1;
|
||||
// Recursive: recursive call
|
||||
int res = recur(n - 1);
|
||||
// Return: return result
|
||||
return n + res;
|
||||
}
|
||||
|
||||
/* Simulate recursion with iteration */
|
||||
static int forLoopRecur(int n) {
|
||||
// Use an explicit stack to simulate the system call stack
|
||||
Stack<Integer> stack = new Stack<>();
|
||||
int res = 0;
|
||||
// Recursive: recursive call
|
||||
for (int i = n; i > 0; i--) {
|
||||
// Simulate "recursive" by "pushing onto the stack"
|
||||
stack.push(i);
|
||||
}
|
||||
// Return: return result
|
||||
while (!stack.isEmpty()) {
|
||||
// Simulate "return" by "popping from the stack"
|
||||
res += stack.pop();
|
||||
}
|
||||
// res = 1+2+3+...+n
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Tail recursion */
|
||||
static int tailRecur(int n, int res) {
|
||||
// Termination condition
|
||||
if (n == 0)
|
||||
return res;
|
||||
// Tail recursive call
|
||||
return tailRecur(n - 1, res + n);
|
||||
}
|
||||
|
||||
/* Fibonacci sequence: Recursion */
|
||||
static int fib(int n) {
|
||||
// Termination condition f(1) = 0, f(2) = 1
|
||||
if (n == 1 || n == 2)
|
||||
return n - 1;
|
||||
// Recursive call f(n) = f(n-1) + f(n-2)
|
||||
int res = fib(n - 1) + fib(n - 2);
|
||||
// Return result f(n)
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
int res;
|
||||
|
||||
res = recur(n);
|
||||
System.out.println("\nSum result of the recursive function res = " + res);
|
||||
|
||||
res = forLoopRecur(n);
|
||||
System.out.println("\nSum result using iteration to simulate recursion res = " + res);
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
System.out.println("\nSum result of the tail-recursive function res = " + res);
|
||||
|
||||
res = fib(n);
|
||||
System.out.println("\nThe " + n + "th number in the Fibonacci sequence is " + res);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* File: space_complexity.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
import utils.*;
|
||||
import java.util.*;
|
||||
|
||||
public class space_complexity {
|
||||
/* Function */
|
||||
static int function() {
|
||||
// Perform some operations
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Constant complexity */
|
||||
static void constant(int n) {
|
||||
// Constants, variables, objects occupy O(1) space
|
||||
final int a = 0;
|
||||
int b = 0;
|
||||
int[] nums = new int[10000];
|
||||
ListNode node = new ListNode(0);
|
||||
// Variables in a loop occupy O(1) space
|
||||
for (int i = 0; i < n; i++) {
|
||||
int c = 0;
|
||||
}
|
||||
// Functions in a loop occupy O(1) space
|
||||
for (int i = 0; i < n; i++) {
|
||||
function();
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear complexity */
|
||||
static void linear(int n) {
|
||||
// Array of length n occupies O(n) space
|
||||
int[] nums = new int[n];
|
||||
// A list of length n occupies O(n) space
|
||||
List<ListNode> nodes = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
nodes.add(new ListNode(i));
|
||||
}
|
||||
// A hash table of length n occupies O(n) space
|
||||
Map<Integer, String> map = new HashMap<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
map.put(i, String.valueOf(i));
|
||||
}
|
||||
}
|
||||
|
||||
/* Linear complexity (recursive implementation) */
|
||||
static void linearRecur(int n) {
|
||||
System.out.println("Recursion n = " + n);
|
||||
if (n == 1)
|
||||
return;
|
||||
linearRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Quadratic complexity */
|
||||
static void quadratic(int n) {
|
||||
// Matrix occupies O(n^2) space
|
||||
int[][] numMatrix = new int[n][n];
|
||||
// A two-dimensional list occupies O(n^2) space
|
||||
List<List<Integer>> numList = new ArrayList<>();
|
||||
for (int i = 0; i < n; i++) {
|
||||
List<Integer> tmp = new ArrayList<>();
|
||||
for (int j = 0; j < n; j++) {
|
||||
tmp.add(0);
|
||||
}
|
||||
numList.add(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
/* Quadratic complexity (recursive implementation) */
|
||||
static int quadraticRecur(int n) {
|
||||
if (n <= 0)
|
||||
return 0;
|
||||
// Array nums length = n, n-1, ..., 2, 1
|
||||
int[] nums = new int[n];
|
||||
System.out.println("Recursion n = " + n + " in the length of nums = " + nums.length);
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
/* Exponential complexity (building a full binary tree) */
|
||||
static TreeNode buildTree(int n) {
|
||||
if (n == 0)
|
||||
return null;
|
||||
TreeNode root = new TreeNode(0);
|
||||
root.left = buildTree(n - 1);
|
||||
root.right = buildTree(n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
int n = 5;
|
||||
// Constant complexity
|
||||
constant(n);
|
||||
// Linear complexity
|
||||
linear(n);
|
||||
linearRecur(n);
|
||||
// Quadratic complexity
|
||||
quadratic(n);
|
||||
quadraticRecur(n);
|
||||
// Exponential complexity
|
||||
TreeNode root = buildTree(n);
|
||||
PrintUtil.printTree(root);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
/**
|
||||
* File: time_complexity.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
public class time_complexity {
|
||||
/* Constant complexity */
|
||||
static int constant(int n) {
|
||||
int count = 0;
|
||||
int size = 100000;
|
||||
for (int i = 0; i < size; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear complexity */
|
||||
static int linear(int n) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < n; i++)
|
||||
count++;
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Linear complexity (traversing an array) */
|
||||
static int arrayTraversal(int[] nums) {
|
||||
int count = 0;
|
||||
// Loop count is proportional to the length of the array
|
||||
for (int num : nums) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Quadratic complexity */
|
||||
static int quadratic(int n) {
|
||||
int count = 0;
|
||||
// Loop count is squared in relation to the data size n
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < n; j++) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Quadratic complexity (bubble sort) */
|
||||
static int bubbleSort(int[] nums) {
|
||||
int count = 0; // Counter
|
||||
// Outer loop: unsorted range is [0, i]
|
||||
for (int i = nums.length - 1; i > 0; i--) {
|
||||
// Inner loop: swap the largest element in the unsorted range [0, i] to the right end of the range
|
||||
for (int j = 0; j < i; j++) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// Swap nums[j] and nums[j + 1]
|
||||
int tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // Element swap includes 3 individual operations
|
||||
}
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential complexity (loop implementation) */
|
||||
static int exponential(int n) {
|
||||
int count = 0, base = 1;
|
||||
// Cells split into two every round, forming the sequence 1, 2, 4, 8, ..., 2^(n-1)
|
||||
for (int i = 0; i < n; i++) {
|
||||
for (int j = 0; j < base; j++) {
|
||||
count++;
|
||||
}
|
||||
base *= 2;
|
||||
}
|
||||
// count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Exponential complexity (recursive implementation) */
|
||||
static int expRecur(int n) {
|
||||
if (n == 1)
|
||||
return 1;
|
||||
return expRecur(n - 1) + expRecur(n - 1) + 1;
|
||||
}
|
||||
|
||||
/* Logarithmic complexity (loop implementation) */
|
||||
static int logarithmic(int n) {
|
||||
int count = 0;
|
||||
while (n > 1) {
|
||||
n = n / 2;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Logarithmic complexity (recursive implementation) */
|
||||
static int logRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
}
|
||||
|
||||
/* Linear logarithmic complexity */
|
||||
static int linearLogRecur(int n) {
|
||||
if (n <= 1)
|
||||
return 1;
|
||||
int count = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
for (int i = 0; i < n; i++) {
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Factorial complexity (recursive implementation) */
|
||||
static int factorialRecur(int n) {
|
||||
if (n == 0)
|
||||
return 1;
|
||||
int count = 0;
|
||||
// From 1 split into n
|
||||
for (int i = 0; i < n; i++) {
|
||||
count += factorialRecur(n - 1);
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
// Can modify n to experience the trend of operation count changes under various complexities
|
||||
int n = 8;
|
||||
System.out.println("Input data size n = " + n);
|
||||
|
||||
int count = constant(n);
|
||||
System.out.println("Number of constant complexity operations = " + count);
|
||||
|
||||
count = linear(n);
|
||||
System.out.println("Number of linear complexity operations = " + count);
|
||||
count = arrayTraversal(new int[n]);
|
||||
System.out.println("Number of linear complexity operations (traversing the array) = " + count);
|
||||
|
||||
count = quadratic(n);
|
||||
System.out.println("Number of quadratic order operations = " + count);
|
||||
int[] nums = new int[n];
|
||||
for (int i = 0; i < n; i++)
|
||||
nums[i] = n - i; // [n,n-1,...,2,1]
|
||||
count = bubbleSort(nums);
|
||||
System.out.println("Number of quadratic order operations (bubble sort) = " + count);
|
||||
|
||||
count = exponential(n);
|
||||
System.out.println("Number of exponential complexity operations (implemented by loop) = " + count);
|
||||
count = expRecur(n);
|
||||
System.out.println("Number of exponential complexity operations (implemented by recursion) = " + count);
|
||||
|
||||
count = logarithmic(n);
|
||||
System.out.println("Number of logarithmic complexity operations (implemented by loop) = " + count);
|
||||
count = logRecur(n);
|
||||
System.out.println("Number of logarithmic complexity operations (implemented by recursion) = " + count);
|
||||
|
||||
count = linearLogRecur(n);
|
||||
System.out.println("Number of linear logarithmic complexity operations (implemented by recursion) = " + count);
|
||||
|
||||
count = factorialRecur(n);
|
||||
System.out.println("Number of factorial complexity operations (implemented by recursion) = " + count);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
/**
|
||||
* File: worst_best_time_complexity.java
|
||||
* Created Time: 2022-11-25
|
||||
* Author: krahets (krahets@163.com)
|
||||
*/
|
||||
|
||||
package chapter_computational_complexity;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class worst_best_time_complexity {
|
||||
/* Generate an array with elements {1, 2, ..., n} in a randomly shuffled order */
|
||||
static int[] randomNumbers(int n) {
|
||||
Integer[] nums = new Integer[n];
|
||||
// Generate array nums = { 1, 2, 3, ..., n }
|
||||
for (int i = 0; i < n; i++) {
|
||||
nums[i] = i + 1;
|
||||
}
|
||||
// Randomly shuffle array elements
|
||||
Collections.shuffle(Arrays.asList(nums));
|
||||
// Integer[] -> int[]
|
||||
int[] res = new int[n];
|
||||
for (int i = 0; i < n; i++) {
|
||||
res[i] = nums[i];
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Find the index of number 1 in array nums */
|
||||
static int findOne(int[] nums) {
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
// When element 1 is at the start of the array, achieve best time complexity O(1)
|
||||
// When element 1 is at the end of the array, achieve worst time complexity O(n)
|
||||
if (nums[i] == 1)
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
public static void main(String[] args) {
|
||||
for (int i = 0; i < 10; i++) {
|
||||
int n = 100;
|
||||
int[] nums = randomNumbers(n);
|
||||
int index = findOne(nums);
|
||||
System.out.println("\nThe array [ 1, 2, ..., n ] after being shuffled = " + Arrays.toString(nums));
|
||||
System.out.println("The index of number 1 is " + index);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user