diff --git a/README.md b/README.md index 49b9be2c..ecc6437b 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,148 @@ java-algorithms-implementation ============================== Algorithms and Data Structures implemented in Java + + +This is a collection of algorithms and data structures which I've implement over the years in my academic and professional life. The code isn't overly-optimized but is written to be correct and readable. The algorithms and data structures are well tested and, unless noted, are believe to be 100% correct. + +* Created by Justin Wetherell +* Google: http://code.google.com/p/java-algorithms-implementation +* Github: http://github.com/phishman3579/java-algorithms-implementation +* LinkedIn: http://www.linkedin.com/in/phishman3579 +* E-mail: phishman3579@gmail.com +* Twitter: http://twitter.com/phishman3579 + +# What's been implemented: + +## Data Structures +* AVL Tree +* B-Tree +* Binary Heap [backed by an array or a tree] +* Binary Search Tree +* Compact Suffix Trie [backed by a Patricia Trie] +* Graph + + Undirected + + Directed (Digraph) +* Hash Array Mapped Trie (HAMT) +* Hash Map (associative array) +* Interval Tree +* KD Tree (k-dimensional tree or k-d tree) +* List [backed by an array or a linked list] +* Matrix +* Patricia Trie +* Quad-Tree (Point-Region or MX-CIF) +* Queue [backed by an array or a linked list] +* Radix Trie (associative array) [backed by a Patricia Trie] +* Red-Black Tree +* Segment Tree +* Skip List +* Splay Tree +* Stack [backed by an array or a linked list] +* Suffix Tree (Ukkonen's algorithm) +* Suffix Trie [backed by a Trie] +* Treap +* Tree Map (associative array) [backed by an AVL Tree] +* Trie +* Trie Map (associative array) [backed by a Trie] + +## Mathematics +* Distance + + chebyshev + + euclidean +* Division + + using a loop + + using recursion + + using shifts and multiplication + + using only shifts + + using logarithm +* Multiplication + + using a loop + + using recursion + + using only shifts + + using logarithms +* Primes + + is prime + + prime factorization + + sieve of eratosthenes + +## Numbers +* Integers + + to binary String + - using divide and modulus + - using right shift and modulus + - using BigDecimal + - using divide and double + + is a power of 2 + - using a loop + - using recursion + - using logarithm + - using bits + + greatest common divisor + - using Euclid's algorithm + + to English (e.g. 1 would return "one") +* Longs + + to binary String + - using divide and modulus + - using right shift and modulus + - using BigDecimal + +## Path +* Find shortest path(s) in a Graph from a starting Vertex + - Dijkstra's algorithm (non-negative weight graphs) + - Bellman-Ford algorithm (negative and positive weight graphs) +* Find minimum spanning tree + - Prim's (undirected graphs) +* Find all pairs shortest path + - Johnsons's algorithm (negative and positive weight graphs) + - Floyd-Warshall (negative and positive weight graphs) +* Cycle detection + - Depth first search while keeping track of visited Verticies +* Topological sort + +## Search +* Get index of value in array + + Linear + + Quickselect + + Binary [sorted array input only] + + Optimized binary (binary until a threashold then linear) [sorted array input only] + + Interpolation [sorted array input only] + +## Sequences +* Find longest common subsequence (dynamic programming) +* Find number of times a subsequence occurs in a sequence (dynamic programming) +* Find i-th element in a Fibonacci sequence + + using a loop + + using recursion + + using matrix multiplication + + using Binet's formula +* Find total of all elements in a sequence + + using a loop + + using Triangular numbers + +## Sorts +* American Flag Sort +* Bubble Sort +* Counting Sort (Integers only) +* Heap Sort +* Insertion Sort +* Merge Sort +* Quick Sort +* Radix Sort (Integers only) +* Shell's Sort + +## String Functions +* Reverse characters in a string + + using additional storage (a String or StringBuilder) + + using in-place swaps + + using in-place XOR +* Reverse words in a string + + using char swaps and additional storage (a StringBuilder) + + using StringTokenizer and additional (a String) + + using split() method and additional storage (a StringBuilder and String[]) + + using in-place swaps +* Is Palindrome + + using additional storage (a StringBuilder) + + using in-place symetric element compares +* Subsets of characters in a String +* Edit (Levenshtein) Distance of two Strings + diff --git a/src/com/jwetherell/algorithms/sorts/AmericanFlagSort.java b/src/com/jwetherell/algorithms/sorts/AmericanFlagSort.java index ad91dbe4..1d6f0632 100644 --- a/src/com/jwetherell/algorithms/sorts/AmericanFlagSort.java +++ b/src/com/jwetherell/algorithms/sorts/AmericanFlagSort.java @@ -5,10 +5,14 @@ * distributes items into hundreds of buckets. Non-comparative sorting * algorithms such as radix sort and American flag sort are typically used to * sort large objects such as strings, for which comparison is not a unit-time - * operation. Family: Bucket. Space: In-place. Stable: False. + * operation. * - * Average case = O(n*k/d) Worst case = O(n*k/d) Best case = O(n*k/d) NOTE: n is - * the number of digits and k is the average bucket size + * Family: Bucket. + * Space: In-place. + * Stable: False. + * + * Average case = O(n*k/d) Worst case = O(n*k/d) Best case = O(n*k/d) + * NOTE: n is the number of digits and k is the average bucket size * * http://en.wikipedia.org/wiki/American_flag_sort * @@ -22,8 +26,7 @@ private AmericanFlagSort() { } public static Integer[] sort(Integer[] unsorted) { - int numberOfDigits = getMaxNumberOfDigits(unsorted); // Max number of - // digits + int numberOfDigits = getMaxNumberOfDigits(unsorted); // Max number of digits int max = 1; for (int i = 0; i < numberOfDigits - 1; i++) max *= 10; diff --git a/src/com/jwetherell/algorithms/sorts/BubbleSort.java b/src/com/jwetherell/algorithms/sorts/BubbleSort.java index 2a6b7fdc..b9ce8af8 100644 --- a/src/com/jwetherell/algorithms/sorts/BubbleSort.java +++ b/src/com/jwetherell/algorithms/sorts/BubbleSort.java @@ -6,7 +6,9 @@ * swapping them if they are in the wrong order. The pass through the list is * repeated until no swaps are needed, which indicates that the list is sorted. * - * Family: Exchanging. Space: In-place. Stable: True. + * Family: Exchanging. + * Space: In-place. + * Stable: True. * * Average case = O(n^2) Worst case = O(n^2) Best case = O(n) * diff --git a/src/com/jwetherell/algorithms/sorts/CountingSort.java b/src/com/jwetherell/algorithms/sorts/CountingSort.java index a52c84f2..d7ea3ba3 100644 --- a/src/com/jwetherell/algorithms/sorts/CountingSort.java +++ b/src/com/jwetherell/algorithms/sorts/CountingSort.java @@ -5,11 +5,14 @@ * to keys that are small integers; that is, it is an integer sorting algorithm. * It operates by counting the number of objects that have each distinct key * value, and using arithmetic on those counts to determine the positions of - * each key value in the output sequence. Family: Counting. Space: An Array of - * length r. Stable: True. + * each key value in the output sequence. * - * Average case = O(n+r) Worst case = O(n+r) Best case = O(n+r) NOTE: r is the - * range of numbers (0 to r) to be sorted. + * Family: Counting. + * Space: An Array of length r. + * Stable: True. + * + * Average case = O(n+r) Worst case = O(n+r) Best case = O(n+r) + * NOTE: r is the range of numbers (0 to r) to be sorted. * * http://en.wikipedia.org/wiki/Counting_sort * diff --git a/src/com/jwetherell/algorithms/sorts/HeapSort.java b/src/com/jwetherell/algorithms/sorts/HeapSort.java index e6aec3e8..6df121af 100644 --- a/src/com/jwetherell/algorithms/sorts/HeapSort.java +++ b/src/com/jwetherell/algorithms/sorts/HeapSort.java @@ -4,8 +4,11 @@ * Heapsort is a comparison-based sorting algorithm to create a sorted array (or * list), and is part of the selection sort family. Although somewhat slower in * practice on most machines than a well-implemented quicksort, it has the - * advantage of a more favorable worst-case O(n log n) runtime. Family: - * Selection. Space: In-place. Stable: False. + * advantage of a more favorable worst-case O(n log n) runtime. + * + * Family: Selection. + * Space: In-place. + * Stable: False. * * Average case = O(n*log n) Worst case = O(n*log n) Best case = O(n*log n) * @@ -34,24 +37,16 @@ private static > void sortHeap(T[] unsorted) { if (left >= index) // node has no left child break; int right = left + 1; - if (right >= index) { // node has a left child, but no right - // child + if (right >= index) { // node has a left child, but no right child if (unsorted[left].compareTo(unsorted[i]) > 0) - swap(left, i, unsorted); // if - // left - // child - // is - // greater - // than - // node + swap(left, i, unsorted); // if left child is greater than node break; } T ithElement = unsorted[i]; T leftElement = unsorted[left]; T rightElement = unsorted[right]; if (ithElement.compareTo(leftElement) < 0) { // (left > i) - if (unsorted[left].compareTo(rightElement) > 0) { // (left > - // right) + if (unsorted[left].compareTo(rightElement) > 0) { // (left > right) swap(left, i, unsorted); i = left; continue; diff --git a/src/com/jwetherell/algorithms/sorts/InsertionSort.java b/src/com/jwetherell/algorithms/sorts/InsertionSort.java index e29fc521..c402d8d3 100644 --- a/src/com/jwetherell/algorithms/sorts/InsertionSort.java +++ b/src/com/jwetherell/algorithms/sorts/InsertionSort.java @@ -4,7 +4,11 @@ * Insertion sort is a simple sorting algorithm: a comparison sort in which the * sorted array (or list) is built one entry at a time. It is much less * efficient on large lists than more advanced algorithms such as quicksort, - * heapsort, or merge sort. Family: Insertion. Space: In-place. Stable: True. + * heapsort, or merge sort. + * + * Family: Insertion. + * Space: In-place. + * Stable: True. * * Average case = O(n^2) Worst case = O(n^2) Best case = O(n) * diff --git a/src/com/jwetherell/algorithms/sorts/MergeSort.java b/src/com/jwetherell/algorithms/sorts/MergeSort.java index 38ffcced..0e3e174b 100644 --- a/src/com/jwetherell/algorithms/sorts/MergeSort.java +++ b/src/com/jwetherell/algorithms/sorts/MergeSort.java @@ -3,8 +3,11 @@ /** * Merge sort is an O(n log n) comparison-based sorting algorithm. Most * implementations produce a stable sort, which means that the implementation - * preserves the input order of equal elements in the sorted output. Family: - * Merging. Space: In-place. Stable: True. + * preserves the input order of equal elements in the sorted output. + * + * Family: Merging. + * Space: In-place. + * Stable: True. * * Average case = O(n*log n) Worst case = O(n*log n) Best case = O(n*log n) * diff --git a/src/com/jwetherell/algorithms/sorts/QuickSort.java b/src/com/jwetherell/algorithms/sorts/QuickSort.java index e7593270..1be2c4c2 100644 --- a/src/com/jwetherell/algorithms/sorts/QuickSort.java +++ b/src/com/jwetherell/algorithms/sorts/QuickSort.java @@ -3,10 +3,13 @@ import java.util.Random; /** - * Quicksort is a sorting algorithm which, on average, makes comparisons to sort - * n items. In the worst case, it makes comparisons, though this behavior is - * rare. Quicksort is often faster in practice than other algorithms. Family: - * Divide and conquer. Space: In-place. Stable: False. + * Quicksort is a sorting algorithm which, on average, makes O(n*log n) comparisons to sort + * n items. In the worst case, it makes O(n^2) comparisons, though this behavior is + * rare. Quicksort is often faster in practice than other algorithms. + * + * Family: Divide and conquer. + * Space: In-place. + * Stable: False. * * Average case = O(n) Worst case = O(n^2) Best case = O(n*log n) * diff --git a/src/com/jwetherell/algorithms/sorts/RadixSort.java b/src/com/jwetherell/algorithms/sorts/RadixSort.java index 9506c0f7..df0b00fa 100644 --- a/src/com/jwetherell/algorithms/sorts/RadixSort.java +++ b/src/com/jwetherell/algorithms/sorts/RadixSort.java @@ -8,11 +8,14 @@ * same significant position and value. A positional notation is required, but * because integers can represent strings of characters (e.g., names or dates) * and specially formatted floating point numbers, radix sort is not limited to - * integers. Family: Bucket. Space: 10 Buckets with at most n integers per - * bucket. Stable: True. + * integers. * - * Average case = O(n*k) Worst case = O(n*k) Best case = O(n*k) NOTE: n is the - * number of digits and k is the average bucket size + * Family: Bucket. + * Space: 10 Buckets with at most n integers per bucket. + * Stable: True. + * + * Average case = O(n*k) Worst case = O(n*k) Best case = O(n*k) + * NOTE: n is the number of digits and k is the average bucket size * * http://en.wikipedia.org/wiki/Radix_sort * @@ -28,10 +31,8 @@ private RadixSort() { public static Integer[] sort(Integer[] unsorted) { int[][] buckets = new int[NUMBER_OF_BUCKETS][10]; for (int i = 0; i < NUMBER_OF_BUCKETS; i++) - buckets[i][0] = 1; // Size is one since the size is stored in first - // element - int numberOfDigits = getMaxNumberOfDigits(unsorted); // Max number of - // digits + buckets[i][0] = 1; // Size is one since the size is stored in first element + int numberOfDigits = getMaxNumberOfDigits(unsorted); // Max number of digits int divisor = 1; for (int n = 0; n < numberOfDigits; n++) { int digit = 0; diff --git a/test.txt b/test.txt deleted file mode 100644 index e69de29b..00000000