Skip to content

Instantly share code, notes, and snippets.

View vkostyukov's full-sized avatar

Vladimir Kostyukov vkostyukov

View GitHub Profile
/**
* A tiny class that extends a list with four combinatorial operations:
* ''combinations'', ''subsets'', ''permutations'', ''variations''.
*
* You can find all the ideas behind this code at blog-post:
*
* http://vkostyukov.ru/posts/combinatorial-algorithms-in-scala/
*
* How to use this class.
*
/**
* Time - O(n)
* Space - O(1)
*/
def waterfall(a: Array[Int]): Int = {
// get the max index
var maxIndex = a.indexOf(a.max)
// answer
var v = 0
@vkostyukov
vkostyukov / IntHeap.scala
Created October 21, 2013 16:59
Simplified version of Heap for lambdadays.org.
// Heap.scala for lambdadays.org
abstract sealed class Heap {
def min: Int
def left: Heap
def right: Heap
def size: Int
def height: Int
@vkostyukov
vkostyukov / RBTree.scala
Created September 2, 2013 16:34
Simplified RB-Tree for Scala NSK User group.
abstract sealed class Tree {
def value: Int
def left: Tree
def right: Tree
def isEmpty: Boolean
def isBlack: Boolean
/**
* Time - O(log n)
* Space - O(log n)
@vkostyukov
vkostyukov / IntTree.scala
Created September 2, 2013 15:06
Simplified version of BST (NSK Scala user group)
import scala.annotation.tailrec
abstract sealed class Tree {
def value: Int
def left: Tree
def right: Tree
def isEmpty: Boolean
/**
* Time - O(log n)
* Space - O(log n)
@vkostyukov
vkostyukov / IntList.scala
Created September 2, 2013 14:52
Simplified version of List (Scala NSK user group)
import scala.annotation.tailrec
abstract sealed class List {
def head: Int
def tail: List
def isEmpty: Boolean
/**
* Time - O(n)
* Space - O(n)
*/
@vkostyukov
vkostyukov / Tree.scala
Created September 2, 2013 01:38
A BST for PFDS in Scala (NSK usergroup)
import scala.annotation.tailrec
abstract sealed class Tree[+A] {
def value: A
def left: Tree[A]
def right: Tree[A]
def isEmpty: Boolean
def size: Int
/**
* Time - O(log n)
@vkostyukov
vkostyukov / Queue.scala
Created September 1, 2013 12:24
A Queue for PFDS in Scala (NSK user group)
class Queue[+A](in: List[A] = Nil, out: List[A] = Nil) {
/**
* Time - O(1)
* Space - O(1)
*/
def enqueue[B >: A](x: B): Queue[B] = new Queue(x :: in, out)
/**
* Time - O(1)
@vkostyukov
vkostyukov / List.scala
Created September 1, 2013 08:29
A List for PFDS in Scala (NSK user group)
import scala.annotation.tailrec
abstract sealed class List[+A] {
def head: A
def tail: List[A]
def isEmpty: Boolean
/**
* Time - O(n)
* Space - O(n)
*/
@vkostyukov
vkostyukov / BinarysearchVSDualPivotBinarysearch.java
Last active December 20, 2015 21:58
Experiments with Dual-Pivot Binary Search.
/**
* Experiments with Dual-Pivot Binary Search.
* Written by Vladimir Kostyukov, http://vkostyukov.ru
*/
package dpbs;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.annotations.Scope;