sort from Java Collections O(n^2): Bubble, Selection, InsertionO(nlog(n)): Merge, Quick, HeapO(n): Counting, Radix, BucketO(n): linear searchO(log(n)): binary search (Java Collections):::::{tab-set} ::::{tab-item} Descriptions
:::: ::::{tab-item} IO format
N number of cars. N pairs are the cars and their relative displacement.N until N == 0.:::: ::::{tab-item} Thought Process
:::: ::::{tab-item} AC
:::: :::::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
## Special Sorting Problems
- Inversion Index: count the number of swap to make a list into a sorted order
- `O(n^2)`: actual runs bubble sort.
- `O(nlog(n))`:
- Modification of Merge Sort: increment count by one when the `front right` is selected in the merging process before the `front left`.
- Sorting in linear time:
- Depending on data type
- `O(n + k)` Counting Sort
- `O(d * (n + k))` Radix Sort
<figure
>
<picture>
<!-- Auto scaling with imagemagick -->
<!--
See https://www.debugbear.com/blog/responsive-images#w-descriptors-and-the-sizes-attribute and
https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images for info on defining 'sizes' for responsive images
-->
<source
class="responsive-img-srcset"
srcset="/assets/img/courses/csc495/02-ds-algo/inversion_index-480.webp 480w,/assets/img/courses/csc495/02-ds-algo/inversion_index-800.webp 800w,/assets/img/courses/csc495/02-ds-algo/inversion_index-1400.webp 1400w,"
type="image/webp"
sizes="95vw"
>
<img
src="/assets/img/courses/csc495/02-ds-algo/inversion_index.png"
width="50%"
height="auto"
alt="Inversion Index Example"
data-zoomable
loading="lazy"
onerror="this.onerror=null; $('.responsive-img-srcset').remove();"
>
</picture>
</figure>
[Problem's link](https://open.kattis.com/problems/height)
:::::{tab-set}
::::{tab-item} Descriptions
- Students placement: Insertion Sort.
- Count number of displacements due to insertion.
::::
::::{tab-item} IO format
- First value is the `P` number of cases
- Each subsequent `21` integers:
- First integer is case number (to be used in output)
- Next twenty integers are the students' heights.
::::
::::{tab-item} Thought Process
- Read each line and place 20 integers into array
- Perform insertion sort
- Increase count if a swap is done.
::::
::::{tab-item} AC
<script src="https://gist.github.com/linhbngo/3529c879f8375d9e7eb29f355805bc0c.js?file=kattis_height.java"></script>
::::
:::::
lightweight small set of Boolean values.BitSet class of Java.1 0 0 0 1 0 5 4 3 2 1 0 32 16 8 4 2 1 F E D C B A.{1, 5} or {F, B} or {32, 2}.<< 1: multiply by 2/shift left 1<< 2: multiple by 4/shift left 2>> 1: divide by 2/shift right 1.j, use bitwise OR: S = S | (1 << j) 1 << j will shift 1 forward j position, every other bits remain 0.j to 1.j is on, use bitwise AND: T = S & (1 << j). T == 0: j bit is 0T != 0: j bit is 1j: S = S & ~(1 << j) j: S = S ^ (1 << j) T = ((S) & -(S)) n: S = (1 << n) - 1 :::::{tab-set} ::::{tab-item} Descriptions
N <= 250000 1 <= n <= 30 k position of the n-bit Reflected Gray Code: 0 <= k <= 2^n.:::: ::::{tab-item} IO format
:::: ::::{tab-item} Thought Process
:::: ::::{tab-item} AC
:::: :::::
:::::{tab-set} ::::{tab-item} Descriptions
f is the standard cell-based transformation that is similar to the Game of Life’s principle: i so that function f transforms g back to itself.:::: ::::{tab-item} IO format
N number of grids.0 or 1.:::: ::::{tab-item} Thought Process
f. g is transformed back to itself, this means we need to keep track of the past data structures.i is easy: we only need to know when the current grid matches the original grid.i steps, this means there is another circular tranformation back to itself after an additional i steps.circular transformation that does not contain the original grid, this means we have hit the infinite case.
:::: ::::{tab-item} AC
:::: :::::
1
2
3
4
5
6
7
8
9
10
11
12
13
14
## Big Integer
- Use [Java's BigInteger](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/math/BigInteger.html) for this purpose
- Kattis: [Simple Addition](https://open.kattis.com/problems/simpleaddition)
- Kattis: [Wizardsof Odds](https://open.kattis.com/problems/wizardofodds)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
## Non-linear Data Structures: Binary Heap (Priority Queue)
- [Java PriorityQueue](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/PriorityQueue.html).
- From node `i`:
- To parent: `floor(i)` or `i>>1`
- To left child: `2 * i` or `i<<1`
- To right child: `2 * i + 1` or `(i<<1) + 1`
- Heap property:
- Items on left and right subtree of root `x` are always smaller than `x`.
- The root is always the max-value item.
- Highest value-item can be dequeued in `log(n)` time.
- New item can be enqueue in `log(n)` time.
- Important components for:
- Prim's and Kruskal's algorithms for minimum spanning tree.
- Dijkstra's Single Source Shortest Path.
[Problem's link](https://open.kattis.com/problems/knigsoftheforest)
:::::{tab-set}
::::{tab-item} Descriptions
::::
::::{tab-item} Thought Process
- PriorityQueue
- Two queues
- Rewrite comparator to custom compare.
- [Writing custom comparator](https://www.callicoder.com/java-priority-queue/)
- [More description on comparator - how do they compare?](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Comparator.html)
::::
::::{tab-item} AC
<script src="https://gist.github.com/linhbngo/3529c879f8375d9e7eb29f355805bc0c.js?file=kattis_knigsoftheforest.java"></script>
::::
:::::
- Kattis: [Stock Prices](https://open.kattis.com/problems/stockprices)
Direct Addressing Table.:::::{tab-set} ::::{tab-item} Descriptions
:::: ::::{tab-item} Thought Process
:::: ::::{tab-item} AC
:::: :::::
```
log(n) time.lastKey for max value, firstKey for smallest value.v, then ranking of v is k.k=1 and k=n: manual comparison, best run time is n.nlog(n).n algorithm: Utilize RandPartition of QuickSort: q = RandPartition(A, 0, n-1), all element <= A[q] will be placed before the pivot, so A[q] will be in its correct order statistics, which is q+1.nlog(n) log(n) by traversing down the tree.