<?xml version="1.0" encoding="UTF-8" standalone="no"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:gd="http://schemas.google.com/g/2005" xmlns:georss="http://www.georss.org/georss" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-1629739459234463496</atom:id><lastBuildDate>Fri, 29 May 2026 11:42:23 +0000</lastBuildDate><category>Java</category><category>Algorithms</category><category>cplusplus</category><category>Qt</category><category>Recursion</category><category>Generics</category><category>Data Structures</category><category>Binary Tree</category><category>Binary search algorithm</category><category>QGraphicsScene</category><category>Ubuntu</category><category>Boost</category><category>Pointer</category><category>QGraphicsView</category><category>QDialog</category><category>QGraphicsItem</category><category>J2ME</category><category>PL/SQL</category><category>QString</category><category>Serialization</category><category>Apache POI</category><category>Header Files</category><category>Maven2</category><category>PostgreSQL</category><category>QDataStream</category><category>QSqlDatabase</category><category>QSqlQuery</category><category>QtSql</category><category>RpcGen</category><category>Ruby</category><category>Ruby on Rails</category><category>Software Engineering</category><category>socket</category><category>.Net Framework</category><category>C#</category><category>Commons Net FtpClient</category><category>Dictionary</category><category>Dynamic-link library</category><category>Enumeration</category><category>Exceptions</category><category>Hibernate</category><category>Junit</category><category>Logger</category><category>MySQL</category><category>QDebug</category><category>QDir</category><category>QFileDialog</category><category>QImage</category><category>QLayout</category><category>QLibrary</category><category>QMutex</category><category>QRegExp</category><category>QSqlError</category><category>QStringList</category><category>QTest</category><category>QVariant</category><category>QXMLStreamWriter</category><category>Regex</category><category>Run-time type information</category><category>Unicode</category><category>Visual Studio IDE</category><title>Tufan Görel</title><description></description><link>http://tufangorel.blogspot.com/</link><managingEditor>noreply@blogger.com (tufang)</managingEditor><generator>Blogger</generator><openSearch:totalResults>108</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><xhtml:meta content="noindex" name="robots" xmlns:xhtml="http://www.w3.org/1999/xhtml"/><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-7497700838115401146</guid><pubDate>Mon, 14 Mar 2016 22:30:00 +0000</pubDate><atom:updated>2016-03-15T00:36:13.566+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Data Structures</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Array of HashMaps in Java</title><description>&lt;a href="https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html" target="_blank"&gt;HashMap&lt;/a&gt; in Java is one of the common data structures used to store key-value pairs. HashMap can also be used as items of an ordinary array.
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package util;

import java.util.HashMap;
import java.util.Map;

public class ArrayUtility {


    @SuppressWarnings("unchecked")
    public static HashMap&amp;lt;String,Object&amp;gt;[] createAndFillHashMapArray( final int size )
    {
        HashMap&amp;lt;String, Object&amp;gt;[] hashMapArray = (HashMap&amp;lt;String, Object&amp;gt;[])new HashMap[size];
        int i = 0;
        for (HashMap&amp;lt;String, Object&amp;gt; hashMap : hashMapArray) {
             hashMap = new HashMap&amp;lt;String, Object&amp;gt;();
             hashMap.put("key"+i, "value"); 
             hashMapArray[i] = hashMap;
             i++;
        }
        return hashMapArray;
    }
 
    public static void displayHashMapArrayContent( HashMap&amp;lt;String, Object&amp;gt;[] hashMapArray )
    {
        if( hashMapArray != null &amp;amp;&amp;amp; hashMapArray.length &amp;gt; 0 )
        {
           for (HashMap&amp;lt;String, Object&amp;gt; hashMap : hashMapArray) {
              for (Map.Entry&amp;lt;String, Object&amp;gt; entry : hashMap.entrySet()) {
                  String key = entry.getKey();
                  Object value = entry.getValue();
                  System.out.println(key+" - "+value);
               }
           }
         }
    }
 
    public static void main(String[] args) {
  
         HashMap&amp;lt;String, Object&amp;gt;[] hashMapArray = createAndFillHashMapArray(5);
         displayHashMapArrayContent(hashMapArray);
    }
}

&lt;/pre&gt;
&lt;br /&gt;
createAndFillHashMapArray method takes the size of the array and fills it with HashMaps.&lt;br /&gt;
displayHashMapArrayContent method takes the hashMap array and displays its content as follows :&lt;br /&gt;
&lt;br /&gt;
key0 - value &lt;br /&gt;
key1 - value &lt;br /&gt;
key2 - value &lt;br /&gt;
key3 - value &lt;br /&gt;
key4 - value </description><link>http://tufangorel.blogspot.com/2016/03/array-of-hashmaps-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-3716274493564804113</guid><pubDate>Thu, 12 Nov 2015 16:21:00 +0000</pubDate><atom:updated>2015-11-12T18:28:25.493+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Recursion</category><title>Maximum Contiguous Subsequence Sum in Linearithmic Time Recursively in Java</title><description>There are different solutions to maximum subsequence sum problem with varying complexities such as &lt;a href="http://tufangorel.blogspot.com/2015/11/maximum-contiguous-subsequence-sum-in-linear-time-in-java.html" target="_blank"&gt;linear&lt;/a&gt;, &lt;a href="http://tufangorel.blogspot.com/2015/11/maximum-contiguous-subsequence-sum-in-quadratic-time-in-java.html" target="_blank"&gt;quadratic&lt;/a&gt; and &lt;a href="http://tufangorel.blogspot.com/2015/11/maximum-contiguous-subsequence-sum-in-cubic-time-in-java.html" target="_blank"&gt;cubic&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
There is also a linearithmic solution for the maximum subsequence sum problem implemented in Java.&lt;br /&gt;
&lt;br /&gt;
This linearithmic &lt;a href="http://www.geeksforgeeks.org/divide-and-conquer-maximum-sum-subarray/" target="_blank"&gt;solution &lt;/a&gt;is achieved by applying a &lt;a href="https://en.wikipedia.org/wiki/Divide_and_conquer_algorithms" target="_blank"&gt;divide-and-conquer algorithm design strategy&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Algorithm divides the input array into two different parts from the middle and then searches the max sum&lt;br /&gt;
1-) in the first half&lt;br /&gt;
2-) in the second half&lt;br /&gt;
3-) in the sub array that crosses the mid point of the array&lt;br /&gt;
&lt;br /&gt;
1st and 2nd cases are achieved with recursive calls.&lt;br /&gt;
&lt;br /&gt;
3rd case is achieved by as &lt;a href="http://www.amazon.com/Data-Structures-Algorithm-Analysis-Edition/dp/0132576279" target="_blank"&gt;described at&lt;/a&gt; :&lt;br /&gt;
&lt;br /&gt;
"by finding the largest sum in the first half that includes the last element in the first half, and the largest sum in the second half that includes the first element in the second half."&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;

package basics;

public class MaxSubSumLinearithmic {

 public static int maxSubSum(int[] inArr) {
  return maxSubSum( inArr, 0, inArr.length-1 );
 }

 public static int maxSubSum(int[] inArr, int left, int right) {

  if (left == right)
   if (inArr[left] &amp;gt; 0)
    return inArr[left];
   else
    return 0;

  int mid = (left + right) / 2;
  int maxLSum = maxSubSum( inArr, left, mid);
  int maxRSum = maxSubSum( inArr, mid+1, right);

  int maxLBorderSum = 0, leftBorderSum = 0;
  for (int i = mid; i &amp;gt;= left; i--) {
   leftBorderSum += inArr[i];
   if ( leftBorderSum &amp;gt; maxLBorderSum )
    maxLBorderSum = leftBorderSum;
  }

  int maxRBorderSum = 0, rightBorderSum = 0;
  for (int i = mid + 1; i &amp;lt;= right; i++) {
   rightBorderSum += inArr[i];
   if ( rightBorderSum &amp;gt; maxRBorderSum )
    maxRBorderSum = rightBorderSum;
  }

  return getMax3( maxLSum, maxRSum, maxLBorderSum+maxRBorderSum );
 }
 
 private static int getMax3( int a, int b, int c )
 {
      return a &amp;gt; b ? a &amp;gt; c ? a : c : b &amp;gt; c ? b : c;
 }

