Wednesday, May 2, 2018

Merge Sort

import java.io.File;
import java.util.Arrays;
import java.util.Scanner;

/* File      :MergeSort.java 
* Author    :Prasanna Paudyal 
* Date      :05/02/2018 
* ----------------------------- 
* OVERVIEW * ======== 
* This is a simple program to 
* sort an array of numbers. This 
*  program does this by implementing  
*  the concept of Merge sort. 
*/ 
 
 public class MergeSort {
    public static void main(String[]args){
        int[] arr = new int[100];
        File file = new File(args[0]);
        try{
            Scanner scan = new Scanner(file);
            int i = 0;
            while(scan.hasNextLine()){
                String line = scan.nextLine();
                Scanner input = new Scanner(line);
                while(input.hasNext()){
                    String number = input.next();
                    int num = Integer.parseInt(number);
                    arr[i++] = num;
                }
                input.close();
            }
            scan.close();
        }catch(Exception e){
            System.err.println("File doesn't exist\n");
        }

        sort(arr);
 
 //print the sorted array 
 for(int i = 0; ilength;i++){
    System.out.printf("%d ",arr[i]);
    }
}

/** Merge sort algorithm 
 * @param arr: takes an array as an arguments and sorts them using merge sort 
*/  


public static void sort(int [] arr){
        if(arr.length <= 1){
            return;
        }
        //Split array into two 
        int middle = arr.length/2;
        int[] leftArray = Arrays.copyOfRange(arr,0,middle);
        int[] rightArray = Arrays.copyOfRange(arr, middle,arr.length);

        //Recursive call 
        sort(leftArray);
        sort(rightArray);

        // sort and build array back 
        int i = 0;
        int j = 0;
        while ((i +j)  < arr.length) {
            if (i < leftArray.length && j < rightArray.length) {
                if (leftArray[i] < rightArray[j]) {
                    arr[i + j] = leftArray[i];
                    i++;
                } else {
                    arr[i + j] = rightArray[j];
                    j++;
                }
            } else if (i < leftArray.length) {
                arr[i + j] = leftArray[i];
                i++;
            } else {
                arr[i + j] = rightArray[j];
                j++;
            }
        }

    }

}



/*
*Note: Find the test file for numbers here
*/ 

Insertion Sort to sort names.

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

/* File      :InsertionSort.java 
* Author     :blnakbug
* Date       :05/01/2018 
* ----------------------------- 
* OVERVIEW 
* ======== 
* This is a simple program to 
* sort a list of names. This program 
* does it by implementing the 
* concept of insertion sort. 
*/
 
 
 public class InsertionSort {

    public static void main(String[] args) {
        ArrayList arr = new ArrayList<>();

        String name = args[0];
        File file = new File(name);
        try{
            Scanner input = new Scanner(file);
            while(input.hasNextLine()){
                String line = input.nextLine();
                Scanner lineScan = new Scanner(line);

                while(lineScan.hasNext()){
                    String nameSort = lineScan.next();
                    String names = nameSort.toLowerCase();
                    arr.add(names);

                }
                lineScan.close();
            }
            input.close();
        }catch(Exception e){
            System.err.print("File doesn't exist.");
        }


        sorting(arr);
        System.out.printf("\tSorting list containing %d names....\n",arr.size());


        System.out.println("\t--------------------------------\n");
        for(int i = 0 ; i < arr.size();i++){
            System.out.printf("\t%s\n",arr.get(i));
        }
    }

    //method for sorting 
   public static void sorting(ArrayList list){
        for(int i = 1;i< list.size();i++){
            String key = list.get(i);
            int j = i - 1;
            while(j >= 0 && list.get(j).compareTo(key) > 0){
                String temp = list.get(j);
                list.set(j,list.get(j+1));
                list.set(j+1, temp);
                j--;

            }

        }
    }
}
 
/* 
* Note 
* Visit github  for the test file. 
* Usages: java InsertionSort [filename]
*/
 
 
 

Thursday, July 27, 2017

Hailstone Sequence

/* * This program generates hailstone sequence 
 * Use internet to find out how hailstone sequence works.
 */


public class HailstoneSequence {

    public static void sequenceCalculation(int num) {

        if(num == 0) {
            System.out.println("Please enter a natural number > 0:");
            return;
        }
        System.out.print(num);
        while(num!=1) {
            if(num % 2 == 0) {
                num = num/2;
            } else {
                num = 3*num + 1;
            }
            System.out.printf(", %d ", num);
        }
    }

    //main method    
public static void main(String [] args) {
        sequenceCalculation(7);
    }

}


   /*This program is not complete. It doesn't interact with the users.
  Use Scanner class to take user input and display the hailstone sequence.
 After doing some research you knew that the Hailstone sequence is a 
 sequence of numbers where the the nextnumber is division of 2 if the
 preceding num is even or if its odd then the following number will
 be 3 times the preceeding number plus 1.Mathematically:                 
                                      
                N(t+1) = N(t)/2 or 3N(t)+1; 
 
                       Where t = 1,2,3,4,...... ;             N = number;              
  */