12/29/2012

String Reverse using XOR

Better implementation of string reverse is by using XOR operation. This eliminates swap operation. See how it works:        public static final String reverseWithXOR(String string) {         char[] array = string.toCharArray();      ...

12/21/2012

Binary Search algorithm

This implementation of Binary Search algorithm is aim to be one of the most simplest. The elements are in sorted array. If the search element (item) value is less than the middle value - > take the left part and apply the algorithm on it. If the search element (item) value is bigger than the middle value - > take the right part and apply the algorithm on it. If the search element is the middle...

Implementation of string replace method in JAVA

The idea is to create a custom implementation of the well known method in java, which replaces substring of a string with other string. Example: String str1 = "Lyubomir"; System.out.println(str1.replace("Ly", "fiko")); Result: fikoubomir The method will be implemented using arrays or characters, although Strings can be used as well. This will be much simple, but the idea here is to...

12/14/2012

Majority Vote Algorithm

Question: How we can determine which element in an array is majority element? (A majority element is the one which occurs most of the time compared with the other elements) For example in the string: ABCAGRADART - A is the majority element. A naive solution of this problem will be to traverse the string (the array) and count the number of occurrences of every character. - In that case we...