## Pages

### Algorithm Puzzles

Here you'll find some of the algorithm puzzles interview questions.

If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...

#### 1 comment:

package javalatte;

import java.util.Arrays;

public class InsertionSort {

/**
* @param args
*/

private static int[] insertionSort(int[] array){

for (int i =1; i < array.length; i++) {
System.out.println("******************");
for (int j = i; j > 0; j--) {
boolean swapped = false;

if( array[j-1] > array[j] ){
int temp = array[j];
array[j] = array[j-1];
array[j-1] = temp;

swapped = true;
}
if( ! swapped )
break;

System.out.println(Arrays.toString(array));
}
// System.out.println(Arrays.toString(array));
}

return array;
}

for (int i =1; i < array.length; i++) {
System.out.println("******************");
for (int j = 0; j < i; j++) {
if( array[i] < array[j] ){
int temp = array[j];
array[j] = array[i];
array[i] = temp;
}
System.out.println(Arrays.toString(array));
}
// System.out.println(Arrays.toString(array));
}

return array;
}

public static void main(String[] args) {

int[] array = {2,5,3,1,8,4,6,9,7};
// int[] array = {1,2,3,4,5,6,7,8,9};

System.out.println("************ insertionSort *************");
System.out.println("Before insertion sort..........");
System.out.println(Arrays.toString(array));

array = insertionSort( array );

System.out.println("\nAfter insertion..........");
System.out.println(Arrays.toString(array));

/////////////////

System.out.println("Before insertion sort..........");
System.out.println(Arrays.toString(array));