Friday 26 February 2016

Generic type Insertion Sort

Generic type Insertion Sort

This below code is capable of doing insertion sorting with Multiple data types like String, double, int as of now. It can be altered for any object that implements comparable.
 import java.util.Scanner;  
   
 public class MyInsertionSort {  
           public static void main(String[] args) {  
                     Scanner in = new Scanner(System.in);  
   
                     System.out.print("Enter data type to sort : ");  
                     String type = in.nextLine();  
   
                     System.out.print("Enter number of elements : ");  
                     String insertionSort = in.nextLine();  
                     int num = Integer.parseInt(insertionSort);  
                     String array[] = new String[num];  
                     for (int i = 0; i < array.length; i++) {  
                               System.out.print("Input the Number at array index " + i + ": ");  
                               array[i] = in.nextLine();  
                     }  
                     MyInsertionSort.insertionSortByType(array, type);  
                     in.close();  
           }  
   
           public static void insertionSortByType(String array[], String type) {  
                     switch (type) {  
                     case "double":  
                               Double[] ConvertedArrayDouble = new Double[array.length];  
                               for (int i = 0; i < array.length; i++)  
                                         ConvertedArrayDouble[i] = Double.parseDouble(array[i]);  
                               MyInsertionSort.insertionSort(ConvertedArrayDouble);  
                               break;  
                     case "int":  
                               Integer[] ConvertedArrayInt = new Integer[array.length];  
                               for (int i = 0; i < array.length; i++)  
                                         ConvertedArrayInt[i] = Integer.parseInt(array[i]);  
                               MyInsertionSort.insertionSort(ConvertedArrayInt);  
                               break;  
                     default:  
                               MyInsertionSort.insertionSort(array);  
                     }  
           }  
   
           public static <E extends Comparable<? super E>> void insertionSort(  
                               E array[]) {  
                     int n = array.length;  
                     for (int j = 1; j < n; j++) {  
                               E key = array[j];  
                               int i = j - 1;  
                               while ((i > -1) && (array[i].compareTo(key) > 0)) {  
                                         array[i + 1] = array[i];  
                                         i--;  
                               }  
                               array[i + 1] = key;  
                     }  
   
                     printNumbers(array);  
           }  
   
           public static <E> void printNumbers(E array[]) {  
                     for (E i : array) {  
                               System.out.println(i);  
                     }  
           }  
 }  

Tuesday 16 February 2016

Maven Build JAR once and share it offline

In Maven, you can build your project only once and get a JAR file fully packed with all dependencies. So that, you can share this JAR to other machines off-line.
Below are the steps to make it.
  1. First update your pom.xml with the setting
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.thanga.MyTest[REPLACE WITH YOUR MAIN CLASS]
                            </mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
  2. Package your project with the goal package assembly:single as shown below
This is equivalent to "mvn package assembly:single"


  1. Run this and you can get the two JAR files. One of them MyFullPack-0.0.1-SNAPSHOT-jar-with-dependencies.jar has the full dependencies loaded.

  1. You can open the JAR to see the dependencies are packed as shown below.

  1. You can share this JAR to other machines off-line without any more build