Saturday, 4 June 2016

insertion sort implementation in java

in this  blogpost we will see what is insertion sort how it  can be implemented in java





what we have is an unsorted array , first we have to split the array in to two indices from 0 -> sorted array and from 0+1 -> length of the array an unsorted array  , so in the above array 4 is sorted and the rest of the array is unsorted so we are increasing the sorting array in the next iteration we are increasing the index to 1 , and then from end of the sorting array to the beginning of the sorted array we are comparing each elements we are comparing each elements if they are not sorted then we swap their positions , here the sorted array is from 0-1 and the elements are 4, 3 since it is not sorted we are swapping the two elements after swapping 


now 0-1 is sorted and we move the sorted index to 2 , and there are three elements then from index 2 to 0 we comparing if all the elements are sorted , first we compare the index 2 with 1 since it is  not sorted we swap the elements and move the cursor to one then we compare  index 1 with 0 since those elements were not sorted we swap them  after that the array will look like this 




then we increase the sorted index and repeat the process 


lets see how the code looks Like 





and it was run successfully against these following Test cases 











Sunday, 17 April 2016

Doubly Linked List implementation in Java

In this blog post we will see  how to implement LinkedList in java,  first we have to understand how Linked List differs from ArrayList  , This is how Array List is stored in memory




but Link List just has a reference to the next node ,  in the case of doubly Linked a Node has pointer to both previous element and the next element , if the node previous Node is null then it is the head node , if its next node is null then it is the tail or Last element 



Lets see the implementation and  the Test cases 



















as you can see the implementation is straight forward , nothing complex the entire code and Test cases can be found in the following repo





Saturday, 12 March 2016

CREATING A ZIP file in JAVA

package zip;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class CreatingZip {
    public static void main(String[] args) {
FileOutputStream fos;
try {
    fos= new FileOutputStream("/home/myname/kkk.zip");
    ZipOutputStream zis = new ZipOutputStream(fos);
    File file = new File("/home/myname/solo.txt");
    File file2 = new File("/home/myname/innerfolder/innerfile.txt");
    List<File> filelist= new ArrayList<File>(); 
    filelist.add(file2);
    filelist.add(file);

    for(File filename:filelist){
    FileInputStream FS = new FileInputStream(filename);
     System.out.println(filename.getAbsolutePath());
     System.out.println(filename.getPath());
     System.out.println(filename.getName());
    ZipEntry zipentry = new ZipEntry(filename.getPath());
    zis.putNextEntry(zipentry);
    int i;
    while((i=FS.read())>-1){
        zis.write(i);
    }
   

    }
   

    zis.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}       
     
       

}}

Tuesday, 8 March 2016

EXTRACTING A ZIP FILE IN JAVA

in this tutorial we are going to see how to extract a zip file using java. there are two ways to extract zip files

1)zipinputstream
2)zipfile

here we use the zip file

package zip;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class Multizipcreation {

    public static void main(String[] args) {
        File file = new File("/home/solomon/pdf.zip");
        try {
            byte[] buffer = new byte[1024];
            ZipFile zipFile = new ZipFile(file);
            File outputfolder = new File(file.getParent()+File.separator+"op");
            if(!outputfolder.exists()){
                outputfolder.mkdirs();
            }
            Enumeration<? extends ZipEntry> entries = zipFile.entries();
            while(entries.hasMoreElements()){
             ZipEntry entry = entries.nextElement();
             InputStream is = zipFile.getInputStream(entry);
             extract(is,entry,outputfolder);
           
            }
           
            zipFile.close();
       
           
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
               

    }

    private static void extract(InputStream is, ZipEntry entry,File op) {
     try {
       
        FileOutputStream fos = new FileOutputStream(op+File.separator+entry.getName());

       
        int i;
        while((i=is.read())>-1){
        fos.write(i);
        }
        fos.close();
       
       
    } catch ( IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
       
    }

}