//In Insertion Sort, sorted array is built one entry at a time
//Efficiency of insertion sort is less compared to other sorting method
public class HackDefence {
public static void main(String[] args) {
int[] input = {8,4,9,7,2,0,1,5,3,6};
insertionSort(input);
}
private static void displaySortedArray(int[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + "|");
}
System.out.println("\n");
}
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int k = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > k) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = k;
displaySortedArray(array);
}
}
}
(my facebook profile link: https://www.facebook.com/dipankar.choudhury1)
OUTPUT:
//Efficiency of insertion sort is less compared to other sorting method
public class HackDefence {
public static void main(String[] args) {
int[] input = {8,4,9,7,2,0,1,5,3,6};
insertionSort(input);
}
private static void displaySortedArray(int[] input) {
for (int i = 0; i < input.length; i++) {
System.out.print(input[i] + "|");
}
System.out.println("\n");
}
public static void insertionSort(int array[]) {
int n = array.length;
for (int j = 1; j < n; j++) {
int k = array[j];
int i = j-1;
while ( (i > -1) && ( array [i] > k) ) {
array [i+1] = array [i];
i--;
}
array[i+1] = k;
displaySortedArray(array);
}
}
}
(my facebook profile link: https://www.facebook.com/dipankar.choudhury1)
OUTPUT:
No comments:
Post a Comment