Skip to content

Instantly share code, notes, and snippets.

@gabhi
gabhi / gist:11264220
Created April 24, 2014 18:15
Quick Sort
public static void quickSort(int[] a, int p, int r)
{
if(p<r)
{
int q=partition(a,p,r);
quickSort(a,p,q);
quickSort(a,q+1,r);
}
}
@gabhi
gabhi / gist:11264273
Last active August 29, 2015 14:00
Merge Sort
public static int[] mergeSort(int [] list) {
if (list.length <= 1) {
return list;
}
// Split the array in half
int[] first = new int[list.length / 2];
int[] second = new int[list.length - first.length];
System.arraycopy(list, 0, first, 0, first.length);
System.arraycopy(list, first.length, second, 0, second.length);
@gabhi
gabhi / gist:11264547
Created April 24, 2014 18:27
String permutations
public static void main(String[] args) {
String input = "abcd";
permute("", input);
}
public static void permute(String prefix, String input) {
int m = prefix.length();
if (m >= 1) {
@gabhi
gabhi / gist:11265636
Created April 24, 2014 19:01
Leaders in Array
/**
* Write a program to print all the LEADERS in the array.
* An element is leader if it is greater than all the elements to its right side.
* And the rightmost element is always a leader. For example int the array {16, 17, 4, 3, 5, 2}, leaders are 17, 5 and 2.
*/
void printLeaders(int arr[], int size)
{
int max_from_right = arr[size-1];
int i;
@gabhi
gabhi / gist:11338577
Created April 27, 2014 05:58
Hashtable implementation using arrays
public class HashEntry {
private int key;
private int value;
HashEntry(int key, int value) {
this.key = key;
this.value = value;
}
public int getKey() {
@gabhi
gabhi / gist:11338700
Created April 27, 2014 06:08
Flattern linked list
import java.util.LinkedList;
import java.util.List;
public final class FlattenUtil {
public static List<Object> flatten(List<?> list) {
List<Object> retVal = new LinkedList<Object>();
flatten(list, retVal);
return retVal;
@gabhi
gabhi / gist:11338746
Created April 27, 2014 06:11
Find LCA lowest common ancestor
public Node findLca(Node node, int t1, int t2) {
if(node == null) {
return null;
}
if(node.value > t2 && node.value > t1) {
// both targets are left
return findLca(node.left, t1, t2);
} else if (node.value < t2 && node.value < t1) {
// both targets are right
return findLca(node.right, t1, t2);
@gabhi
gabhi / gist:11363820
Last active August 1, 2018 19:58
Detect and remove cycle in linked list
Node slow, fast, start;
fast = slow = head;
//PART I - Detect if a loop exists
while (true)
{
// fast will always fall off the end of the list if it is linear
if (fast == null || fast.next == null)
{
// no loop
@gabhi
gabhi / gist:11364028
Created April 28, 2014 07:19
left sibling right child tree
// Sets the nextRight of root and calls connectRecur() for other nodes
void connect (struct node *p)
{
// Set the nextRight for root
p->nextRight = NULL;
// Set the next right for rest of the nodes (other than root)
connectRecur(p);
}
@gabhi
gabhi / gist:11364603
Created April 28, 2014 07:46
longest common palindrom
// This function prints the longest palindrome substring of str[].
// It also returns the length of the longest palindrome
int longestPalSubstr( char *str )
{
int n = strlen( str ); // get length of input string
// table[i][j] will be false if substring str[i..j] is not palindrome.
// Else table[i][j] will be true
bool table[n][n];
memset( table, 0, sizeof( table ) );