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!...
- Insertion Sort Algorithm
- Bubble Sort Algorithm
- Selection Sort Algorithm
- Linked List Implemenation in Java
- Array implementation of Stack in Java
- Linked List implementation of Stack in Java
- Implementation of two stacks in one array
- Linked List implementation of Queue Data structure in Java
- Find nth node from the end of a linked list in Java
- How to check whether array has duplicated elements
- Given an integer array, find all pairs that sum up to a specific value k
- Find Next Higher Number With Same Digits
If you know anyone who has started learning Java, why not help them out! Just share this post with them. Thanks for studying today!...
Please try this code
ReplyDeletepackage 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;
}
private static int[] insertionSortPradeep(int[] 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("************ insertionSortPradeep *************");
System.out.println("Before insertion sort..........");
System.out.println(Arrays.toString(array));
array = insertionSortPradeep( array );
System.out.println("\nAfter insertion..........");
System.out.println(Arrays.toString(array));
} // main ends
} // class ends
I really a lot while reading your blog.. thanks for sharing
ReplyDeleteDot Net Developer Jobs available in Chennai
I like your blog. very interesting. it will be useful for java learners and developers also.
ReplyDeletewe are the best web design and development company in coimbatoreour services are
logo design coimbatore
digital marketing company in coimbatore
seo services in coimbatore