QuickSort is a sorting algorithm developed by Tony Hoare that, on average, makes
O(n log n) comparisons to sort n items. It is also known as partition-exchange
sort because of its use of the partition
algorithm. In the worst
case, it makes O(n2) comparisons, though this behavior is rare. QuickSort is
often faster in practice than other O(n log n) algorithms.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.util.Random | |
// print unshuffled range | |
(1 to 10) foreach { i => println(i) } | |
// print shuffled range | |
val r = new Random() | |
r.shuffle(1 to 10 toIterable) foreach { i => println(i) } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalation.linalgebra.VectorD | |
import scalation.random.Normal | |
object NaiveBayes extends App { | |
val miles = VectorD(1300, 900, 2000, 1450, 2190, 1600, 300, 3100, 1521, 1786) | |
val mean = milel.sum / miles.dim.toDouble | |
val variance = ((miles - mean) ~^ 2).sum / (miles.dim.toDouble - 1) | |
def PrMiles (x: Double) = Normal(mean, variance).pf(x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Search { | |
public static <T extends Comparable<T>> int binarySearch(T[] array, T value, int lo, int hi) { | |
if (lo < hi) { | |
int mid = (lo / 2) + (hi / 2); | |
int cmp = array[mid].compareTo(value); | |
if (cmp < 0) return binarySearch(array, value, lo, mid - 1); | |
if (cmp > 0) return binarySearch(array, value, mid + 1, hi); | |
return mid; | |
} // if |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A disjoint sets data structure. | |
* | |
* @author Michael E. Cotterell <mepcotterell@gmail.com> | |
* @param n the number of initial disjoint sets | |
*/ | |
class OldDisjointSets (n: Int) { | |
// make sure we're using mutable sets | |
import scala.collection.mutable.{ BitSet, Set } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Armstrong { | |
public static void main(String[] args) { | |
for (int a = 0; a < 9; a++) { | |
for (int b = 0; b < 9; b++) { | |
for (int c = 0; c < 9; c++) { | |
int n = a * 100 + b * 10 + c; | |
int a3 = a * a * a; | |
int b3 = b * b * b; | |
int c3 = c * c * c; | |
if (a3 + b3 + c3 == n) System.out.println(n); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; add line numbers | |
(global-linum-mode 1) | |
;; display line numbers and column numbers | |
(setq line-number-mode t) | |
(setq column-number-mode t) | |
;; make sure the line numbers don't touch the text | |
(setq linum-format "%d ") |
When you declare a variable of any type (including class/reference types), some space is reserved in memory in order to hold the value of the variable. The variable is just a symbolic name for this value in memory.
Let's say we declare a long.
long x;
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Linear Congruential Generator | |
* | |
* This implementation allows you to grab an individual number by its index. It | |
* also provides a stats object for displaying statistics about the lcg. | |
* | |
* @see http://en.wikipedia.org/wiki/Linear_congruential_generator | |
* @author Michael E. Cotterell <mepcotterell@gmail.com> | |
* @param a the multiplier | |
* @param c the increment |