Skip to content

Commit 2ae2c1c

Browse files
committed
Updated
1 parent 2112cb8 commit 2ae2c1c

File tree

4 files changed

+59
-29
lines changed

4 files changed

+59
-29
lines changed

min_heap.md

+19-9
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ Min Heap Algorithm
22
========================
33

44
Implementations:
5-
[MINHEAP]
6-
```
5+
6+
```java
77
class MinHeap {
88
int[] heap;
99
int maxSize;
@@ -14,18 +14,23 @@ class MinHeap {
1414
heap = new heap[maxSize + 1];
1515
heap[0] = Integer.MIN_VALUE;
1616
}
17+
1718
int parent(int position) {
1819
return position / 2;
1920
}
21+
2022
int left(int position) {
2123
return position * 2;
2224
}
25+
2326
int right(int position) {
2427
return position * 2 + 1;
2528
}
29+
2630
boolean isLeaf(int position) {
2731
return (position >= counter/2 && position <= counter)
2832
}
33+
2934
void insert(int element) {
3035
heap[++counter] = element;
3136
int i = counter;
@@ -34,12 +39,14 @@ class MinHeap {
3439
current = parent(current);
3540
}
3641
}
42+
3743
int remove() {
3844
int popped = heap[FRONT];
3945
heap[FRONT] = heap[counter--];
4046
minify(FRONT);
4147
return popped;
4248
}
49+
4350
void minify(int position) {
4451
if (!isLeaf(pos)) {
4552
if (heap[pos]) > heap[left(pos)] || heap[pos] < heap[right(pos)]) {
@@ -53,25 +60,28 @@ class MinHeap {
5360
}
5461
}
5562
}
63+
5664
void extractMin() {
57-
if (counter == 0) return Integer.MAX_VALUE;
65+
if (counter == 0) {
66+
return Integer.MAX_VALUE;
67+
}
68+
5869
int root = heap[0];
70+
5971
if (counter > 1) {
6072
heap[0] = heap[counter - 1];
6173
}
74+
6275
counter--;
6376
}
6477
}
65-
```
66-
[/MINHEAP]
6778

68-
[KTH SMALLEST]
69-
```
7079
int kthSmallest(int a[], int n, int k) {
7180
//build the heap and use the insert method mh
72-
for (int i=0; i < k-1; i++)
81+
for (int i=0; i < k-1; i++) {
7382
mh.extractMin();
83+
}
84+
7485
return mh.getMin();
7586
}
7687
```
77-
[/KTH SMALLEST]

quick_sort.md

+16-13
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,32 @@ QuickSort Algorithm
33

44
Implementations:
55

6-
[QUICKSORT]
7-
8-
```
6+
```java
97
//O(n log n)
108
void quickSort(int a[], int left, int right) {
119
int index = partition(a, left, right);
12-
if (left < index - 1)
10+
if (left < index - 1) {
1311
quickSort(a, left, index - 1);
14-
if (index < right)
12+
}
13+
14+
if (index < right) {
1515
quickSort(a, index, right);
16+
}
1617
}
17-
```
18-
[/QUICKSORT]
1918

20-
[PARTITION]
21-
```
2219
int partition(int numbers[], int left, int right) {
2320
int i = left, j = right;
2421
int tmp;
2522
int pivot = numbers[(left + right) / 2];
2623
while (i <= j) {
27-
while (numbers[i] < pivot) i++;
28-
while (numbers[j] > pivot) j--;
24+
while (numbers[i] < pivot) {
25+
i++;
26+
}
27+
28+
while (numbers[j] > pivot) {
29+
j--;
30+
}
31+
2932
if (i <= j) {
3033
tmp = numbers[i];
3134
arr[i] = numbers[j];
@@ -34,7 +37,7 @@ int partition(int numbers[], int left, int right) {
3437
j--;
3538
}
3639
}
40+
3741
return i;
3842
}
39-
```
40-
[/PARTITION]
43+
```

selection_rank.md

+19-6
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,29 @@ int rank(int[] array, int left, int right, int rank) {
1919
int pivot = array[random];
2020
int leftEnd = partition(array, left, right, pivot); //partition
2121
int leftSize = leftEnd - left + 1;
22-
if (leftSize == rank + 1) return Math.max(array[left], array[leftEnd]);
23-
else if (rank < leftSide) return rank(array, left, leftEnd, rank);
24-
else return rank(array, leftEnd + 1, right, rank - leftSize);
22+
if (leftSize == rank + 1) {
23+
return Math.max(array[left], array[leftEnd]);
24+
} else if (rank < leftSide) {
25+
return rank(array, left, leftEnd, rank);
26+
} else {
27+
return rank(array, leftEnd + 1, right, rank - leftSize);
28+
}
2529
}
2630

2731
int partition(int[] array, int left, int right, int pivot) {
2832
while (true) {
29-
while (left <= right && array[left] <= pivot) left++;
30-
while (left <= right && array[right] > pivot) right--;
31-
if (left > right) return left -1;
33+
while (left <= right && array[left] <= pivot) {
34+
left++;
35+
}
36+
37+
while (left <= right && array[right] > pivot) {
38+
right--;
39+
}
40+
41+
if (left > right) {
42+
return left -1;
43+
}
44+
3245
swap(array, left, right);
3346
}
3447
}

strings.md

+5-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ Strings
55

66
```java
77
boolean isRotation(String s1, String s2) {
8-
if (s1 == null || s2 == null || s1.length() == 0 || s2.length() == 0) return false;
8+
if (s1 == null || s2 == null || s1.length() == 0 || s2.length() == 0) {
9+
return false;
10+
}
11+
912
if (s1.length() == s2.length()) {
1013
return isSubString(s1+s1, s2);
1114
}
15+
1216
return false;
1317
}
1418
```

0 commit comments

Comments
 (0)