Skip to content

Commit 6f867ce

Browse files
committed
Init
1 parent 0cfb76c commit 6f867ce

13 files changed

+633
-0
lines changed

algorithm-analysis/PrimeNumber.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package algorithm_analysis;
2+
3+
import java.util.Scanner;
4+
5+
// For a given integer n, determine whether the number is prime or not.
6+
7+
public class PrimeNumber {
8+
9+
// Time: O(sqrt(n))
10+
public static boolean isPrime(long n) {
11+
if (n < 2) {
12+
return false;
13+
}
14+
int limit = (int)Math.sqrt(n);
15+
for (long i = 2; i <= limit; i++) { // It's enough to check until sqrt(n)
16+
if (n % i == 0) {
17+
return false;
18+
}
19+
}
20+
return true;
21+
}
22+
23+
public static void main(String[] args) {
24+
Scanner sc = new Scanner(System.in);
25+
long n = sc.nextLong();
26+
27+
long startTime = System.nanoTime();
28+
29+
System.out.println(isPrime(n));
30+
31+
long endTime = System.nanoTime();
32+
long totalTime = endTime - startTime;
33+
System.out.printf("Time: %.4fs", (totalTime / 1000000000.0)); // print execution time
34+
sc.close();
35+
}
36+
37+
}
38+
39+
// For each number, we can divide its divisors into two groups
40+
// 1) integers smaller than the square root
41+
// 2) integers greater than the square root
42+
43+
// 1 2 3 4 6 | 9 12 18 36
44+
// 1 * 36 = 36
45+
// 2 * 18 = 36
46+
// 3 * 12 = 36
47+
// 4 * 9
48+
// 6 * 6 = 36
49+
50+
51+
// For testing purpose:
52+
// Prime numbers: 7, 101, 103703 1000000007 32416190039
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package algorithm_analysis;
2+
3+
public class SpaceComplexityExamples {
4+
5+
// Space: O(1)
6+
public static int example1(int n) {
7+
int sum = 0;
8+
for (int i = 1; i <= n; i++) {
9+
sum += i;
10+
}
11+
return sum;
12+
}
13+
14+
// Space: O(n)
15+
public static int[] example2(int[] array) {
16+
int n = array.length;
17+
int[] result = new int[2 * n];
18+
for (int i = 0; i < n; i++) {
19+
result[i] = array[i];
20+
result[n + i] = array[i];
21+
}
22+
return result;
23+
}
24+
25+
// Space: O(n * m)
26+
public static int example3(int[][] matrix) {
27+
int n = matrix.length;
28+
int m = matrix[0].length;
29+
int sum = 0;
30+
for (int i = 0; i < n; i++) {
31+
for (int j = 0; j < m; j++) {
32+
sum += matrix[i][j];
33+
}
34+
}
35+
int[] array = new int[n];
36+
for (int i = 0; i < n; i++) {
37+
array[i] = matrix[i][0];
38+
}
39+
return sum;
40+
}
41+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package algorithm_analysis;
2+
3+
public class TimeComplexityExamples {
4+
5+
// Time: O(n^2)
6+
public static boolean hasDuplicates(int[] array) {
7+
int n = array.length;
8+
for (int i = 0; i < n; i++) {
9+
for (int j = 0; j < n; j++) {
10+
if (i != j && array[i] == array[j]) {
11+
return true;
12+
}
13+
}
14+
}
15+
return false;
16+
}
17+
18+
// Time: O(n^2)
19+
public static int f(int n) {
20+
int sum = 0;
21+
for (int i = 1; i <= n; i++) {
22+
for (int j = 1; j <= i; j++) {
23+
sum += i * j;
24+
}
25+
}
26+
for (int i = 0; i < n; i++) {
27+
sum = sum - i;
28+
}
29+
for (int i = 0; i < 100; i++) {
30+
sum += i;
31+
}
32+
return sum;
33+
}
34+
35+
// Time: O(n + m)
36+
public static int[] union(int[] A, int[] B) {
37+
int n = A.length;
38+
int m = B.length;
39+
int[] C = new int[n + m];
40+
int index = 0;
41+
for (int i = 0; i < n; i++) {
42+
C[index] = A[i];
43+
index++;
44+
}
45+
for (int i = 0; i < m; i++) {
46+
C[index] = B[i];
47+
index++;
48+
}
49+
return C;
50+
}
51+
52+
// Time: O(n * m)
53+
public static int matrixSum(int[][] matrix) {
54+
int n = matrix.length;
55+
int m = matrix[0].length;
56+
int sum = 0;
57+
for (int i = 0; i < n; i++) {
58+
for (int j = 0; j < m; j++) {
59+
sum += matrix[i][j];
60+
}
61+
}
62+
return sum;
63+
}
64+
65+
// Time: O(logn)
66+
public static int g(int n) {
67+
int result = 0;
68+
int i = n;
69+
while (i > 1) {
70+
result += i;
71+
i /= 2;
72+
}
73+
return result;
74+
}
75+
76+
}

binary-search/BinarySearch.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package binarySearch;
2+
3+
public class BinarySearch {
4+
5+
public static int search(int[] array, int target) { // O(logn)
6+
int lo = 0;
7+
int hi = array.length - 1;
8+
while (lo <= hi) {
9+
int mid = (lo + hi) / 2;
10+
if (target > array[mid]) {
11+
lo = mid + 1;
12+
} else if (target < array[mid]) {
13+
hi = mid - 1;
14+
} else {
15+
return mid; // match
16+
}
17+
}
18+
return -1; // no match
19+
}
20+
21+
public static void main(String[] args) {
22+
int[] array = {1, 3, 3, 3, 4, 4, 6, 6, 6, 6, 7, 7, 7, 8, 8, 11, 15, 22};
23+
int index = search(array, 2);
24+
System.out.println(index);
25+
}
26+
27+
}

binary-search/FirstOccurrence.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package binarySearch;
2+
3+
public class FirstOccurrence {
4+
5+
public static int firstOccurrence(int[] array, int target) {
6+
int lo = 0;
7+
int hi = array.length - 1;
8+
while (lo < hi) {
9+
int mid = (lo + hi) / 2;
10+
if (target > array[mid]) {
11+
lo = mid + 1; // search the right half [mid + 1, hi]
12+
} else {
13+
hi = mid; // search left half + middle element [lo, mid]
14+
}
15+
}
16+
if (array[lo] == target) {
17+
return lo;
18+
} else {
19+
return -1;
20+
}
21+
}
22+
23+
public static void main(String[] args) {
24+
int[] array = {1, 3, 3, 3, 4, 4, 6, 6, 6, 6, 7, 7, 7, 8, 8, 11, 15, 22};
25+
int index = firstOccurrence(array, 13);
26+
System.out.println(index);
27+
}
28+
29+
}

binary-search/LastOccurrence.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package binarySearch;
2+
3+
public class LastOccurrence {
4+
5+
public static int lastOccurrence(int[] array, int target) {
6+
int lo = 0;
7+
int hi = array.length - 1;
8+
while (lo < hi) {
9+
int mid = (lo + hi + 1) / 2; // [1, 2]
10+
if (target < array[mid]) {
11+
hi = mid - 1; // [lo, mid-1]
12+
} else {
13+
lo = mid; // [mid, hi]
14+
}
15+
}
16+
if (array[lo] == target) {
17+
return lo;
18+
} else {
19+
return -1;
20+
}
21+
}
22+
23+
public static void main(String[] args) {
24+
int[] array = {1, 2};
25+
int index = lastOccurrence(array, 1);
26+
System.out.println(index);
27+
}
28+
29+
// lo = 0, hi = 1, mid = 1
30+
// lo = 0, hi = 0
31+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package binarySearch;
2+
3+
public class NumberOfOccurrences {
4+
5+
public static int firstOccurrence(int[] array, int target) {
6+
int lo = 0;
7+
int hi = array.length - 1;
8+
while (lo < hi) {
9+
int mid = (lo + hi) / 2;
10+
if (target > array[mid]) {
11+
lo = mid + 1;
12+
} else {
13+
hi = mid;
14+
}
15+
}
16+
if (array[lo] == target) {
17+
return lo;
18+
} else {
19+
return -1;
20+
}
21+
}
22+
23+
public static int lastOccurrence(int[] array, int target) {
24+
int lo = 0;
25+
int hi = array.length - 1;
26+
while (lo < hi) {
27+
int mid = (lo + hi + 1) / 2;
28+
if (target < array[mid]) {
29+
hi = mid - 1;
30+
} else {
31+
lo = mid;
32+
}
33+
}
34+
if (array[lo] == target) {
35+
return lo;
36+
} else {
37+
return -1;
38+
}
39+
}
40+
41+
public static int numberOfOccurrences(int[] array, int target) { // O(logn)
42+
int first = firstOccurrence(array, target); // O(logn)
43+
int last = lastOccurrence(array, target); // O(logn)
44+
if (first == -1) {
45+
return 0;
46+
} else {
47+
return last - first + 1; // [lo, hi] => hi - lo + 1
48+
}
49+
}
50+
51+
public static void main(String[] args) {
52+
int[] array = {1, 3, 3, 3, 4, 4, 6, 6, 6, 6, 7, 7, 7, 8, 8, 11, 15, 22};
53+
int count = numberOfOccurrences(array, 4);
54+
System.out.println(count);
55+
}
56+
57+
// linear solution O(n)
58+
59+
}

sorting/BubbleSort.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package sorting;
2+
3+
public class BubbleSort {
4+
5+
public static void sort(int[] array) {
6+
int n = array.length;
7+
while (true) {
8+
boolean swapped = false;
9+
for (int i = 0; i < n - 1; i++) {
10+
if (array[i + 1] < array[i]) {
11+
swap(array, i, i + 1);
12+
swapped = true;
13+
}
14+
}
15+
if (!swapped) break;
16+
}
17+
}
18+
19+
private static void swap(int[] array, int i, int j) {
20+
int temp = array[i];
21+
array[i] = array[j];
22+
array[j] = temp;
23+
}
24+
25+
private static void print(int[] A) {
26+
System.out.print("[");
27+
for (int i = 0; i < A.length; i++) {
28+
System.out.print(A[i]);
29+
if (i < A.length - 1) {
30+
System.out.print(", ");
31+
}
32+
}
33+
System.out.println("]");
34+
}
35+
36+
public static void main(String[] args) {
37+
int[] A = {105, 8, 4, 22, 16, 3, 9, 11, 1, 19, 8, 6, 7};
38+
sort(A);
39+
print(A);
40+
}
41+
42+
}
43+
44+
45+

0 commit comments

Comments
 (0)