├── README.md ├── src ├── datastructure │ ├── Graph.java │ ├── Node.java │ ├── HashTable.java │ ├── LinkedList.java │ ├── BinarySearchTree.java │ └── ArrayList.java └── algorithm │ ├── package-info.java │ ├── Search.java │ └── Sort.java ├── .gitattributes └── .gitignore /README.md: -------------------------------------------------------------------------------- 1 | javadata 2 | ======== 3 | 4 | Common data structures and algorithms in Java. 5 | -------------------------------------------------------------------------------- /src/datastructure/Graph.java: -------------------------------------------------------------------------------- 1 | package datastructure; 2 | 3 | public class Graph { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/datastructure/Node.java: -------------------------------------------------------------------------------- 1 | package datastructure; 2 | 3 | public class Node { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/datastructure/HashTable.java: -------------------------------------------------------------------------------- 1 | package datastructure; 2 | 3 | public class HashTable { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/datastructure/LinkedList.java: -------------------------------------------------------------------------------- 1 | package datastructure; 2 | 3 | public class LinkedList { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/datastructure/BinarySearchTree.java: -------------------------------------------------------------------------------- 1 | package datastructure; 2 | 3 | public class BinarySearchTree { 4 | 5 | } 6 | -------------------------------------------------------------------------------- /src/algorithm/package-info.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | /** 5 | * @author charleslai 6 | * 7 | */ 8 | package algorithm; -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /src/datastructure/ArrayList.java: -------------------------------------------------------------------------------- 1 | package datastructure; 2 | 3 | public class ArrayList { 4 | private T[] array; 5 | private int length; 6 | private double loadFactor = .85; 7 | 8 | @SuppressWarnings("unchecked") 9 | public ArrayList(){ 10 | array = (T[]) new Object[10]; 11 | } 12 | 13 | public ArrayList(T[] array){ 14 | this.array = array; 15 | } 16 | 17 | @SuppressWarnings("unchecked") 18 | public void add(T v){ 19 | if (length/array.length > loadFactor) { 20 | // Double arraylist size 21 | T[] temparray = array; 22 | array = (T[]) new Object[array.length * 2]; 23 | // Copy elements into new array 24 | for(int i = 0; i < length; i++) { 25 | array[i] = temparray[i]; 26 | } 27 | } 28 | // Push the value onto the end of the arraylist 29 | array[length+1] = v; 30 | length++; 31 | } 32 | 33 | public void remove(int k){ 34 | // Enforce arraylist bounds 35 | assert k < length; 36 | // Push down values and leave last item 37 | for(; k < length-1; k++) { 38 | array[k] = array[k+1]; 39 | } 40 | length--; 41 | 42 | } 43 | 44 | public T get(int k){ 45 | // Enforce k is a valid index 46 | assert k < length; 47 | return array[k]; 48 | 49 | } 50 | 51 | public void set(int k, T v){ 52 | // Enforce k is a valid index 53 | assert k < length; 54 | array[k] = v; 55 | } 56 | 57 | public int getLength() { 58 | return length; 59 | } 60 | 61 | public double getLoadFactor() { 62 | return loadFactor; 63 | } 64 | 65 | public void setLoadFactor(double loadFactor) { 66 | this.loadFactor = loadFactor; 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/algorithm/Search.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package algorithm; 5 | 6 | /** 7 | * Search Class 8 | * Contains: 9 | * Linear Search 10 | * Binary Search 11 | * @author charleslai 12 | * 13 | */ 14 | public class Search { 15 | /** 16 | * A simple, "brute-force" searching algorithm. 17 | * @param T[] arr - an array of objects that implement Comparable 18 | * @param T query - an search query object that implements Comparable 19 | */ 20 | public static > Boolean linearSearch(T[] arr, T query) { 21 | //Iterate through the entire array 22 | for (int i = 0; i < arr.length; i++) { 23 | if (arr[i] == query) { 24 | return true; 25 | } 26 | } 27 | return false; 28 | } 29 | 30 | /** 31 | * A Java implementation of binary search 32 | * @param T[] arr - an array of objects that implement Comparable 33 | * @param T query - an search query object that implements Comparable 34 | */ 35 | public static > Boolean binarySearch(T[] arr, T query) { 36 | int low = 0; 37 | int high = arr.length - 1; 38 | // Keep searching until we can't divide any further 39 | while (low <= high) { 40 | //Divide and Conquer Step: find midpoint 41 | int mid = (low + high)/2; 42 | T midVal = arr[mid]; 43 | int result = midVal.compareTo(query); 44 | // If midVal > query - binary search bottom half 45 | if (result > 0) { 46 | high = mid - 1; 47 | } 48 | // if midVal < query - binary search top half 49 | else if (result < 0) { 50 | low = mid + 1; 51 | } 52 | // If result == 0, then value found 53 | else { 54 | return true; 55 | } 56 | } 57 | return false; 58 | } 59 | 60 | /** 61 | * Class main method for testing 62 | * @param args 63 | */ 64 | public static void main(String[] args) { 65 | Integer[] a = {1,2,3,4,5}; 66 | String[] b = {"Hello", ",", "World", "!"}; 67 | System.out.println(Search.linearSearch(a, 5)); 68 | System.out.println(Search.binarySearch(a, 5)); 69 | System.out.println(Search.linearSearch(b, "Hello")); 70 | } 71 | 72 | } 73 | -------------------------------------------------------------------------------- /src/algorithm/Sort.java: -------------------------------------------------------------------------------- 1 | /** 2 | * 3 | */ 4 | package algorithm; 5 | 6 | import java.util.Arrays; 7 | 8 | /** 9 | * Sort Class 10 | * Contains: 11 | * Selection Sort 12 | * Insertion Sort 13 | * Quick Sort 14 | * Merge Sort 15 | * @author charleslai 16 | * 17 | */ 18 | public class Sort> { 19 | /** 20 | * Selection sort procedure: 21 | * @param T[] arr - an array of objects that implement Comparable 22 | * 23 | */ 24 | public void selectionSort(T[] arr) { 25 | //Initialize values 26 | int iMin; 27 | //Iterate through the entire array 28 | for (int i = 0; i < arr.length; i++) { 29 | // Find the min element in the unsorted array arr[i .. arr.length-1] 30 | // Assume the min element is the first unsorted element at first 31 | iMin = i; 32 | for (int j = i+1; j < arr.length; j++) { 33 | // If a value in the unsorted list is < arr[iMin], update iMin 34 | if (arr[j].compareTo(arr[iMin]) < 0) { 35 | iMin = j; 36 | } 37 | } 38 | // If the iMin had to be updated, swap the values to maintain min invariant 39 | if (iMin != i) { 40 | swap(arr, i, iMin); 41 | } 42 | } 43 | } 44 | 45 | /** 46 | * Insertion sort: A worst case O(n^2) sorting algorithm. 47 | * @param T[] arr - an array of objects that implement Comparable 48 | */ 49 | public Boolean insertionSort(T[] arr) { 50 | //Iterate through the entire array 51 | for (int i = 0; i < arr.length; i++) { 52 | /*Comparable temp = arr[i]; 53 | int k; 54 | for (k = i; 0 < k && temp < arr[k-1]; k--) { 55 | arr[k] = arr[k-1]; 56 | } 57 | arr[k] = temp;*/ 58 | } 59 | return false; 60 | } 61 | 62 | /** 63 | * Quicksort: An average case O(nlogn) sorting algorithm. 64 | * @param arr: 65 | * 66 | */ 67 | public void quickSort(T[] arr) { 68 | return; 69 | } 70 | 71 | /** 72 | * Mergesort: An average case O(nlogn) sorting algorithm. 73 | * @param arr: 74 | * 75 | */ 76 | public void mergeSort(T[] arr){ 77 | return; 78 | } 79 | 80 | /*=============================================================================== 81 | * 82 | * HELPER FUNCTIONS 83 | * 84 | *==============================================================================*/ 85 | /** 86 | * Swap Procedure 87 | * @param T[] arr - the array that contains the indices we want to swap 88 | * @param int i - the index of the value we want to swap with arr[j] 89 | * @param int j - the index of the value we want to swap with arr[i] 90 | * Preconditions: i,j must both be valid indicies of arr 91 | */ 92 | public void swap (T[] arr, int i, int j){ 93 | T temp = arr[i]; 94 | arr[i] = arr[j]; 95 | arr[j] = temp; 96 | } 97 | 98 | public static void main(String[] args) { 99 | Sort intSort = new Sort(); 100 | Integer[] a = {5,4,3,2,1,0}; 101 | intSort.selectionSort(a); 102 | System.out.println(Arrays.toString(a)); 103 | } 104 | 105 | } 106 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | --------------------------------------------------------------------------------