Skip to content

Commit 12f651e

Browse files
committed
Re-practice the basic algos
1 parent 9511120 commit 12f651e

File tree

5 files changed

+195
-3
lines changed

5 files changed

+195
-3
lines changed

src/problems/leetcode/InsertionSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class InsertionSort {
77
// runtime: O(N^2)
88
// space: O(1)
99
public static void sort(int[] nums) {
10-
if (nums == null || nums.length < 1) {
10+
if (nums == null || nums.length < 2) {
1111
return;
1212
}
1313

src/problems/leetcode/MaxHeap.java

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package problems.leetcode;
2+
3+
import java.util.Arrays;
4+
5+
public class MaxHeap<T extends Comparable<T>> {
6+
7+
private T[] heap;
8+
private int size;
9+
private int maxSize;
10+
11+
// Constructor to initialize an
12+
// empty max heap with given maximum
13+
// capacity
14+
public MaxHeap(int maxSize) {
15+
// This keyword refers to current instance itself
16+
this.maxSize = maxSize;
17+
this.size = 0;
18+
this.heap = (T[]) new Comparable[this.maxSize];
19+
}
20+
21+
private int parent(int pos) {
22+
return (pos - 1) / 2;
23+
}
24+
25+
private int leftChild(int pos) {
26+
return (2 * pos) + 1;
27+
}
28+
29+
private int rightChild(int pos) {
30+
return (2 * pos) + 2;
31+
}
32+
33+
private void swap(int l, int r) {
34+
T tmp;
35+
tmp = heap[l];
36+
heap[l] = heap[r];
37+
heap[r] = tmp;
38+
}
39+
40+
private void maxHeapify(int i) {
41+
int l = leftChild(i);
42+
int r = rightChild(i);
43+
int largest = i;
44+
if (l < size && heap[l].compareTo(heap[i]) > 0) {
45+
largest = l;
46+
}
47+
if (r < size && heap[r].compareTo(heap[largest]) > 0) {
48+
largest = r;
49+
}
50+
if (largest != i) {
51+
swap(i, largest);
52+
maxHeapify(largest);
53+
}
54+
}
55+
56+
public void add(T element) {
57+
if (size == maxSize) {
58+
throw new IllegalStateException("heap full!");
59+
}
60+
61+
heap[size] = element;
62+
int current = size;
63+
while (heap[current].compareTo(heap[parent(current)]) > 0) {
64+
swap(current, parent(current));
65+
current = parent(current);
66+
}
67+
size++;
68+
}
69+
70+
public T remove() {
71+
if (size == 0) {
72+
throw new IllegalStateException("empty heap!");
73+
}
74+
T popped = heap[0];
75+
heap[0] = heap[--size];
76+
heap[size] = null;
77+
maxHeapify(0);
78+
return popped;
79+
}
80+
81+
@Override
82+
public String toString() {
83+
return Arrays.toString(heap);
84+
}
85+
86+
public static void main(String[] args) {
87+
MaxHeap<Integer> heap = new MaxHeap<>(10);
88+
89+
heap.add(5);
90+
heap.add(10);
91+
heap.add(20);
92+
heap.add(15);
93+
94+
System.out.println(heap);
95+
96+
System.out.println(heap.remove());
97+
System.out.println(heap);
98+
99+
System.out.println(heap.remove());
100+
System.out.println(heap);
101+
102+
System.out.println(heap.remove());
103+
System.out.println(heap);
104+
105+
System.out.println(heap.remove());
106+
System.out.println(heap);
107+
}
108+
}

src/problems/leetcode/MergeSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ private static void sort(int[] nums, int lo, int hi, int[] aux) {
4242
// runtime: O(NlgN)
4343
// space: O(N)
4444
public static void sort(int[] nums) {
45-
if (nums == null || nums.length < 1) {
45+
if (nums == null || nums.length < 2) {
4646
return;
4747
}
4848

src/problems/leetcode/QuickSort.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package problems.leetcode;
2+
3+
import java.util.Arrays;
4+
import java.util.Collections;
5+
6+
public class QuickSort {
7+
8+
// 5, 1, 4, 8, 0, 9
9+
// pivot = 5
10+
// 1, 4, 0, 5, 8, 9
11+
12+
private static void exch(int[] nums, int i, int j) {
13+
if (i == j) {
14+
return;
15+
}
16+
17+
int n = nums[i];
18+
nums[i] = nums[j];
19+
nums[j] = n;
20+
}
21+
22+
private static int partition(int[] nums, int lo, int hi) {
23+
// Partition into nums[lo..i-1], a[i], nums[i+1..hi].
24+
// where a[i] will be at its final place.
25+
int i = lo, j = hi + 1; // left and right scan indices
26+
int pivot = nums[i];
27+
while (true) {
28+
// Scan right, scan left, check for scan complete, and exchange.
29+
while (nums[++i] < pivot) {
30+
if (i == hi) {
31+
break;
32+
}
33+
}
34+
35+
while (pivot < nums[--j]) {
36+
if (j == lo) {
37+
break;
38+
}
39+
}
40+
41+
if (i >= j) {
42+
break;
43+
}
44+
45+
exch(nums, i, j);
46+
}
47+
48+
exch(nums, lo, j); // put pivot = nums[j] into its final position
49+
return j; // with nums[lo..j-1] <= nums[j] <= nums[j+1..hi].
50+
}
51+
52+
private static void sort(int[] nums, int lo, int hi) {
53+
if (hi <= lo) {
54+
return;
55+
}
56+
57+
int p = partition(nums, lo, hi);
58+
sort(nums, lo, p - 1);
59+
sort(nums, p + 1, hi);
60+
}
61+
62+
// space: O(1)
63+
// runtime:
64+
// - O(NlgN) on avg,
65+
// - O(N^2) worst case, if the input is reverse sorted for e.g.
66+
// we can avoid worst-case by shuffling before sort.
67+
public static void sort(int[] nums) {
68+
if (nums == null || nums.length < 2) {
69+
return;
70+
}
71+
72+
// shuffle the array to avoid the worst-case perf
73+
Collections.shuffle(Arrays.asList(nums));
74+
sort(nums, 0, nums.length - 1);
75+
System.out.println(Arrays.toString(nums));
76+
}
77+
78+
public static void main(String[] args) {
79+
sort(new int[] { 1, 2, 3, 4, 5 });
80+
sort(new int[] { 5, 4, 3, 2, 1 });
81+
sort(new int[] { 1, 2, 3, 4, 5, 4, 3, 2, 1 });
82+
}
83+
84+
}

src/problems/leetcode/SelectionSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class SelectionSort {
77
// space complexity: O(1)
88
// runtime complexity: O(N^2)
99
public static void sort(int[] nums) {
10-
if (nums == null || nums.length < 1) {
10+
if (nums == null || nums.length < 2) {
1111
return;
1212
}
1313

0 commit comments

Comments
 (0)