Skip to content

Instantly share code, notes, and snippets.

@KodeSeeker
KodeSeeker / MedianofABSTAlgo.txt
Created April 24, 2013 20:44
Median of a BST.
Approach 1:
If tree balanced then the root is the median !
Approach 2:
Store in order traversal of the tree in an array and find the median of the array
Approach 3:
Do an inorder traversal to find out the number of elements (say n)
Do an inorder traversal again for n/2 times
@KodeSeeker
KodeSeeker / ReverseANumberRecursively.java
Created April 25, 2013 16:55
Reverse a number recursively
inf f(int num, int car=0)
{
car *=10;
if((num/10) == 0)
return ( car + num );
else
return f(num/10, car+(num%10) );
}
@KodeSeeker
KodeSeeker / AnagramCheck.java
Created April 25, 2013 22:15
Check if two strings are anagrams. Hashmap solution O(n).
public static boolean isAnagram(String s1, String s2)
{
boolean result = false;
//Basic check for the length
if(s1.length()!=s2.length())
return result;
char c1[]= s1.toLowerCase().toCharArray();
char c2[]= s2.toLowerCase().toCharArray();
HashMap<Character,Integer> h = new HashMap<Character,Integer>();
@KodeSeeker
KodeSeeker / SpellChecker.java
Created April 25, 2013 22:21
Spell Checker in Java- not reviewed
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// References
// - http://norvig.com/spell-correct.html
// - http://raelcunha.com/spell-correct.php
@KodeSeeker
KodeSeeker / ReverseLinkedList.java
Created April 25, 2013 22:32
Reverse a singly linkedList- recursive
public Node reverse(Node previous, Node current) {
if(previous == null)
return null;
if(previous.equals(head))
previous.setNext(null);
if(current == null) { // end of list
head = previous;
return head;
} else {
Node temp = current.getNext();
@KodeSeeker
KodeSeeker / DetectDeadlock.java
Created April 25, 2013 23:04
Programmatically detect deadlocks in Java
//References-
//1-http://meteatamel.wordpress.com/2012/03/21/deadlock-detection-in-java/
//2-http://stackoverflow.com/questions/1102359/programmatic-deadlock-detection-in-java
private static void detectDeadlock() {
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
long[] threadIds = threadBean.findMonitorDeadlockedThreads();
int deadlockedThreads = threadIds != null? threadIds.length : 0;
System.out.println("Number of deadlocked threads: " + deadlockedThreads);
@KodeSeeker
KodeSeeker / ConvertHextoInt.java
Created April 25, 2013 23:32
Convert hexadecimal string to integer
int i= Integer.parseInt(hexStringinput,16);
@KodeSeeker
KodeSeeker / QuickSort.java
Created April 26, 2013 21:41
Quick Sort implmentation
public void quickSort(int array[])
// pre: array is full, all elements are non-null integers
// post: the array is sorted in ascending order
{
quickSort(array, 0, array.length - 1); // quicksort all the elements in the array
}
public void quickSort(int array[], int start, int end)
{
@KodeSeeker
KodeSeeker / CountSort.java
Created April 26, 2013 21:48
Counting Sort Implementation
/*Suppose we have an input array a[] consisting of n integers, each in the range 0 to k-1.
The counting-sort algorithm sorts using an auxiliary array of counters. It outputs a sorted version of a[]
as an auxiliary array b[] .The idea behind counting-sort is simple: For each i on 0 to k-1 , count the number of occurrences
of i in a[] and store this in c[i] . Now, after sorting, the output will look likec[0] occurrences of 0, followed by c[1] occurrences of 1, followed by c[2] occurrences of 2,...
, followed by c[k-1] occurrences of k-1 .
*/
int[] countingSort(int[] a, int k) {
@KodeSeeker
KodeSeeker / HeapSort.java
Last active December 16, 2015 17:29
Heap Sort in Java
//Source code for Heap Sort
public class HeapSort
{
private static int[] a;
private static int n;
private static int left;
private static int right;
private static int largest;
public static void buildheap(int []a){