Skip to content

Instantly share code, notes, and snippets.

@gtke
gtke / Quick Find Algorithm
Created September 2, 2012 21:57
Quick Find Algorithm Java Implementation
public class quickFind {
private int [] id;
public void QuickFindUF (int N){
id = new int [N];
for(int i = 0; i< N; i++){
id[i]=i;
}
}
@gtke
gtke / Quick Union Algorithm
Created September 2, 2012 22:15
Quick Union Algorithm Java Implentation
public class quickUnion {
private int[] id;
public void QuickUnionUF(int N){
id = new int [N];
for(int i = 0; i < N; i++){
id[i] = i;
}
}
@gtke
gtke / Sieve of Eratosthenes
Created September 10, 2012 18:40
Sieve of Eratosthenes - Algorithm that generates prime numbers
import java.util.Scanner;
public class Eratosthenes {
/**
* Sieve of Eratosthenes.
* Generates prime numbers.
* You can find more about the algorithm here:
* http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
* @author gtkesh
*/
@gtke
gtke / QuickSort
Created October 25, 2012 03:09
QuickSort java implementation
public static int partition(int arr[], int left, int right){
int i = left, j = right;
int temp;
int pivot = arr[(left + right) / 2];
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
@gtke
gtke / bst
Created January 28, 2013 16:14
Binary Search Tree - JAVA implementation
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
/**
* BST
* @author gtkesh
*/
public class BST<T extends Comparable<T>> {
@gtke
gtke / BST add()
Last active December 11, 2015 20:39
BST Insertion
/**
* Adds a data entry to the BST
*
* null is positive infinity
*
* @param data The data entry to add
*/
public void add(T data) {
root = add(data, root);
}
@gtke
gtke / BST search()
Last active December 11, 2015 20:48
BST Searching
@gtke
gtke / in-order
Created January 28, 2013 18:52
BST Traversal
/**
* Finds the in-order traversal of the BST
*
* @return A list of the data set in the BST in in-order
*/
public List<T> inOrder() {
List<T> list = new ArrayList<T>();
inOrder(root, list);
return list;
}
@gtke
gtke / remove
Created January 28, 2013 19:42
BST Deletion
/**
* Removes a data entry from the BST
*
* null is positive infinity
*
* @param data The data entry to be removed
* @return The removed data entry (null if nothing is removed)
*
*/
public T remove(T data) {
@gtke
gtke / contains
Created January 28, 2013 19:49
BST Contains
/**
* Checks if the BST contains a data entry
*
* null is positive infinity
*
* @param data The data entry to be checked
* @return If the data entry is in the BST
*/
public boolean contains(T data) {
if(root == null){