Skip to content

Instantly share code, notes, and snippets.

@ravichandrae
Created October 16, 2013 05:16
Show Gist options
  • Save ravichandrae/7002942 to your computer and use it in GitHub Desktop.
Save ravichandrae/7002942 to your computer and use it in GitHub Desktop.
This program converts an array of format {a1,a2,...an,b1,b2,...bn} to {a1,b1,a2,b2,...an,bn}
/**
* Created with IntelliJ IDEA.
* User: Ravi
* Date: 10/15/13
* Time: 7:51 PM
* To change this template use File | Settings | File Templates.
*/
public class ArrangeAlternate {
public static void main( String[] args )
{
int[] array = {1,3,5,7,2,4,6,8};
interleave(array);
for( int i = 0; i < array.length; i++ )
{
System.out.print( array[i] + " ");
}
}
public static void interleave(int[] array)
{
int i = 0;
int j = 0;
int mid = (array.length/2) - 1;//points the end of first half
//the outer loop runs for (length/2) - 1 times
for( i = 0; i < mid ; i++ )
{
//runs for (i+1) times
for( j = 0; j <= i; j++ )
{
//calculate the indices to be swapped
int l = mid - i + (2 * j);
int r = mid - i + (2 * j) + 1;
//swap the elements at left (l) and right (r)
int temp = array[l];
array[l] = array[r];
array[r] = temp;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment