Package

Source Code of ShellSort

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.util.Scanner;


public class ShellSort {
  //counter the number of array accsess
  static protected long counter = 0;
 
  static void insert(double[] a, int h, int r, double v) {
    int i = r - h;
    while (i >= 0 && a[i] > v) {
      a[i+h] = a[i];
      i = i - h;
      counter++;
    }
    a[i+h] = v;
    counter++;
  }
  static void version0(double[] a, int p, int q) {
    int h;
    h = 1;
    for (int i = p + h; i < q; i++) {
      insert(a, h, i, a[i]);
    }
  }
  static void version1(double[] a, int p, int q) {
    int h;
    for (h = 1; h <= a.length / 6; h = 6 * h)
      /* nothing */ ;
    for ( ; h > 0; h = h / 6) {
      for (int i = p + h; i < q; i++) {
        insert(a, h, i, a[i]);
      }
    }
  }
  public static void main(String[] argv) {
   
    long assingCounter[][]; //values in to sort in array
    double[] arrayCopy; //copy for unsorted array
    int numValSort=0, interval=0; //inputs to set the paramiters of array
    String outputAssingment = "",csv = "", fileName = "";
   
    Scanner in = new Scanner(System.in);
    //input amunts from user
    System.out.println("How many values do you want to sort?");
    numValSort = in.nextInt();
   
    System.out.println("What interval do you want to set");
    interval = in.nextInt();
   
    System.out.println("Please name your csv file");
    fileName = in.next();
    in.close();
   
    //make array with the rows the size of the interval ie 100/5000 = 50
    //and make two cols to store version0 and 1
    assingCounter = new long[numValSort/interval][2];
   
    //start the loop to build array with random numbers to be sorted
    for(int b =1; b < assingCounter.length; b++)
    {
     
      //make array the size of the interval ie 100,200
      double a[] = new double[ b*interval ];
     
      //loop in the random number for sorting
      for (int i = 0; i < a.length; i++)
      {
        a[ i ] = Math.random();
        //System.out.println(i + " " + a[ i ]);
      }
     
    //make a copy of the unsorted array 
    arrayCopy = (double[]) a.clone();
   
    //test version 2
    version0(a, 0, a.length);
    assingCounter[b][0] = counter;
    counter = 0; //counter reset
   
    //test version 1
    version1(arrayCopy, 0, a.length);
    assingCounter[b][1] = counter;
    counter = 0;// counter reset
   
   
    outputAssingment = outputAssingment+ b*interval+"\t\t "+assingCounter[b][0]+"\t "+assingCounter[b][1]+"\n";
    csv = csv + b*interval+","+assingCounter[b][0]+","+assingCounter[b][1]+"\n";
    }
    //print output check
    System.out.println("Number of inserted values   version0  version1\n"+outputAssingment);
    //csv maker
    FileOutputStream fileOut = null;
      try {
        fileOut = new FileOutputStream(fileName+".csv");
        PrintWriter out = new PrintWriter(fileOut);
   
        out.print(csv);
        out.close();
      } catch (FileNotFoundException e) {
   
        e.printStackTrace();
      }
   
  }
 
}
TOP

Related Classes of ShellSort

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.