You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -524,6 +524,14 @@ Examples of data structures developed in the book [*Algorithms, 4th Edition, Rob
524
524
| ternary search trie | 5.3 | TST | three links per node |
525
525
526
526
527
+
528
+
<br/>
529
+
<divalign="right">
530
+
<b><a href="#top">↥ back to top</a></b>
531
+
</div>
532
+
<br/>
533
+
534
+
527
535
### 2.3. Analysis of Algorithms
528
536
529
537
**Reasons to analyze algorithms**:
@@ -597,6 +605,14 @@ Total memory usage for a data type value:
597
605
598
606
*Deep memory usage*: If array entry or instance variable is a reference, add memory (recursively) for referenced object.
599
607
608
+
609
+
<br/>
610
+
<divalign="right">
611
+
<b><a href="#top">↥ back to top</a></b>
612
+
</div>
613
+
<br/>
614
+
615
+
600
616
### 2.4. Union-Find
601
617
602
618
We shall consider three different implementations, all based on using the site-indexed `id[]` array, to determine whether two sites are in the same connected component.
@@ -832,6 +848,14 @@ private static void exch(Comparable[] a, int i, int j)
832
848
833
849
See more: [Sorting Algorithms Animations](https://www.toptal.com/developers/sorting-algorithms).
834
850
851
+
852
+
<br/>
853
+
<divalign="right">
854
+
<b><a href="#top">↥ back to top</a></b>
855
+
</div>
856
+
<br/>
857
+
858
+
835
859
### 3.1. Selection sort
836
860
837
861
- In iteration `i`, find index min of smallest remaining entry.
@@ -873,6 +897,14 @@ public class Selection
873
897
874
898
**Data movement is minimal**. Linear number of exchanges.
875
899
900
+
901
+
<br/>
902
+
<divalign="right">
903
+
<b><a href="#top">↥ back to top</a></b>
904
+
</div>
905
+
<br/>
906
+
907
+
876
908
### 3.2. Insertion sort
877
909
878
910
- In iteration `i`, swap `a[i]` with each larger entry to its left.
@@ -922,6 +954,14 @@ e.g.: `A E E L M O T R X P S`
922
954
**Proposition**. For partially-sorted arrays, insertion sort runs in linear time.
923
955
> *Pf*. Number of exchanges equals the number of inversions. (number of compares = exchanges + (N – 1).)
924
956
957
+
958
+
<br/>
959
+
<divalign="right">
960
+
<b><a href="#top">↥ back to top</a></b>
961
+
</div>
962
+
<br/>
963
+
964
+
925
965
### 3.3. Shellsort
926
966
927
967
Shellsort is a simple extension of insertion sort that gains speed by allowing exchanges of array entries that are far apart, to produce partially sorted arrays that can be efficiently sorted, eventually by insertion sort.
@@ -981,6 +1021,14 @@ public class Shell {
981
1021
> - Average-case performance?
982
1022
983
1023
1024
+
1025
+
<br/>
1026
+
<divalign="right">
1027
+
<b><a href="#top">↥ back to top</a></b>
1028
+
</div>
1029
+
<br/>
1030
+
1031
+
984
1032
### 3.4. Shuffling
985
1033
986
1034
*Goal*. Rearrange array so that result is a uniformly random permutation.
@@ -1211,6 +1259,14 @@ private static void sort(Comparable[] a, Comparable[] aux, int lo, int hi)
1211
1259
}
1212
1260
```
1213
1261
1262
+
1263
+
<br/>
1264
+
<divalign="right">
1265
+
<b><a href="#top">↥ back to top</a></b>
1266
+
</div>
1267
+
<br/>
1268
+
1269
+
1214
1270
### 4.3. Bottom-up mergesort
1215
1271
1216
1272
Bottom-up mergesort consists of a sequence of passes over the whole array, doing `sz-by-sz` merges, starting with `sz` equal to `1` and doubling `sz` on each pass. The final subarray is of size `sz` only when the array size is an even multiple of `sz` (otherwise it is less than `sz`).
@@ -1282,6 +1338,14 @@ Lower bound may not hold if the algorithm has information about:
1282
1338
1283
1339
*Digital properties of keys*. We can use digit/character compares instead of key compares for numbers and strings.
1284
1340
1341
+
1342
+
<br/>
1343
+
<divalign="right">
1344
+
<b><a href="#top">↥ back to top</a></b>
1345
+
</div>
1346
+
<br/>
1347
+
1348
+
1285
1349
### 4.5. Comparator
1286
1350
1287
1351
Comparator interface: sort using an *alternate order*.
@@ -1488,6 +1552,14 @@ public class Quick
1488
1552
1489
1553
*Proposition*. Quicksort is not stable.
1490
1554
1555
+
1556
+
<br/>
1557
+
<divalign="right">
1558
+
<b><a href="#top">↥ back to top</a></b>
1559
+
</div>
1560
+
<br/>
1561
+
1562
+
1491
1563
### 5.4. Quicksort: practical improvements
1492
1564
1493
1565
*Insertion sort small subarrays.*
@@ -1527,6 +1599,14 @@ private static void sort(Comparable[] a, int lo, int hi)
1527
1599
}
1528
1600
```
1529
1601
1602
+
1603
+
<br/>
1604
+
<divalign="right">
1605
+
<b><a href="#top">↥ back to top</a></b>
1606
+
</div>
1607
+
<br/>
1608
+
1609
+
1530
1610
### 5.5. Selection
1531
1611
1532
1612
*Goal*. Given an array of *N* items, find a `kth` smallest item.
@@ -1616,6 +1696,14 @@ public class Quick3way
1616
1696
1617
1697
Randomized quicksort with 3-way partitioning reduces running time from *linearithmic* to *linear* in broad class of applications.
1618
1698
1699
+
1700
+
<br/>
1701
+
<divalign="right">
1702
+
<b><a href="#top">↥ back to top</a></b>
1703
+
</div>
1704
+
<br/>
1705
+
1706
+
1619
1707
### 5.7. Sorting applications
1620
1708
1621
1709
Sorting algorithms are essential in a broad variety of applications:
@@ -1774,6 +1862,14 @@ Order of growth of running time for priority queue with N items:
1774
1862
| ordered array | N | 1 | 1 |
1775
1863
| goal | log N | log N | log N |
1776
1864
1865
+
1866
+
<br/>
1867
+
<divalign="right">
1868
+
<b><a href="#top">↥ back to top</a></b>
1869
+
</div>
1870
+
<br/>
1871
+
1872
+
1777
1873
### 6.5. Binary heaps
1778
1874
1779
1875
*Binary tree*. Empty or node with links to left and right binary trees.
@@ -2183,6 +2279,14 @@ public class FrequencyCounter
2183
2279
}
2184
2280
```
2185
2281
2282
+
2283
+
<br/>
2284
+
<div align="right">
2285
+
<b><a href="#top">↥ back to top</a></b>
2286
+
</div>
2287
+
<br/>
2288
+
2289
+
2186
2290
### 8.2. Ordered symbol tables
2187
2291
2188
2292
In typical applications, keys are *Comparable* objects, so the option exists of using the code `a.compareTo(b)` to compare two keys a and b.
0 commit comments