|
1 |
| -import java.util.Scanner; |
2 |
| - |
3 | 1 | /**
|
4 |
| - * This class implements Insertion Sort |
5 |
| - * |
6 |
| - * @author Unknown |
| 2 | + * |
| 3 | + * @author Varun Upadhyay (https://github.com/varunu28) |
7 | 4 | *
|
8 | 5 | */
|
9 |
| -class InsertionSort |
10 |
| -{ |
11 |
| - /** |
12 |
| - * Main Method |
13 |
| - * |
14 |
| - * @param args Command line arguments |
15 |
| - */ |
16 |
| - public static void main(String[] args) |
17 |
| - { |
18 |
| - int array[]=new int[6]; |
19 |
| - Scanner input=new Scanner(System.in); |
20 |
| - |
21 |
| - //Input |
22 |
| - System.out.println("Enter any 6 Numbers for Unsorted Array : "); |
23 |
| - for(int i=0; i<6; i++) |
24 |
| - { |
25 |
| - array[i]=input.nextInt(); |
26 |
| - } |
27 |
| - |
28 |
| - //Sorting |
29 |
| - for(int i=0; i<6; i++) |
30 |
| - { |
31 |
| - int temp=array[i]; |
32 |
| - int j=i-1; |
33 |
| - while(j>=0 && temp<array[j] ) |
34 |
| - { |
35 |
| - array[j+1]=array[j]; |
36 |
| - j--; |
37 |
| - } |
38 |
| - |
39 |
| - array[j+1]=temp; |
40 |
| - } |
41 |
| - |
42 |
| - //Output |
43 |
| - for(int i=0; i<6; i++) |
44 |
| - { |
45 |
| - System.out.print(array[i]+"\t"); |
46 |
| - } |
47 |
| - input.close(); |
48 |
| - } |
| 6 | + |
| 7 | +class InsertionSort { |
| 8 | + |
| 9 | + /** |
| 10 | + * This method implements the Generic Insertion Sort |
| 11 | + * |
| 12 | + * @param array The array to be sorted |
| 13 | + * @param last The count of total number of elements in array |
| 14 | + * Sorts the array in increasing order |
| 15 | + **/ |
| 16 | + |
| 17 | + public static <T extends Comparable<T>> void IS(T array[], int last) { |
| 18 | + T key; |
| 19 | + for (int j=1;j<last;j++) { |
| 20 | + |
| 21 | + // Picking up the key(Card) |
| 22 | + key = array[j]; |
| 23 | + int i = j-1; |
| 24 | + while (i>=0 && key.compareTo(array[i]) < 0) { |
| 25 | + array[i+1] = array[i]; |
| 26 | + i--; |
| 27 | + } |
| 28 | + // Placing the key (Card) at its correct position in the sorted subarray |
| 29 | + array[i+1] = key; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + // Driver Program |
| 34 | + public static void main(String[] args) { |
| 35 | + // Integer Input |
| 36 | + int[] arr1 = {4,23,6,78,1,54,231,9,12}; |
| 37 | + int last = arr1.length; |
| 38 | + Integer[] array = new Integer[arr1.length]; |
| 39 | + for (int i=0;i<arr1.length;i++) { |
| 40 | + array[i] = arr1[i]; |
| 41 | + } |
| 42 | + |
| 43 | + IS(array, last); |
| 44 | + |
| 45 | + // Output => 1 4 6 9 12 23 54 78 231 |
| 46 | + for (int i=0;i<array.length;i++) { |
| 47 | + System.out.print(array[i] + " "); |
| 48 | + } |
| 49 | + System.out.println(); |
| 50 | + |
| 51 | + // String Input |
| 52 | + String[] array1 = {"c", "a", "e", "b","d"}; |
| 53 | + last = array1.length; |
| 54 | + |
| 55 | + IS(array1, last); |
| 56 | + |
| 57 | + //Output => a b c d e |
| 58 | + for(int i=0; i<last; i++) |
| 59 | + { |
| 60 | + System.out.print(array1[i]+"\t"); |
| 61 | + } |
| 62 | + } |
49 | 63 | }
|
0 commit comments