 public static void main(String[] args) {

  int input[] = { -2, -3, 4, -1, -2, 1, 5, -3 };

  System.out.println("Max Sub Array Sum Result = " + maxSubSum(input));

  input = new int[] { -2, 3, -16, 100, -4, 5 };

  System.out.println("Max Sub Array Sum Result = " + maxSubSum(input));

  input = new int[] { -2, 11, -4, 13, -5, 2 };

  System.out.println("Max Sub Array Sum Result = " + maxSubSum(input));

  input = new int[] { -15, -23, -476, -3, -292 };

  System.out.println("Max Sub Array Sum Result = " + maxSubSum(input));
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a MaxSubSumLinearithmic.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the MaxSubSumLinearithmic class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Max Sub Array Sum Result = 7 &lt;br /&gt;
Max Sub Array Sum Result = 101 &lt;br /&gt;
Max Sub Array Sum Result = 20 &lt;br /&gt;
Max Sub Array Sum Result = 0 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References : 
&lt;br /&gt;&lt;br /&gt;
&lt;div style="background-color: white; box-sizing: border-box; color: #111111; font-family: Arial, sans-serif; line-height: 1.2; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding: 0px; text-rendering: optimizeLegibility;"&gt;
&lt;span class="a-size-extra-large" id="productTitle" style="box-sizing: border-box; font-weight: normal; line-height: 1.2 !important; text-rendering: optimizeLegibility;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;a href="http://www.amazon.com/Data-Structures-Algorithm-Analysis-Edition/dp/0132576279" target="_blank"&gt;Data Structures and Algorithm Analysis in Java (3rd Edition)&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;a href="http://www.geeksforgeeks.org/divide-and-conquer-maximum-sum-subarray/"&gt;Divide and Conquer Solution to Max Sub Sum&lt;/a&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;a href="http://www.java-tips.org/java-se-tips-100019/24-java-lang/1891-finding-maximum-contiguous-subsequence-sum-using-divide-and-conquer-approach.html"&gt;Finding-maximum-contiguous-subsequence-sum-using-divide-and-conquer-approach&lt;/a&gt;
&lt;br /&gt;&lt;br /&gt;
&lt;a href="https://en.wikipedia.org/wiki/Maximum_subarray_problem"&gt;Max Sub Array Problem Definition from Wikipedia&lt;/a&gt;
&lt;br /&gt;&lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/11/maximum-contiguous-subsequence-sum-in-linearithmic-time-recursively-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-2977734917504063433</guid><pubDate>Thu, 12 Nov 2015 15:30:00 +0000</pubDate><atom:updated>2015-11-12T17:30:29.934+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Finding Maximum of Three Integers in Java</title><description>Finding maximum of three integers problem helps to understand if-else branching and &lt;a href="https://en.wikipedia.org/wiki/Conditional_(computer_programming)" target="_blank"&gt;conditionals &lt;/a&gt;topic in a better way.&lt;br /&gt;
&lt;br /&gt;
Following is the implementation of the find maximum of three integers in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;

package basics;

public class ThreeComparator {

 public static int max3( int a, int b, int c )
 {
  if( a&amp;gt;b )
  {
   if( a &amp;gt; c )
       return a;
   else
       return c;
  }
  else if( b&amp;gt;c )
  {
                 return b;
  }
  else
  {
   return c;
  }
  
 }
 
 public static void main(String[] args) {
  
  int a = 4;
  int b = 3;
  int c = 5;
  
  System.out.println("Maximum of "+a+" , "+b+" and "+c+" is = "+max3( a,b,c ));
  System.out.println("Maximum of "+a+" , "+c+" and "+b+" is = "+max3( a,c,b ));
  System.out.println("Maximum of "+b+" , "+a+" and "+c+" is = "+max3( b,a,c ));
  System.out.println("Maximum of "+b+" , "+c+" and "+a+" is = "+max3( b,c,a ));
  System.out.println("Maximum of "+c+" , "+a+" and "+b+" is = "+max3( c,a,b ));
  System.out.println("Maximum of "+c+" , "+b+" and "+a+" is = "+max3( c,b,a ));
  
  System.out.println("Maximum of 4 , 5 and 4 is = "+max3( 4,5,4 ));
  System.out.println("Maximum of 41 , 41 and 41 is = "+max3( 41,41,41 ));
  System.out.println("Maximum of 11 , 21 and 31 is = "+max3( 11,21,31 ));  
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a ThreeComparator.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the ThreeComparator class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Maximum of 4 , 3 and 5 is = 5 &lt;br /&gt;
Maximum of 4 , 5 and 3 is = 5 &lt;br /&gt;
Maximum of 3 , 4 and 5 is = 5 &lt;br /&gt;
Maximum of 3 , 5 and 4 is = 5 &lt;br /&gt;
Maximum of 5 , 4 and 3 is = 5 &lt;br /&gt;
Maximum of 5 , 3 and 4 is = 5 &lt;br /&gt;
Maximum of 4 , 5 and 4 is = 5 &lt;br /&gt;
Maximum of 41 , 41 and 41 is = 41 &lt;br /&gt;
Maximum of 11 , 21 and 31 is = 31 &lt;br /&gt;
&lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/11/finding-maximum-of-three-integers-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-7482414311417510161</guid><pubDate>Thu, 12 Nov 2015 13:45:00 +0000</pubDate><atom:updated>2015-11-12T18:29:22.389+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Recursion</category><title>Power of a Number Implementation Recursive in Java</title><description>Calculation of &lt;a href="https://en.wikipedia.org/wiki/Exponentiation" target="_blank"&gt;exponentiation &lt;/a&gt;returns the value of the base number a raised to the exponent n.&lt;br /&gt;
&lt;br /&gt;
Following is the recursive implementation of the power method in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class PowerCalculator {

 public static long pow(long base, int n) {
  
  if (n == 0)
      return 1;
  
  if (n == 1)
      return base;
  
  if (n%2==0)
      return pow( base*base, n/2 );
  else
      return pow( base*base, n/2 )*base;
 }

 public static void main(String[] args) {
  
  long base = 2;
  int exponent = 4;
  System.out.println("Result of "+base+" to the "+exponent+" is = "+pow( base, exponent ));
  
  base = 3;
  exponent = 5;
  System.out.println("Result of "+base+" to the "+exponent+" is = "+pow( base, exponent ));
  
  base = 5;
  exponent = 4;
  System.out.println("Result of "+base+" to the "+exponent+" is = "+pow( base, exponent ));
  
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a PowerCalculator.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the PowerCalculator class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Result of 2 to the 4 is = 16 &lt;br /&gt;
Result of 3 to the 5 is = 243 &lt;br /&gt;
Result of 5 to the 4 is = 625 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References : 
&lt;br /&gt;&lt;br /&gt;
&lt;div style="background-color: white; box-sizing: border-box; color: #111111; font-family: Arial, sans-serif; line-height: 1.2; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding: 0px; text-rendering: optimizeLegibility;"&gt;
&lt;span class="a-size-extra-large" id="productTitle" style="box-sizing: border-box; font-weight: normal; line-height: 1.2 !important; text-rendering: optimizeLegibility;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;a href="http://www.amazon.com/Data-Structures-Algorithm-Analysis-Edition/dp/0132576279" target="_blank"&gt;Data Structures and Algorithm Analysis in Java (3rd Edition)&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a href="https://en.wikipedia.org/wiki/Exponentiation"&gt;Exponentiation in Wiki&lt;/a&gt;
&lt;br /&gt; &lt;br /&gt;
&lt;a href="http://www.tutorialspoint.com/java/lang/math_pow.htm"&gt;Math pow method&lt;/a&gt;
&lt;br /&gt; &lt;br /&gt;
&lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#pow(double,%20double)"&gt;Java 7 Math.pow implementation&lt;/a&gt;
&lt;br /&gt; &lt;br /&gt;
&lt;a href="http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#pow%28int%29"&gt;For big numbers you can use Java-BigInteger&lt;/a&gt;
&lt;br /&gt;&lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/11/power-of-a-number-implementation-recursive-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-4791172514969360487</guid><pubDate>Thu, 12 Nov 2015 13:26:00 +0000</pubDate><atom:updated>2015-11-12T18:29:30.821+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Recursion</category><title>Greatest Common Divisor GCD Implementation Recursive and Iterative in Java</title><description>&lt;a href="https://en.wikipedia.org/wiki/Greatest_common_divisor" target="_blank"&gt;The greatest common divisor&lt;/a&gt; (gcd) of two integers is the largest positive integer that divides the numbers without a remainder.&lt;br /&gt;
&lt;br /&gt;
For example, the GCD of 8 and 12 is 4.
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class GCD {

 public static long gcdRecursive(long a, long b) {
  
  if (a == 0)
   return b;
  if (b == 0)
   return a;
  if (a &amp;gt; b)
   return gcdRecursive(b, a % b);
  
  return gcdRecursive(a, b % a);
 }

 public static long gcdIterative(long m, long n) {
  
  while ( m!=0 &amp;amp;&amp;amp; n != 0 ) {
   long remainder = m % n;
   m = n;
   n = remainder;
  }
  return m+n;
 }
 
 public static void main(String[] args) {
  
  System.out.println("Recursive GCD of 50 and 0 = "+gcdRecursive(50,0));
  System.out.println("Recursive GCD of 0 and 50 = "+gcdRecursive(0,50));
  System.out.println("Recursive GCD of 50 and 10 = "+gcdRecursive(50,10));
  System.out.println("Recursive GCD of 40 and 9 = "+gcdRecursive(40,9));
  System.out.println("Recursive GCD of 8 and 12 = "+gcdRecursive(8,12));
  System.out.println();
  
  System.out.println("Iterative GCD of 50 and 0 = "+gcdIterative(50,0));
  System.out.println("Iterative GCD of 0 and 50 = "+gcdIterative(0,50));
  System.out.println("Iterative GCD of 50 and 10 = "+gcdIterative(50,10));
  System.out.println("Iterative GCD of 40 and 9 = "+gcdIterative(40,9));
  System.out.println("Iterative GCD of 8 and 12 = "+gcdRecursive(8,12));
 }

}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a GCD.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the GCD class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Recursive GCD of 50 and 0 = 50 &lt;br /&gt;
Recursive GCD of 0 and 50 = 50 &lt;br /&gt;
Recursive GCD of 50 and 10 = 10 &lt;br /&gt;
Recursive GCD of 40 and 9 = 1 &lt;br /&gt;
Recursive GCD of 8 and 12 = 4 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Iterative GCD of 50 and 0 = 50 &lt;br /&gt;
Iterative GCD of 0 and 50 = 50 &lt;br /&gt;
Iterative GCD of 50 and 10 = 10 &lt;br /&gt;
Iterative GCD of 40 and 9 = 1 &lt;br /&gt;
Iterative GCD of 8 and 12 = 4 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References : 
&lt;br /&gt;&lt;br /&gt;
&lt;div style="background-color: white; box-sizing: border-box; color: #111111; font-family: Arial, sans-serif; line-height: 1.2; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding: 0px; text-rendering: optimizeLegibility;"&gt;
&lt;span class="a-size-extra-large" id="productTitle" style="box-sizing: border-box; font-weight: normal; line-height: 1.2 !important; text-rendering: optimizeLegibility;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;a href="http://www.amazon.com/Data-Structures-Algorithm-Analysis-Edition/dp/0132576279" target="_blank"&gt;Data Structures and Algorithm Analysis in Java (3rd Edition)&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a href="https://en.wikipedia.org/wiki/Greatest_common_divisor"&gt;Wikipedia = Greatest Common Divisor&lt;/a&gt;</description><link>http://tufangorel.blogspot.com/2015/11/greatest-common-divisor-gcd-implementation-recursive-and-iterative-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-7240672432033518382</guid><pubDate>Wed, 11 Nov 2015 16:51:00 +0000</pubDate><atom:updated>2015-11-24T11:47:23.664+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Maximum Contiguous Subsequence Sum in Quadratic Time in Java</title><description>Finding the&amp;nbsp;&lt;a href="https://en.wikipedia.org/wiki/Maximum_subarray_problem" target="_blank"&gt;maximum contiguous subsequence sum&lt;/a&gt;&amp;nbsp;of an array of integers among other subsequences is one of the basic problems about operations on arrays, indices and complexity analysis.&lt;br /&gt;
&lt;br /&gt;
For the array {-2, -3, 4, -1, -2, 1, 5, -3} &amp;nbsp; the maximum subsequence sum contains elements&lt;br /&gt;
"4, -1, -2, 1, 5" so the sum = 4+(-1)+(-2)+1+5 = 7.&lt;br /&gt;
&lt;br /&gt;
Using two for loops gives the solution so it is a quadratic&amp;nbsp;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;O&lt;/i&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;(&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;n&lt;/i&gt;&lt;sup style="background-color: #f9f9f9; font-family: sans-serif; font-size: 11.2px; line-height: 1;"&gt;2&lt;/sup&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;)&lt;/span&gt;&amp;nbsp;solution to maximum contiguous subsequence sum in quadratic time in Java.&lt;br /&gt;
&lt;br /&gt;
Removing one of the extra for loops also helps to get rid of expensive operations so the quadratic time becomes preferable to cubic time solution.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class MaxSubSumQuadratic {

 public static int maxSubSum(int[] inArr) {

  int maxSum = 0;

  for (int i = 0; i &amp;lt; inArr.length; i++) {
   
   int thisSum = 0;
   
   for (int j = i; j &amp;lt; inArr.length; j++) {
   
    thisSum += inArr[j];

    if (thisSum &amp;gt; maxSum)
     maxSum = thisSum;
    
   }   
  }

  return maxSum;
 }

 public static void main(String[] args) {

  int input[] = { -2, -3, 4, -1, -2, 1, 5, -3 };

  System.out.println("Max Sub Sum Result = " + maxSubSum(input));

  input = new int[] { -2, 3, -16, 100, -4, 5 };

  System.out.println("Max Sub Sum Result = " + maxSubSum(input));

  input = new int[] { -2, 11, -4, 13, -5, 2 };

  System.out.println("Max Sub Sum Result = " + maxSubSum(input));

  input = new int[] { -15, -23, -476, -3, -292 };

  System.out.println("Max Sub Sum Result = " + maxSubSum(input));
 }

}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a MaxSubSumQuadratic.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the MaxSubSumQuadratic class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Max Sub Sum Result = 7 &lt;br /&gt;
Max Sub Sum Result = 101 &lt;br /&gt;
Max Sub Sum Result = 20 &lt;br /&gt;
Max Sub Sum Result = 0 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
References : 
&lt;br /&gt;
&lt;br /&gt;
&lt;div style="background-color: white; box-sizing: border-box; color: #111111; font-family: Arial, sans-serif; line-height: 1.2; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding: 0px; text-rendering: optimizeLegibility;"&gt;
&lt;span class="a-size-extra-large" id="productTitle" style="box-sizing: border-box; font-weight: normal; line-height: 1.2 !important; text-rendering: optimizeLegibility;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;a href="http://www.amazon.com/Data-Structures-Algorithm-Analysis-Edition/dp/0132576279" target="_blank"&gt;Data Structures and Algorithm Analysis in Java (3rd Edition)&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</description><link>http://tufangorel.blogspot.com/2015/11/maximum-contiguous-subsequence-sum-in-quadratic-time-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-3296191915434374136</guid><pubDate>Wed, 11 Nov 2015 16:38:00 +0000</pubDate><atom:updated>2015-11-12T18:29:45.797+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Maximum Contiguous Subsequence Sum in Linear Time in Java</title><description>Finding the&amp;nbsp;&lt;a href="https://en.wikipedia.org/wiki/Maximum_subarray_problem" target="_blank"&gt;maximum contiguous subsequence sum&lt;/a&gt;&amp;nbsp;of an array of integers among other subsequences is one of the basic problems about operations on arrays, indices and complexity analysis.&lt;br /&gt;
&lt;br /&gt;
For the array {-2, -3, 4, -1, -2, 1, 5, -3} &amp;nbsp; the maximum subsequence sum contains elements&lt;br /&gt;
"4, -1, -2, 1, 5" so the sum = 4+(-1)+(-2)+1+5 = 7.&lt;br /&gt;
&lt;br /&gt;
Using one for loop gives the solution so it is a linear&amp;nbsp;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;O&lt;/i&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;(&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;n&lt;/i&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;)&lt;/span&gt;&amp;nbsp;solution to maximum contiguous subsequence sum in linear time in java.&lt;br /&gt;
&lt;br /&gt;
In this solution inside the for loop, if the currently added item leads to a larger result then the previous maximum then this becomes the new maximum.&lt;br /&gt;
&lt;br /&gt;
If the newly added item leads to a negative sum result then the current sum is assigned the "0".&lt;br /&gt;
&lt;br /&gt;
This solution works for the arrays that contain at least one positive integer.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class MaxSubSumLinear {

 public static int maxSubSum(int[] inArr) 
 {  
  int maxSum = 0, currentSum = 0;

  for (int j = 0; j &amp;lt; inArr.length; j++) {
   currentSum += inArr[j];

   if ( currentSum &amp;gt; maxSum)
    maxSum = currentSum;
   else if ( currentSum &amp;lt; 0)
    currentSum = 0;
  }

  return maxSum;
 }

 public static void main(String[] args) {

  int input[] = { -2, -3, 4, -1, -2, 1, 5, -3 };

  System.out.println("Max Sub Sum Result = " + maxSubSum(input));

  input = new int[] { -2, 3, -16, 100, -4, 5 };

  System.out.println("Max Sub Sum Result = " + maxSubSum(input));

  input = new int[] { -2, 11, -4, 13, -5, 2 };

  System.out.println("Max Sub Sum Result = " + maxSubSum(input));

  input = new int[] { -15, -23, -476, -3, -292 };

  System.out.println("Max Sub Sum Result = " + maxSubSum(input));
 }

}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a MaxSubSumLinear.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the MaxSubSumLinear class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Max Sub Sum Result = 7 &lt;br /&gt;
Max Sub Sum Result = 101 &lt;br /&gt;
Max Sub Sum Result = 20 &lt;br /&gt;
Max Sub Sum Result = 0 &lt;br /&gt;
&lt;br /&gt;
References : 
&lt;br /&gt;&lt;br /&gt;
&lt;div style="background-color: white; box-sizing: border-box; color: #111111; font-family: Arial, sans-serif; line-height: 1.2; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding: 0px; text-rendering: optimizeLegibility;"&gt;
&lt;span class="a-size-extra-large" id="productTitle" style="box-sizing: border-box; font-weight: normal; line-height: 1.2 !important; text-rendering: optimizeLegibility;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;a href="http://www.amazon.com/Data-Structures-Algorithm-Analysis-Edition/dp/0132576279" target="_blank"&gt;Data Structures and Algorithm Analysis in Java (3rd Edition)&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</description><link>http://tufangorel.blogspot.com/2015/11/maximum-contiguous-subsequence-sum-in-linear-time-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-4084378211992979658</guid><pubDate>Wed, 11 Nov 2015 16:04:00 +0000</pubDate><atom:updated>2015-11-12T18:29:51.056+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Maximum Contiguous Subsequence Sum in Cubic Time in Java</title><description>Finding the &lt;a href="https://en.wikipedia.org/wiki/Maximum_subarray_problem" target="_blank"&gt;maximum contiguous subsequence sum&lt;/a&gt; of an array of integers among other subsequences is one of the basic problems about operations on arrays, indices and complexity analysis.&lt;br /&gt;
&lt;br /&gt;
For the array {-2, -3, 4, -1, -2, 1, 5, -3} &amp;nbsp; the maximum subsequence sum contains elements&lt;br /&gt;
"4, -1, -2, 1, 5" so the sum = 4+(-1)+(-2)+1+5 = 7.&lt;br /&gt;
&lt;br /&gt;
Three different indexes defined to solve this problem which are i,j and k respectively.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhd8CiM7indyOrtF__OzYxdEJY_W4o2ppe0I_xuuWaOEklMRxWN2BpKdRPhuvL0s3Ge9bc-Fc2XnmMEDbwa91QiI4hGSkvcxon_5iyMkkpzye59vWflCF8TusrepqRjS5TLChioXG9hGqk/s1600/cubic_max_sub_array.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="186" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhd8CiM7indyOrtF__OzYxdEJY_W4o2ppe0I_xuuWaOEklMRxWN2BpKdRPhuvL0s3Ge9bc-Fc2XnmMEDbwa91QiI4hGSkvcxon_5iyMkkpzye59vWflCF8TusrepqRjS5TLChioXG9hGqk/s320/cubic_max_sub_array.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Because of there are 3 for loops used to solve its time complexity is&amp;nbsp;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;O&lt;/i&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;(&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;n&lt;/i&gt;&lt;sup style="background-color: #f9f9f9; font-family: sans-serif; font-size: 11.2px; line-height: 1;"&gt;3&lt;/sup&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 14px; line-height: 22.4px;"&gt;)&lt;/span&gt;&lt;span style="background-color: white; color: #545454; font-family: &amp;quot;arial&amp;quot; , sans-serif; font-size: x-small; line-height: 18.2px;"&gt;.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
This solution works for the arrays that contain at least one positive integer.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class MaxSubSumCubic {

 public static int maxSubSum( int[] inArr )
 {
  int maxSum = 0;
  
  for (int i = 0; i &amp;lt; inArr.length; i++) {
   for( int j=i; j&amp;lt;inArr.length; j++ )
   {
    int currentSum = 0;
    
    for( int k=i; k&amp;lt;= j; k++ )
     currentSum += inArr[k];
    
    if( currentSum &amp;gt; maxSum )
     maxSum = currentSum;
   }
  }
  
  return maxSum;
 }
 
 public static void main(String[] args) {
  
  int input[] = {-2, -3, 4, -1, -2, 1, 5, -3};
  
  System.out.println("Max Sub Sum Result = "+maxSubSum(input));
  
  input = new int[]{ -2, 3, -16, 100, -4, 5 };
  
  System.out.println("Max Sub Sum Result = "+maxSubSum(input));
  
  input = new int[]{ -2, 11, -4, 13, -5, 2 };
  
  System.out.println("Max Sub Sum Result = "+maxSubSum(input));
  
  input = new int[]{ -15, -23, -476, -3, -292};
  
  System.out.println("Max Sub Sum Result = "+maxSubSum(input));
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a MaxSubSumCubic.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the MaxSubSumCubic class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Max Sub Sum Result = 7 &lt;br /&gt;
Max Sub Sum Result = 101 &lt;br /&gt;
Max Sub Sum Result = 20 &lt;br /&gt;
Max Sub Sum Result = 0 &lt;br /&gt;
&lt;br /&gt;
References : 
&lt;br /&gt;&lt;br /&gt;
&lt;div style="background-color: white; box-sizing: border-box; color: #111111; font-family: Arial, sans-serif; line-height: 1.2; margin-bottom: 0px !important; margin-left: 0px; margin-right: 0px; margin-top: 0px; padding: 0px; text-rendering: optimizeLegibility;"&gt;
&lt;span class="a-size-extra-large" id="productTitle" style="box-sizing: border-box; font-weight: normal; line-height: 1.2 !important; text-rendering: optimizeLegibility;"&gt;&lt;span style="font-size: x-small;"&gt;&lt;a href="http://www.amazon.com/Data-Structures-Algorithm-Analysis-Edition/dp/0132576279" target="_blank"&gt;Data Structures and Algorithm Analysis in Java (3rd Edition)&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</description><link>http://tufangorel.blogspot.com/2015/11/maximum-contiguous-subsequence-sum-in-cubic-time-in-java.html</link><author>noreply@blogger.com (tufang)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhd8CiM7indyOrtF__OzYxdEJY_W4o2ppe0I_xuuWaOEklMRxWN2BpKdRPhuvL0s3Ge9bc-Fc2XnmMEDbwa91QiI4hGSkvcxon_5iyMkkpzye59vWflCF8TusrepqRjS5TLChioXG9hGqk/s72-c/cubic_max_sub_array.png" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-8573496860175190774</guid><pubDate>Tue, 10 Nov 2015 14:42:00 +0000</pubDate><atom:updated>2015-11-10T16:46:54.098+02:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Recursion</category><title>Print Digits of A Number in Forward Or Backward Order in Java</title><description>Printing the separate digits of a number is another common example of using &lt;a href="https://en.wikipedia.org/wiki/Modulo_operation" target="_blank"&gt;modulo operator&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Forward order display of separate digits of a number can be achieved with a recursive method and backward order display of separate digits of a number can be achieved with an iterative method in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class PrintDigits {

 public static void printDigitsOfNumberInForwardOrder( int number )
 {
      if( number/10 &amp;gt; 0 )
          printDigitsOfNumberInForwardOrder(number/10);
  
      System.out.print(number%10+" ");
 }
 
 public static void printDigitsOfNumberInReverseOrder( int number )
 {
      while ( number &amp;gt; 0) {
          System.out.print(number%10+" ");
          number /= 10;
      }  
 }
 
 public static void main(String[] args) {
  
      int number = 1234;
      System.out.print("Digits of "+number+" in forward order are = ");
      printDigitsOfNumberInForwardOrder(number);
  
      number = 123429345;
      System.out.print("\nDigits of "+number+" in forward order are = ");
      printDigitsOfNumberInForwardOrder(number);
  
      number = 678123478;
      System.out.print("\nDigits of "+number+" in forward order are = ");
      printDigitsOfNumberInForwardOrder(number);
  
      number = 678123478;
      System.out.print("\nDigits of "+number+" in backward order are = ");
      printDigitsOfNumberInReverseOrder(number);
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a PrintDigits.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the PrintDigits class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Digits of 1234 in forward order are = 1 2 3 4 &lt;br /&gt;
Digits of 123429345 in forward order are = 1 2 3 4 2 9 3 4 5 &lt;br /&gt;
Digits of 678123478 in forward order are = 6 7 8 1 2 3 4 7 8 &lt;br /&gt;
Digits of 678123478 in backward order are = 8 7 4 3 2 1 8 7 6 &lt;br /&gt;
&lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/11/print-digits-of-a-number-in-forward-or-backward-order-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-7974166157391160201</guid><pubDate>Fri, 06 Nov 2015 14:10:00 +0000</pubDate><atom:updated>2015-11-06T17:14:26.068+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Binary search algorithm</category><category domain="http://www.blogger.com/atom/ns#">Generics</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Recursion</category><title>Static Generic Recursive Binary Search Algorithm In Java</title><description>&lt;a href="https://en.wikipedia.org/wiki/Binary_search_algorithm" target="_blank"&gt;Binary Search algorithm&lt;/a&gt; can be implemented recursively in a &lt;a href="https://docs.oracle.com/javase/tutorial/java/generics/" target="_blank"&gt;generic &lt;/a&gt;way in Java.&lt;br /&gt;
&lt;br /&gt;
Generic recursive binary search method accepts input parameters as any item that implements Comparable interface.&lt;br /&gt;
&lt;br /&gt;
Static binary search method was tested with &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html" target="_blank"&gt;Integer&lt;/a&gt;, &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html" target="_blank"&gt;Double &lt;/a&gt;and &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html" target="_blank"&gt;String&lt;/a&gt; type of parameters where each of them individually implements &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html" target="_blank"&gt;Comparable &lt;/a&gt;interface.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

import java.util.Arrays;

public class GenericRecursiveBinarySearch {

  public static &amp;lt;T extends Comparable&amp;lt;T&amp;gt;&amp;gt; int index( T[] items, T item )
  {
      return index( items, item, 0, items.length-1 );
  }
  
  public static &amp;lt;T extends Comparable&amp;lt;T&amp;gt;&amp;gt; int index( T[] items, T key, int low, int high )
  {
      if ( key == null )
          return -1;
   
      if( low &amp;gt; high  )
          return -1;
    
      int mid = low+(high-low)/2;
    
      if( key.compareTo( items[mid] ) &amp;gt; 0 )
          return index(items, key, mid+1, high);
      else if( key.compareTo( items[mid] ) &amp;lt; 0 )
          return index( items, key, low, mid-1 );
      else
          return mid;
  }  

  public static void main(String[] args) {

      Integer[] items = { 22, 55, 66, 11, 32, 56, 67, 89, 95, 10 };

      Arrays.sort(items);
      System.out.print("Sorted Integer Array = ");
      for (Integer item : items) {
           System.out.print(item+" ");
      }
  
      int foundIndex = index(items, Integer.valueOf(22));
      System.out.println("\nInteger Array Contains item 22 at index = " + foundIndex);

      foundIndex = index(items, Integer.valueOf(11));
      System.out.println("Integer Array Contains item 11 at index = " + foundIndex);

      foundIndex = index(items, Integer.valueOf(67));
      System.out.println("Integer Array Contains item 67 at index = " + foundIndex);

      foundIndex = index(items, Integer.valueOf(10));
      System.out.println("Integer Array Contains item 10 at index = " + foundIndex);

      foundIndex = index(items, Integer.valueOf(101));
      System.out.println("Integer Array Contains item 101 at index = " + foundIndex);

      foundIndex = index(items, null);
      System.out.println("Integer Array Contains item null at index = " + foundIndex);

      String[] strItems = { "alk", "abc", "adk", "zyt", "fre", "nhy" };
      Arrays.sort(strItems);

      System.out.print("\nSorted String Array = ");
      for (String item : strItems) {
           System.out.print(item+" ");
      }
  
      foundIndex = index(strItems, "alk");
      System.out.println("\nString Array Contains item alk at index = " + foundIndex);

      foundIndex = index(strItems, "nhy");
      System.out.println("String Array Contains item nhy at index = " + foundIndex);

      foundIndex = index(strItems, "zyt");
      System.out.println("String Array Contains item zyt at index = " + foundIndex);

      foundIndex = index(strItems, "zyts");
      System.out.println("String Array Contains item zyts at index = " + foundIndex);

      foundIndex = index(strItems, "null");
      System.out.println("String Array Contains item null at index = " + foundIndex);

      Double[] dItems = { 11.3, 13.3, 6.0, 9.6, 45.7, 23.2 };
      Arrays.sort(dItems);

      System.out.print("\nSorted Double Array = ");
      for (Double item : dItems) {
           System.out.print(item+" ");
      }
  
      foundIndex = index(dItems, 13.3);
      System.out.println("\nDouble Array Contains item 13.3 at index = " + foundIndex);

      foundIndex = index(dItems, 14.3);
      System.out.println("Double Array Contains item 14.3 at index = " + foundIndex);

      foundIndex = index(dItems, 23.0);
      System.out.println("Double Array Contains item 23.0 at index = " + foundIndex);

 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Binary search works for sorted arrays so before calling binary search on arrays call &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(java.lang.Object[],%20int,%20int)" target="_blank"&gt;Arrays.sort&lt;/a&gt; to sort array items.
&lt;br /&gt;&lt;br /&gt;
Create a GenericRecursiveBinarySearch.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the GenericRecursiveBinarySearch class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Sorted Integer Array = 10 11 22 32 55 56 66 67 89 95 &lt;br /&gt;
Integer Array Contains item 22 at index = 2 &lt;br /&gt;
Integer Array Contains item 11 at index = 1 &lt;br /&gt;
Integer Array Contains item 67 at index = 7 &lt;br /&gt;
Integer Array Contains item 10 at index = 0 &lt;br /&gt;
Integer Array Contains item 101 at index = -1 &lt;br /&gt;
Integer Array Contains item null at index = -1 &lt;br /&gt;
&lt;br /&gt;
Sorted String Array = abc adk alk fre nhy zyt &lt;br /&gt;
String Array Contains item alk at index = 2 &lt;br /&gt;
String Array Contains item nhy at index = 4 &lt;br /&gt;
String Array Contains item zyt at index = 5 &lt;br /&gt;
String Array Contains item zyts at index = -1 &lt;br /&gt;
String Array Contains item null at index = -1 &lt;br /&gt;
&lt;br /&gt;
Sorted Double Array = 6.0 9.6 11.3 13.3 23.2 45.7 &lt;br /&gt;
Double Array Contains item 13.3 at index = 3 &lt;br /&gt;
Double Array Contains item 14.3 at index = -1 &lt;br /&gt;
Double Array Contains item 23.0 at index = -1 &lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/11/static-generic-recursive-binary-search-algorithm-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-6467288912261147524</guid><pubDate>Thu, 05 Nov 2015 16:20:00 +0000</pubDate><atom:updated>2015-11-05T19:24:35.187+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Binary search algorithm</category><category domain="http://www.blogger.com/atom/ns#">Generics</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Static Generic Iterative Binary Search Algorithm In Java</title><description>In order take &lt;a href="https://docs.oracle.com/javase/tutorial/java/generics/why.html" target="_blank"&gt;advantage &lt;/a&gt;of &lt;a href="https://en.wikipedia.org/wiki/Generics_in_Java" target="_blank"&gt;Generics &lt;/a&gt;in Java, &lt;a href="https://en.wikipedia.org/wiki/Binary_search_algorithm" target="_blank"&gt;binary search&lt;/a&gt; method can be implemented generically.&lt;br /&gt;
&lt;br /&gt;
Binary search works for sorted Arrays so &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(byte[])" target="_blank"&gt;Arrays.sort&lt;/a&gt; method is used before using binarySearch method.&lt;br /&gt;
&lt;br /&gt;
Any type that implements &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html" target="_blank"&gt;Comparable &lt;/a&gt;interface is accepted by the generic iterative search method.&lt;br /&gt;
&lt;br /&gt;
Built-in &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html" target="_blank"&gt;String&lt;/a&gt;, &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html" target="_blank"&gt;Integer &lt;/a&gt;and &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html" target="_blank"&gt;Double&lt;/a&gt; classes in Java implement Comparable interface so search method accepts these parameters.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

import java.util.Arrays;

public class GenericIterativeBinarySearch {

 public static &amp;lt;T extends Comparable&amp;lt;T&amp;gt;&amp;gt; boolean search( T[] items, T item ) {

  if (item == null) {
   return false;
  }

  int low = 0;
  int high = items.length - 1;

  while (low &amp;lt;= high) {

   int ix = low + (high - low) / 2;

   if (item.compareTo(items[ix]) &amp;lt; 0) {
    high = ix - 1;
   } else if (item.compareTo(items[ix]) &amp;gt; 0) {
    low = ix + 1;
   } else {
    return true;
   }
  }

  return false;
 }


 public static void main(String[] args) {

  Integer[] items = { 22, 55, 66, 11, 32, 56, 67, 89, 95, 10 };

  Arrays.sort(items);

  boolean found = search(items, Integer.valueOf(22) );
  System.out.println("Integer Array Contains item 22 = "+found);

  found = search(items, Integer.valueOf(11) );
  System.out.println("Integer Array Contains item 11 = "+found);

  found = search(items, Integer.valueOf(67) );
  System.out.println("Integer Array Contains item 67 = "+found);

  found = search(items, Integer.valueOf(10) );
  System.out.println("Integer Array Contains item 10 = "+found);

  found = search(items, Integer.valueOf(101) );
  System.out.println("Integer Array Contains item 101 = "+found);  
  
  found = search(items, null );
  System.out.println("Integer Array Contains item null = "+found); 
  
  String[] strItems = { "alk", "abc", "adk", "zyt", "fre", "nhy" };
  Arrays.sort(strItems);
  
  found = search( strItems, "alk" );
  System.out.println("String Array Contains item alk = "+found);
  
  found = search( strItems, "nhy" );
  System.out.println("String Array Contains item nhy = "+found);
  
  found = search( strItems, "zyt" );
  System.out.println("String Array Contains item zyt = "+found);
  
  found = search( strItems, "zyts" );
  System.out.println("String Array Contains item zyts = "+found);
  
  found = search( strItems, "null" );
  System.out.println("String Array Contains item null = "+found);
  
  Double[] dItems = { 11.3, 13.3, 6.0, 9.6, 45.7, 23.2 };
  Arrays.sort(dItems);
  
  found = search( dItems, 13.3 );
  System.out.println("Double Array Contains item 13.3 = "+found);
  
  found = search( dItems, 14.3 );
  System.out.println("Double Array Contains item 14.3 = "+found);
  
  found = search( dItems, 23.0 );
  System.out.println("Double Array Contains item 23.0 = "+found);  
  
 }
}


&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a GenericIterativeBinarySearch.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the GenericIterativeBinarySearch class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Integer Array Contains item 22 = true &lt;br /&gt;
Integer Array Contains item 11 = true &lt;br /&gt;
Integer Array Contains item 67 = true &lt;br /&gt;
Integer Array Contains item 10 = true &lt;br /&gt;
Integer Array Contains item 101 = false &lt;br /&gt;
Integer Array Contains item null = false &lt;br /&gt;
String Array Contains item alk = true &lt;br /&gt;
String Array Contains item nhy = true &lt;br /&gt;
String Array Contains item zyt = true &lt;br /&gt;
String Array Contains item zyts = false &lt;br /&gt;
String Array Contains item null = false &lt;br /&gt;
Double Array Contains item 13.3 = true &lt;br /&gt;
Double Array Contains item 14.3 = false &lt;br /&gt;
Double Array Contains item 23.0 = false </description><link>http://tufangorel.blogspot.com/2015/11/static-generic-iterative-binary-search-algorithm-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-4421995762137430215</guid><pubDate>Tue, 03 Nov 2015 16:53:00 +0000</pubDate><atom:updated>2015-11-03T19:56:55.988+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Recursion</category><title>Find Permutations of a String Recursively in Java</title><description>In order to print all the &lt;a href="https://en.wikipedia.org/wiki/Permutation" target="_blank"&gt;permutations &lt;/a&gt;of a String a recursive method can be implemented.&lt;br /&gt;
&lt;br /&gt;
In this recursive String permutation solution a&amp;nbsp;&lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/StringBuilder.html" target="_blank"&gt;StringBuilder &lt;/a&gt;instance is used as compared to other common&amp;nbsp;&lt;a href="http://introcs.cs.princeton.edu/java/23recursion/Permutations.java.html" target="_blank"&gt;String permutation&lt;/a&gt; implementations in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class StringUtils {

 public static void permute( String str ) {
  print("", str);
 }

 private static void print(String leftPart, String str ) {

  int sLength = str.length();
  if ( sLength == 0) {
   return;
  } else if ( sLength == 1) {
   System.out.println(leftPart + str);
   return;
  }

  StringBuilder buffer = new StringBuilder(str);

  for (int i = 0; i &amp;lt; sLength; i++) {
   char temp = buffer.charAt(i);
   buffer.setCharAt(i, buffer.charAt(0));
   buffer.setCharAt( 0, temp );
   print( leftPart + temp, buffer.substring(1, sLength) );
  }
  
 }
 
 public static void main(String[] args) {
  
  String val = "abc"; 
  System.out.println("All Permutations of "+val);
  permute(val);  
  val = "abcd";
  System.out.println("All Permutations of "+val);
  permute(val);
  
 }

}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a StringUtils.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the StringUtils class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
All Permutations of abc &lt;br /&gt; 
abc &lt;br /&gt;
acb &lt;br /&gt;
bac &lt;br /&gt;
bca &lt;br /&gt;
cab &lt;br /&gt;
cba &lt;br /&gt; 
All Permutations of abcd &lt;br /&gt;
abcd &lt;br /&gt;
abdc &lt;br /&gt;
acbd &lt;br /&gt;
acdb &lt;br /&gt;
adbc &lt;br /&gt;
adcb &lt;br /&gt;
bacd &lt;br /&gt;
badc &lt;br /&gt;
bcad &lt;br /&gt;
bcda &lt;br /&gt;
bdac &lt;br /&gt;
bdca &lt;br /&gt;
cabd &lt;br /&gt;
cadb &lt;br /&gt;
cbad &lt;br /&gt;
cbda &lt;br /&gt;
cdab &lt;br /&gt;
cdba &lt;br /&gt;
dabc &lt;br /&gt;
dacb &lt;br /&gt;
dbac &lt;br /&gt;
dbca &lt;br /&gt;
dcab &lt;br /&gt;
dcba &lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/11/find-permutations-of-a-string-recursively-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-3179890408861592357</guid><pubDate>Tue, 03 Nov 2015 15:36:00 +0000</pubDate><atom:updated>2015-11-03T18:38:00.550+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Recursion</category><title>Palindrome String Check Recursively in Java</title><description>Palindrome String checking is one of the primitive exercises for String processing char-by-char.&lt;br /&gt;
&lt;br /&gt;
Palindrome String check can also be implemented recursively in Java. This recursive implementation is not efficient as the iterative one because it calls String.substring() subsequently.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;

package basics;

public class StringUtils {

 public static boolean isPalindrome( String str )
 {
  int sLength = str.length();
  if ( sLength &amp;lt; 2) {
   return true;
  } else if ( str.charAt(0) != str.charAt( sLength-1 ) ) {
   return false; 
  } else if ( sLength == 2 ) {
   return true;
  } else { 
   return isPalindrome( str.substring( 1, sLength-1 ));
  }
  
 }
 
 public static void main(String[] args) {
  
  String str = "madam";
  boolean result = isPalindrome(str);
  System.out.println(str+" is Palindrome = "+result);
  
  str = "qqaacc";
  result = isPalindrome(str);
  System.out.println(str+" is Palindrome = "+result);
  
  str = "cocoococ";
  result = isPalindrome(str);
  System.out.println(str+" is Palindrome = "+result);
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a StringUtils.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the StringUtils class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
madam is Palindrome = true &lt;br /&gt;
qqaacc is Palindrome = false &lt;br /&gt;
cocoococ is Palindrome = true </description><link>http://tufangorel.blogspot.com/2015/11/palindrome-string-check-recursively-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-3310881856606741062</guid><pubDate>Tue, 03 Nov 2015 14:54:00 +0000</pubDate><atom:updated>2015-11-03T17:54:19.355+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Recursion</category><title>Find Max Integer From The First N Integers of Unsorted Array Recursively in Java</title><description>&lt;a href="https://en.wikipedia.org/wiki/Binary_search_algorithm" target="_blank"&gt;Searching&lt;/a&gt;&amp;nbsp;a sorted integer array for a specific integer is a general use-case for most of the time.&lt;br /&gt;
&lt;br /&gt;
But if it is required to search an unsorted array for the first N elements in order to find the maximum element then it is required to apply a different solution.&lt;br /&gt;
&lt;br /&gt;
In order to find the maximum of first N elements of an unsorted array recursively following method can be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class NumberUtils {

 public static int getMaxItemFromSubArray( int[] inArray, int index ) {
  
  if( index &amp;lt;= 0 || index&amp;gt;inArray.length )
   return -1;
  
  if ( index == 1 ) {
   return inArray[0]; 
  }
  
  int m = getMaxItemFromSubArray( inArray, index-1 ); 
  
  if ( inArray[ index-1 ] &amp;gt; m) {
   return inArray[ index-1 ];
  } else {
   return m;
  }
  
 }

 public static void main(String[] args) {

  int[] items = { 0, 5, 73, 33, 201, 3, 441 };
  int maxItem = getMaxItemFromSubArray( items , 0 );
  System.out.println("Max Item is = " + maxItem);  
  
  maxItem = getMaxItemFromSubArray( items , 8 );
  System.out.println("Max Item is = " + maxItem);  
  
  maxItem = getMaxItemFromSubArray( items , 3 );
  System.out.println("Max Item is = " + maxItem);  
  
  maxItem = getMaxItemFromSubArray( items , 4 );
  System.out.println("Max Item is = " + maxItem); 
  
  maxItem = getMaxItemFromSubArray( items , 5 );
  System.out.println("Max Item is = " + maxItem); 
  
  maxItem = getMaxItemFromSubArray( items , 6 );
  System.out.println("Max Item is = " + maxItem); 
  
  maxItem = getMaxItemFromSubArray( items , 7 );
  System.out.println("Max Item is = " + maxItem); 
  
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a NumberUtils.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the NumberUtils class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Max Item is = -1 &lt;br /&gt;
Max Item is = -1 &lt;br /&gt;
Max Item is = 73 &lt;br /&gt;
Max Item is = 73 &lt;br /&gt;
Max Item is = 201 &lt;br /&gt;
Max Item is = 201 &lt;br /&gt;
Max Item is = 441 </description><link>http://tufangorel.blogspot.com/2015/11/find-max-integer-from-the-first-n-integers-of-unsorted-array-recursively-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-7681205140427185449</guid><pubDate>Tue, 03 Nov 2015 14:04:00 +0000</pubDate><atom:updated>2015-11-03T17:04:22.290+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Recursion</category><title>Sum of the Squares of the First n Numbers Recursively in Java</title><description>Formula for the sum of the squares of the first n numbers is described with the following equation in mathematics.&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;f(n) =&amp;nbsp;&lt;span style="background-color: white; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 13px; line-height: 16px;"&gt;[n(n+1)(2n+1)]/6&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: white; font-family: &amp;quot;helvetica neue&amp;quot; , &amp;quot;helvetica&amp;quot; , &amp;quot;arial&amp;quot; , sans-serif; font-size: 13px; line-height: 16px;"&gt;&lt;br /&gt;&lt;/span&gt;
In order to get the result for the sum of the first n integers in Java a recursive method can be used.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class NumberUtils {

 public static int sumOfFirstNSquares( int number )
 {
  if( number == 0 )
   return 0;
  
  return sumOfFirstNSquares(number-1)+(number*number);
 }
 
 public static void main(String[] args) {
  
  int sum = sumOfFirstNSquares(5);
  System.out.println("Sum is = "+sum);
  
  sum = sumOfFirstNSquares(7);
  System.out.println("Sum is = "+sum);
  
  sum = sumOfFirstNSquares(11);
  System.out.println("Sum is = "+sum);
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a NumberUtils.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
If you are willing to work with real big numbers in Java then a &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html" target="_blank"&gt;BigInteger&lt;/a&gt; can be a solution.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the NumberUtils class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Sum is = 55 &lt;br /&gt;
Sum is = 140 &lt;br /&gt;
Sum is = 506 </description><link>http://tufangorel.blogspot.com/2015/11/sum-of-the-squares-of-the-first-n-numbers-recursively-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-7576616323902571702</guid><pubDate>Mon, 02 Nov 2015 15:10:00 +0000</pubDate><atom:updated>2015-11-02T18:12:16.344+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Data Structures</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Recursion</category><title>Print Singly Linked List in Reverse Order Recursively in Java</title><description>Arrays have got constant size restriction when they are created so it is expensive to resize them dynamically in an application. Instead of using arrays for resizing dynamic structure requirements, &lt;a href="http://www.algolist.net/Data_structures/Singly-linked_list" target="_blank"&gt;singly linked list&lt;/a&gt; data structure can be used.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="https://en.wikipedia.org/wiki/Linked_list" target="_blank"&gt;Singly linked list&lt;/a&gt; has got a node based structure where each node has got next and data fields.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0hH1jk2orjMsSpghG8h0Xsk0I0xaoE3KgZ4g0QKPgTOrvD9_QMjqnfEb6S1dU2Wx1danKoAN2mIhGJzku-i_KgZYOZ8D5pKDSpgInX5v_3faO9rzFDXOCjY5gocZYn30oDbsPrJ-mcoc/s1600/singly_linked_list.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="141" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0hH1jk2orjMsSpghG8h0Xsk0I0xaoE3KgZ4g0QKPgTOrvD9_QMjqnfEb6S1dU2Wx1danKoAN2mIhGJzku-i_KgZYOZ8D5pKDSpgInX5v_3faO9rzFDXOCjY5gocZYn30oDbsPrJ-mcoc/s320/singly_linked_list.png" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Above is a singly linked list with 1-2-3-4-5 elements.&lt;br /&gt;
&lt;br /&gt;
In order to print data elements of each node of a singly linked list data structure in Java following forwardPrint and reversePrint methods can be used.&lt;br /&gt;
&lt;br /&gt;
reversePrint is a recursive method and assumes that the head node is not null.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class LinkedList {

 static class Node
 {
  Node next;
  int data;
 }

 private Node head;
 
 public LinkedList(Node pHead) {
  head = pHead; 
 } 
 
 public void forwardPrint()
 {
  forwardPrint(head);
 }
 
 private void forwardPrint( Node node )
 { 
  Node current = head;
  while( current!=null )
  {
   System.out.println(current.data);
   current = current.next;
  }
 }
 
 public void reversePrint()
 {
  reversePrint(head);
 }
 
 private void reversePrint( Node node )
 {
  if( node.next != null )
   reversePrint(node.next);
  
  System.out.println(node.data);
 }
 
 public static void main(String[] args) {
  
  Node node1 = new Node();
  node1.data = 1;
  Node node2 = new Node();
  node2.data = 2;
  Node node3 = new Node();
  node3.data = 3;
  Node node4 = new Node();
  node4.data = 4;
  Node node5 = new Node();
  node5.data = 5;
  node1.next = node2;
  node2.next = node3;
  node3.next = node4;
  node4.next = node5;
  node5.next = null;
  
  LinkedList list = new LinkedList(node1);
  System.out.println("Forward Print Linked List = ");
  list.forwardPrint();
  System.out.println("Backward Print Linked List = ");
  list.reversePrint();
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Node objects are created separately and node1 object sent as the head of the linked list.
Create a LinkedList.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the LinkedList class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Forward Print Linked List = &lt;br /&gt;
1&lt;br /&gt;
2&lt;br /&gt;
3&lt;br /&gt;
4&lt;br /&gt;
5&lt;br /&gt;
Backward Print Linked List = &lt;br /&gt;
5&lt;br /&gt;
4&lt;br /&gt;
3&lt;br /&gt;
2&lt;br /&gt;
1</description><link>http://tufangorel.blogspot.com/2015/11/print-singly-linked-list-in-reverse-order-recursively-in-java.html</link><author>noreply@blogger.com (tufang)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" height="72" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0hH1jk2orjMsSpghG8h0Xsk0I0xaoE3KgZ4g0QKPgTOrvD9_QMjqnfEb6S1dU2Wx1danKoAN2mIhGJzku-i_KgZYOZ8D5pKDSpgInX5v_3faO9rzFDXOCjY5gocZYn30oDbsPrJ-mcoc/s72-c/singly_linked_list.png" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-6183324142103445045</guid><pubDate>Tue, 27 Oct 2015 15:16:00 +0000</pubDate><atom:updated>2015-10-27T18:20:19.315+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Reverse int Array Iteratively in Java</title><description>A reverse of a primitive int array can be achieved by swapping elements from the start with the end iteratively.&lt;br /&gt;
&lt;br /&gt;
If the swap operation of the primitive array elements starts from the index 0 then it is going to be enough to iterate till the half of the array.&lt;br /&gt;
&lt;br /&gt;
Following is the in-place where there is no need to create-allocate a new array for reversing primitive array items implementation in Java.&lt;br /&gt;&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class IterativeArrayReverse {

 private static void reverseArray( int[] inArr )
 {
     if ( inArr==null || inArr.length==0 )
         return ;
        
     int len = inArr.length;
     for( int i=0; i&amp;lt;len/2; i++ )
         swap(inArr, i, len-i-1);
 }
 
 private static void swap( int[] inArr, int i, int j )
 {
     int temp = inArr[i];
     inArr[i] = inArr[j];
     inArr[j] = temp;
 }
 
 public static void main(String[] args) {
  
     int[] arr = { 1,2,3,4,5,6 };
     System.out.println("Before Reverse");
     for( int i=0; i&amp;lt;arr.length; i++ )
         System.out.printf("%s ", arr[i]);  
  
     reverseArray( arr );
  
     System.out.println("\nAfter Reverse");
     for( int i=0; i&amp;lt;arr.length; i++ )
         System.out.printf("%s ", arr[i]);
  
     int[] arr2 = {4,3,6,2,7,8,9,5};
     System.out.println("\n\nBefore Reverse");
     for( int i=0; i&amp;lt;arr2.length; i++ )
         System.out.printf("%s ", arr2[i]);  
  
     reverseArray( arr2 );
  
     System.out.println("\nAfter Reverse");
     for( int i=0; i&amp;lt;arr2.length; i++ )
         System.out.printf("%s ", arr2[i]);
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a IterativeArrayReverse.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the IterativeArrayReverse class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Before Reverse &lt;br /&gt;
1 2 3 4 5 6 &lt;br /&gt;
After Reverse &lt;br /&gt;
6 5 4 3 2 1 &lt;br /&gt;
&lt;br /&gt;
Before Reverse &lt;br /&gt;
4 3 6 2 7 8 9 5 &lt;br /&gt;
After Reverse &lt;br /&gt;
5 9 8 7 2 6 3 4 &lt;br /&gt;
&lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/10/reverse-int-array-iteratively-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-51970252154869871</guid><pubDate>Tue, 27 Oct 2015 14:17:00 +0000</pubDate><atom:updated>2015-10-27T17:17:25.607+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Generics</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Generic Method to Print Array Elements in Java</title><description>&lt;a href="https://docs.oracle.com/javase/tutorial/java/generics/why.html" target="_blank"&gt;Generics&lt;/a&gt; in Java enable programmers to provide compile-time type-safety.&lt;br /&gt;
&lt;br /&gt;
There are some main benefits of using generics in Java such as :&lt;br /&gt;
&lt;br /&gt;
1-) Strong type-checking&lt;br /&gt;
2-) Elimination of casts&lt;br /&gt;
3-) Provide a way to develop generic algorithms&lt;br /&gt;
&lt;br /&gt;
Methods can also be designed in a generic fashion in Java, too. Following generic method prints all the elements of an array one-by-one in a for-loop for both wrapper types and custom defined types.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class GenericPrint {

 static class Employee
 {
  String name;
  Employee(String pName)
  {
   name = pName;
  }
  
  public String toString() {
   return "[Employee Name = "+name+" ]";
  }
 }
 
 // Generic method
 public static &amp;lt;T&amp;gt; void print( T[] inParam )
 {
  for( T t: inParam )
  {
   System.out.printf("%s ", t);
  }
 }
 
 
 public static void main(String[] args) {
  
  String[] strArr = {"D1","D2","D3","D4"};  
  print(strArr);
  
  Integer[] intArr = { 1,2,3 };
  System.out.println();
  print(intArr);
  
  Double[] dArr = { 5.5, 6.6, 7.7 };
  System.out.println();
  print(dArr);
    
  Employee e1 = new Employee("Emply1");
  Employee e2 = new Employee("Emply2");
  Employee e3 = new Employee("Emply3");
  
  Employee[] empArr = { e1, e2, e3 };
  
  System.out.println();
  print(empArr);  
 } 
}
&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a GenericPrint.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the GenericPrint class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
D1 D2 D3 D4 &lt;br /&gt;
1 2 3 &lt;br /&gt;
5.5 6.6 7.7 &lt;br /&gt;
[Employee Name = Emply1 ] [Employee Name = Emply2 ] [Employee Name = Emply3 ] &lt;br /&gt;
&lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/10/generic-method-to-print-array-elements-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-8232510317577993852</guid><pubDate>Mon, 19 Oct 2015 17:27:00 +0000</pubDate><atom:updated>2015-10-19T20:28:36.750+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Shell Sort Comparable Array Items in Java</title><description>&lt;a href="https://en.wikipedia.org/wiki/Shellsort" target="_blank"&gt;Shell sort&lt;/a&gt; is considered as an extension of insertion sort algorithm.&lt;br /&gt;
&lt;br /&gt;
Insertion sort algorithm is not suggested for large input sets but shell sort comes with increased speed achievements for insertion sort algorithm.&lt;br /&gt;
&lt;br /&gt;
Shell sort achieves speed improvement by exchanging of input entries that are away from each other and produces partially sorted input data sets.&lt;br /&gt;
&lt;br /&gt;
Space and time complexity values are :&lt;br /&gt;
&lt;br /&gt;
Worst case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;        : &lt;/span&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;О(&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;n&lt;/i&gt;&lt;sup style="background-color: #f9f9f9; font-family: sans-serif; font-size: 9.856px; line-height: 1;"&gt;2&lt;/sup&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;)&lt;/span&gt;&lt;br /&gt;
Best case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;           : &lt;/span&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;О(&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;n&lt;/i&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;&amp;nbsp;log&lt;/span&gt;&lt;sup style="background-color: #f9f9f9; font-family: sans-serif; font-size: 9.856px; line-height: 1;"&gt;2&lt;/sup&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;&amp;nbsp;&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;n&lt;/i&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;)&lt;/span&gt;&lt;br /&gt;
Average case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;     : &lt;/span&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;depends on gap sequence&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Space complexiy : &amp;nbsp;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;О(n) total, O(1) auxiliary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
There is an animation for the shell sort algorithm at :&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif" width="257" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
Following is the Java implementation of the Shell Sort algorithm where the sort method accepts an array of Comparable  items as input parameter.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class ShellSort {

 public static void sort(Comparable[] a) {

  // Sort a[] into increasing order.
  int N = a.length;
  int h = 1;
  while (h &amp;lt; N / 3)
   h = 3 * h + 1;
  
  // h-sort the array.
  while (h &amp;gt;= 1) { 
   for (int i = h; i &amp;lt; N; i++) { 
    for (int j = i; j &amp;gt;= h &amp;amp;&amp;amp; (a[j].compareTo(a[j - h]) &amp;lt; 0); j -= h) {
     Comparable temp = a[j];
     a[j] = a[j - h];
     a[j - h] = temp;
    }
   }
   h = h / 3;
  }

 }

 public static void main(String[] args) {

  String[] strArr = { "S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E" };

  System.out.println("Before Sort = ");
  for (int i = 0; i &amp;lt; strArr.length; i++)
   System.out.print(strArr[i] + " ");
  System.out.println("\n");

  sort(strArr);

  System.out.println("After Sort = ");
  for (int i = 0; i &amp;lt; strArr.length; i++)
   System.out.print(strArr[i] + " ");
  System.out.println();

  Integer[] intArr = { 9, 8, 7, 6, 6, 5, 4, 3, 2, 2, 1 };

  System.out.println();
  System.out.println("Before Sort = ");
  for (int i = 0; i &amp;lt; intArr.length; i++)
   System.out.print(intArr[i] + " ");
  System.out.println("\n");

  sort(intArr);

  System.out.println("After Sort = ");
  for (int i = 0; i &amp;lt; intArr.length; i++)
   System.out.print(intArr[i] + " ");
  System.out.println();
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a ShellSort.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the ShellSort class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Before Sort = &lt;br /&gt;
S O R T E X A M P L E &lt;br /&gt;
&lt;br /&gt;
After Sort = &lt;br /&gt;
A E E L M O P R S T X &lt;br /&gt;
&lt;br /&gt;
Before Sort = &lt;br /&gt;
9 8 7 6 6 5 4 3 2 2 1 &lt;br /&gt;
&lt;br /&gt;
After Sort = &lt;br /&gt;
1 2 2 3 4 5 6 6 7 8 9 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Because sort method accepts an array or Comparable items, so it is safe to send an array of items where each item implements Comparable interface in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html" target="_blank"&gt;Integer &lt;/a&gt;and &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html" target="_blank"&gt;String &lt;/a&gt;classes in Java implement Comparable interface so it is acceptable to send this types as parameters to sort method.</description><link>http://tufangorel.blogspot.com/2015/10/shell-sort-comparable-array-items-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-4779202315595477504</guid><pubDate>Mon, 19 Oct 2015 16:33:00 +0000</pubDate><atom:updated>2015-10-19T19:35:19.502+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Insertion Sort Comparable Array Items in Java</title><description>&lt;a href="https://en.wikipedia.org/wiki/Insertion_sort" target="_blank"&gt;Insertion sort&lt;/a&gt; is one of the most common sorting algorithms.&lt;br /&gt;
&lt;br /&gt;
Insertion sort algorithm achieves expected output by applying following steps :&lt;br /&gt;
&lt;br /&gt;
1-) Start from the beginning index&lt;br /&gt;
2-) Compare next element with the previous item&lt;br /&gt;
3-) If current element is smaller than the previous element than swap elements&lt;br /&gt;
4-) Repeat step 3 until a sorted array is achieved&lt;br /&gt;
&lt;br /&gt;
Space and time complexity values are :&lt;br /&gt;
&lt;br /&gt;
Worst case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;О(&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;n&lt;/i&gt;&lt;sup style="background-color: #f9f9f9; font-family: sans-serif; font-size: 9.856px; line-height: 1;"&gt;2&lt;/sup&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;)&lt;/span&gt;&lt;br /&gt;
Best case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;О(&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;n&lt;/i&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;)&lt;/span&gt;&lt;br /&gt;
Average case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;О(&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;n&lt;/i&gt;&lt;sup style="background-color: #f9f9f9; font-family: sans-serif; font-size: 9.856px; line-height: 1;"&gt;2&lt;/sup&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
There is an animation for the insertion sort algorithm at :&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="https://upload.wikimedia.org/wikipedia/commons/0/0f/Insertion-sort-example-300px.gif" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Following is the Java implementation of the Insertion&amp;nbsp;Sort algorithm where the sort method accepts an array of &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html" target="_blank"&gt;Comparable &lt;/a&gt;&amp;nbsp;items as input parameter.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

public class InsertionSort {

 public static void sort(Comparable[] a) {

  // Sort a[] into increasing order.
  int N = a.length;
  
  // Insert a[i] among a[i-1], a[i-2], a[i-3]....
  for (int i = 1; i &amp;lt; N; i++) { 
   for ( int j = i; j &amp;gt; 0 &amp;amp;&amp;amp; ( (a[j].compareTo( a[j-1]))&amp;lt; 0 ); j--)
   {
       Comparable temp = a[j];
       a[j] = a[j - 1];
       a[j - 1] = temp;    
   }    
  }

 }

 public static void main(String[] args) {

  String[] strArr = { "S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E" };

  System.out.println("Before Sort = ");
  for (int i = 0; i &amp;lt; strArr.length; i++)
   System.out.print(strArr[i] + " ");
  System.out.println("\n");

  sort(strArr);

  System.out.println("After Sort = ");
  for (int i = 0; i &amp;lt; strArr.length; i++)
   System.out.print(strArr[i] + " ");
  System.out.println();

  Integer[] intArr = { 9, 8, 7, 6, 6, 5, 4, 3, 2, 2, 1 };

  System.out.println();
  System.out.println("Before Sort = ");
  for (int i = 0; i &amp;lt; intArr.length; i++)
   System.out.print(intArr[i] + " ");
  System.out.println("\n");

  sort(intArr);

  System.out.println("After Sort = ");
  for (int i = 0; i &amp;lt; intArr.length; i++)
   System.out.print(intArr[i] + " ");
  System.out.println();
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create an InsertionSort.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the InsertionSort class executed it is going to print :&lt;br /&gt;
&lt;br /&gt;
Before Sort = &lt;br /&gt;
S O R T E X A M P L E &lt;br /&gt;
&lt;br /&gt;
After Sort = &lt;br /&gt;
A E E L M O P R S T X &lt;br /&gt;
&lt;br /&gt;
Before Sort = &lt;br /&gt;
9 8 7 6 6 5 4 3 2 2 1 &lt;br /&gt;
&lt;br /&gt;
After Sort = &lt;br /&gt;
1 2 2 3 4 5 6 6 7 8 9 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Because sort method accepts an array or Comparable items, so it is safe to send an array of items where each item implements Comparable interface in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html" target="_blank"&gt;Integer &lt;/a&gt;and &lt;a href="http://docs.oracle.com/javase/7/docs/api/java/lang/String.html" target="_blank"&gt;String &lt;/a&gt;classes in Java implement Comparable interface so it is acceptable to send this types as parameters to sort method.</description><link>http://tufangorel.blogspot.com/2015/10/insertion-sort-comparable-array-items-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-2437950341902140829</guid><pubDate>Fri, 16 Oct 2015 16:51:00 +0000</pubDate><atom:updated>2015-10-16T20:11:12.816+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Selection Sort Comparable Array Items in Java</title><description>&lt;a href="https://en.wikipedia.org/wiki/Selection_sort" target="_blank"&gt;Selection sort&lt;/a&gt; is one of the &lt;a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"&gt;in-place&lt;/a&gt; sorting algorithms.&lt;br /&gt;
&lt;br /&gt;
It is considered simple to implement but not efficient for large inputs.&lt;br /&gt;
&lt;br /&gt;
General steps :&lt;br /&gt;
&lt;br /&gt;
1-) Start from the beginning index&lt;br /&gt;
2-) Find the minimum&lt;br /&gt;
3-) Swap the found minimum with the item at the starting position of the set&lt;br /&gt;
4-) Do it until the last item is reached&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Space and time complexity values are :&lt;br /&gt;
&lt;br /&gt;
Worst case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;О(&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;n&lt;/i&gt;&lt;sup style="background-color: #f9f9f9; font-family: sans-serif; font-size: 9.856px; line-height: 1;"&gt;2&lt;/sup&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;)&lt;/span&gt;&lt;br /&gt;
Best case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt;  &lt;/span&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;О(&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;n&lt;/i&gt;&lt;sup style="background-color: #f9f9f9; font-family: sans-serif; font-size: 9.856px; line-height: 1;"&gt;2&lt;/sup&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;)&lt;/span&gt;&lt;br /&gt;
Average case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;О(&lt;/span&gt;&lt;i style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;n&lt;/i&gt;&lt;sup style="background-color: #f9f9f9; font-family: sans-serif; font-size: 9.856px; line-height: 1;"&gt;2&lt;/sup&gt;&lt;span style="background-color: #f9f9f9; font-family: sans-serif; font-size: 12.32px; line-height: 18.48px;"&gt;)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Worst case space complexity&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;O(n) total, O(1) auxiliary&lt;br /&gt;
&lt;br /&gt;
Following is the Java implementation of the Selection Sort algorithm where the sort method accepts an array of Comparable items as input parameter.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;

package basics;

public class SelectionSort {

   public static void sort(Comparable[] a) { 
  
    int N = a.length;
  
    for (int i = 0; i &amp;lt; N; i++) { 

        int minIndex = i;
        for (int j = i + 1; j &amp;lt; N; j++)
        if ( a[j].compareTo(a[minIndex])&amp;lt;0 )
             minIndex = j;

        if (minIndex != i) {
            Comparable temp = a[i];
            a[i] = a[minIndex];
            a[minIndex] = temp;
        }
    }
  
  }

  public static void main(String[] args) {
  
    String[] strArr = { "S", "O", "R", "T", "E", "X", "A", "M", "P", "L", "E" };
  
    System.out.println("Before Sort = " );
    for (int i = 0; i &amp;lt; strArr.length; i++)
     System.out.print(strArr[i] + " ");
    System.out.println("\n");
  
    sort(strArr);
  
    System.out.println("After Sort = " );
    for (int i = 0; i &amp;lt; strArr.length; i++)
     System.out.print(strArr[i] + " ");
    System.out.println();
  
    Integer[] intArr = { 9,8,7,6,6,5,4,3,2,2,1 };
  
    System.out.println();
    System.out.println("Before Sort = " );
    for (int i = 0; i &amp;lt; intArr.length; i++)
     System.out.print(intArr[i] + " ");
    System.out.println("\n");
  
    sort(intArr);
  
    System.out.println("After Sort = " );
    for (int i = 0; i &amp;lt; intArr.length; i++)
     System.out.print(intArr[i] + " ");
    System.out.println();
  }
}

&lt;/pre&gt;

&lt;br /&gt;
&lt;br /&gt;
Create a SelectionSort.java file in your workspace.&lt;br /&gt;&lt;br /&gt;

When the main method inside the SelectionSort class executed it is going to print :&lt;br /&gt;&lt;br /&gt;

Before Sort = &lt;br /&gt;

S O R T E X A M P L E &lt;br /&gt;&lt;br /&gt;

After Sort = &lt;br /&gt;

A E E L M O P R S T X &lt;br /&gt;&lt;br /&gt;

Before Sort = &lt;br /&gt;

9 8 7 6 6 5 4 3 2 2 1 &lt;br /&gt;&lt;br /&gt;

After Sort = &lt;br /&gt;

1 2 2 3 4 5 6 6 7 8 9 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Because sort method accepts an array or Comparable items, it is safe to send an array of items where each item implements Comparable interface in Java.</description><link>http://tufangorel.blogspot.com/2015/10/selection-sort-comparable-array-items-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-7884006080262077419</guid><pubDate>Thu, 15 Oct 2015 15:52:00 +0000</pubDate><atom:updated>2015-10-15T18:57:05.945+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Custom Reverse Primitive Integer Array in Java</title><description>In order to reverse an integer primitive array in java an i&lt;a href="https://en.wikipedia.org/wiki/In-place_algorithm" target="_blank"&gt;n-place&lt;/a&gt;&amp;nbsp;algorithm can be used.&lt;br /&gt;
&lt;br /&gt;
General algorithm for reversing an integer array is :&lt;br /&gt;
&lt;br /&gt;
1-) Traverse the array until the midpoint found in a loop construct&lt;br /&gt;
2-) Implement a swap algorithm to exchange values of the items in the array&lt;br /&gt;
&lt;br /&gt;
Following is a utility method used to reverse an existing array in java.

&lt;pre class="brush:java" name="code"&gt;

package basics;

public class ArrayUtility {
 
 static int[] reverse( int[] input )
 {
     if( input==null || input.length==0 )
         return null;
        
     int N = input.length;
  
     for( int i=0; i&amp;lt;N/2; i++ )
     {
        int temp = input[i];
        input[i] = input[N-i-1];
        input[N-i-1] = temp;
     }
  
     return input;
 } 

 public static void main(String[] args) {
  
  int[] original = {22,55,66,11,32,56,67,89,95,10};
  
  System.out.print("Original Array = ");
  for( int i = 0; i&amp;lt;original.length; i++ )
   System.out.print( original[i]+" " );
  
  int[] reverseArray = reverse( original );
 
  System.out.print("\nReversed Array = ");
  for (int i = 0; i &amp;lt;reverseArray.length; i++)
   System.out.print(reverseArray[i]+" ");  
 }
 
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a ArrayUtility.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the ArrayUtility class executed it is going to print :
&lt;br /&gt;
&lt;br /&gt;
Original Array = 22 55 66 11 32 56 67 89 95 10 &lt;br /&gt;
Reversed Array = 10 95 89 67 56 32 11 66 55 22 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/10/custom-reverse-primitive-integer-array-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-6426047048276974846</guid><pubDate>Thu, 15 Oct 2015 15:24:00 +0000</pubDate><atom:updated>2015-10-15T18:49:51.758+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Custom Deep Copy Primitive Integer Array in Java</title><description>There are different methods to create &lt;a href="https://en.wikipedia.org/wiki/Object_copying" target="_blank"&gt;copies &lt;/a&gt;of an array. Deep copy is considered as an expensive method when compared to shallow copying.&lt;br /&gt;
&lt;br /&gt;
Following is a utility method used to copy an existing array into a new freshly created array in java.&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

import java.util.Arrays;

public class ArrayUtility {
 
 static int[] copy( int[] input )
 {
     if ( input==null || input.length==0 )
         return null;
        
     int N = input.length;
     int[] copy = new int[N];
     for( int i = 0; i&amp;lt;N ; i++ )
         copy[i] = input[i];
  
     return copy;
 } 

 public static void main(String[] args) {
  
  int[] original = {22,55,66,11,32,56,67,89,95,10};
  
  int[] copyArray = copy( original );
  
  boolean result = Arrays.equals( original, copyArray );
  
  System.out.println( "Arrays are equal = "+result ); 

  System.out.print("\nOriginal Array = ");
  for( int i = 0; i&amp;lt; original.length; i++ )
       System.out.print( original[i]+" " );
  

  System.out.print("\nCopy Array = ");
  for (int i = 0; i &amp;lt; copyArray.length; i++)
    System.out.print(copyArray[i]+" ");
  
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a ArrayUtility.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the ArrayUtility class executed it is going to print :
&lt;br /&gt;
&lt;br /&gt;
Arrays are equal = true&lt;br /&gt;
&lt;br /&gt;
Original Array = 22 55 66 11 32 56 67 89 95 10 &lt;br /&gt;
Copy Array = 22 55 66 11 32 56 67 89 95 10 &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/10/custom-deep-copy-primitive-integer-array-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-6221100352499565730</guid><pubDate>Thu, 15 Oct 2015 15:00:00 +0000</pubDate><atom:updated>2015-10-15T18:50:25.777+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Binary search algorithm</category><category domain="http://www.blogger.com/atom/ns#">Java</category><category domain="http://www.blogger.com/atom/ns#">Recursion</category><title>Recursive Binary Search Algorithm in Java</title><description>&lt;a href="https://en.wikipedia.org/wiki/Binary_search_algorithm" target="_blank"&gt;Binary Search&lt;/a&gt;&amp;nbsp;is one of the most effective search algorithms used for searching a list of sorted items.&lt;br /&gt;
&lt;br /&gt;
Known complexities for average, best and worst cases are considered as follows :&lt;br /&gt;
&lt;br /&gt;
Worst case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; =&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;nbsp;O(log n)&lt;br /&gt;
Best case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; = &amp;nbsp;O(1)&lt;br /&gt;
Average case performance &amp;nbsp; =&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;nbsp;O(log n)&lt;br /&gt;
&lt;br /&gt;
Following is the recursive implementation of binary search algorithm in Java.
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

import java.util.Arrays;

public class RecursiveBinarySearch {

 public static int index( int[] input, int key )
 {
     return index( input, key, 0, input.length-1 );
 }
 
 public static int index( int[] input, int key, int low, int high )
 {
     if( low&amp;gt;high )
         return -1;
  
     int mid = low+(high-low)/2;
  
     if( key&amp;gt;input[mid] )
         return index(input, key, mid+1, high);
     else if( key&amp;lt;input[mid] )
         return index( input, key, low, mid-1 );
     else
         return mid;
 }
 
 public static void main(String[] args) {
  
  int[] list = {22,55,66,11,32,56,67,89,95,10};
  
  Arrays.sort(list);
  
  System.out.print("Sorted Array = ");
  for (int i = 0; i &amp;lt; list.length; i++) {
   System.out.print(list[i]+" ");
  }
  
  int itemIndex = index(list, 22);
  System.out.println("\n\nItem = "+22+", Index = "+itemIndex);
  
  itemIndex = index(list, 11);
  System.out.println("Item = "+11+", Index = "+itemIndex);
  
  itemIndex = index(list, 67);
  System.out.println("Item = "+67+", Index = "+itemIndex);
  
  itemIndex = index(list, 10);
  System.out.println("Item = "+10+", Index = "+itemIndex);

  itemIndex = index(list, 101);
  System.out.println("Item = "+101+", Index = "+itemIndex);
  
 }
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a RecursiveBinarySearch.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the RecursiveBinarySearch class executed it is going to print :
&lt;br /&gt;
&lt;br /&gt;
Sorted Array = 10 11 22 32 55 56 66 67 89 95 &lt;br /&gt;
&lt;br /&gt;
Item = 22, Index = 2&lt;br /&gt;
Item = 11, Index = 1&lt;br /&gt;
Item = 67, Index = 7&lt;br /&gt;
Item = 10, Index = 0&lt;br /&gt;
Item = 101, Index = -1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/10/recursive-binary-search-algorithm-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-1629739459234463496.post-1258719319426305770</guid><pubDate>Thu, 15 Oct 2015 14:52:00 +0000</pubDate><atom:updated>2015-10-15T18:58:54.985+03:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Binary search algorithm</category><category domain="http://www.blogger.com/atom/ns#">Java</category><title>Iterative Binary Search Algorithm in Java</title><description>&lt;a href="https://en.wikipedia.org/wiki/Binary_search_algorithm" target="_blank"&gt;Binary Search&lt;/a&gt; is one of the most effective search algorithms used for searching a list of sorted items.&lt;br /&gt;
&lt;br /&gt;
Known complexities for average, best and worst cases are considered as follows :&lt;br /&gt;
&lt;br /&gt;
Worst case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; =&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;nbsp;O(log n)&lt;br /&gt;
Best case performance&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;= &amp;nbsp;O(1)&lt;br /&gt;
Average case performance &amp;nbsp; =&lt;span class="Apple-tab-span" style="white-space: pre;"&gt; &lt;/span&gt;&amp;nbsp;O(log n)&lt;br /&gt;
&lt;br /&gt;
Following is the iterative implementation of binary search algorithm in Java.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre class="brush:java" name="code"&gt;package basics;

import java.util.Arrays;

public class IterativeBinarySearch {
 
 public static int index( int[] input, int key )
 {
    int low = 0;
    int high = input.length-1;  
    while( low&amp;lt;=high )
    {
       int mid = low+(high-low)/2;
       if( key&amp;lt;input[mid] )
          high = mid-1;
       else if( key&amp;gt;input[mid] )
          low = mid+1;
       else
          return mid;
    }
    return -1;
 }
 
 public static void main(String[] args) {
  
  int[] list = {22,55,66,11,32,56,67,89,95,10};
  
  Arrays.sort(list);
  
  int itemIndex = index(list, 22);
  System.out.println(itemIndex);
  
  itemIndex = index(list, 11);
  System.out.println(itemIndex);
  
  itemIndex = index(list, 67);
  System.out.println(itemIndex);
  
  itemIndex = index(list, 10);
  System.out.println(itemIndex);

  itemIndex = index(list, 101);
  System.out.println(itemIndex);
 }
 
}

&lt;/pre&gt;
&lt;br /&gt;
&lt;br /&gt;
Create a IterativeBinarySearch.java file in your workspace.&lt;br /&gt;
&lt;br /&gt;
When the main method inside the IterativeBinarySearch class executed it is going to print :
&lt;br /&gt;
&lt;br /&gt;
2&lt;br /&gt;
1&lt;br /&gt;
7&lt;br /&gt;
0&lt;br /&gt;
-1&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;</description><link>http://tufangorel.blogspot.com/2015/10/iterative-binary-search-algorithm-in-java.html</link><author>noreply@blogger.com (tufang)</author><thr:total>0</thr:total></item></channel></rss>