Translate To Preferred Language

Search Obioku's Thoughts

Please Read Today's Featured Post

Things I Would Like To Happen Soon

For all those who believe in speaking ideas into existence and prayer in the universe will return to a person.  I simply want to write a lis...

Template for File Processing in Java (some bugs may occur)

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pt_2152_obobotette0;

//import packages
import java.io.*;
import java.util.*;

/**
 *
 * @author obobotette0
 */
public class PT_2152_obobotette0 {

    /**
     * @param args the command line arguments
     */
 
    public static void quicksort(int[] point, int low, int high) {
        //temp variable for swap
        int temp;
     
        //range variables
        int l = low;
        int h = high;
     
        //create midpoint and middle node
        int mid = (low + high) / 2;
        int median = point[mid];
        while (l <= h) {
            while (point[l] < median) {
                l++;
            }

            while (point[h] > median) {
                h--;
            }

            if (l <= h) {
                temp = point[l];
                point[l] = point[h];
                point[h] = temp;
                l++;
                h--;

            }
        }
        if (low < h) {
            quicksort(point, low, h);
        }
        if (l < high) {
            quicksort(point, l, high);
        }

    }

    public static void main(String[] args) throws FileNotFoundException, IOException {
        // TODO code application logic here
        int test_array[] = new int[100];
        int i = 0;
        String pt_filename = new String();
        String pt_filename2 = new String();
        pt_filename = "H:\\My Documents\\NetBeansProjects\\PT_2152_obobotette0\\src\\pt_2152_obobotette0\\COSC489_PT_2152_Input.txt";
        pt_filename2 = "H:\\My Documents\\NetBeansProjects\\PT_2152_obobotette0\\src\\pt_2152_obobotette0\\COSC489_PT_2152_Output_obobotette0.txt";
     
        //Read text file
        File pt_input = new File(pt_filename);
        Scanner pt_read = new Scanner(pt_input);
     
        //while there is more, read file and increase iterator
        while (pt_read.hasNextInt()) {
            test_array[i] = pt_read.nextInt();
            i++;

        }
        //close reader
        pt_read.close();

        //begin output files
        FileWriter pt_output = new FileWriter(pt_filename2);
        PrintWriter pt_write = new PrintWriter(pt_output);
        String format_out = new String();
     
        //output index value during each iteration
        for (int x = 0; x <= i; x++) {
            format_out = x + ": " + test_array[x] + ",\t\t";
            pt_write.append(format_out);
         
            //print after every 5 numbers
            if (x > 0 && x % 5 == 0) {
                pt_write.flush();
                pt_write.append("\n");
            }

        }
        pt_write.append("\n\n");

        //print again from the last entry to the first
        for (int y = i; y >= 0; y--) {
            format_out = y + ": " + test_array[y] + ",\t\t";
            pt_write.append(format_out);
         
            //flush after every 5 entries
            if (y % 5 == 0) {
                pt_write.flush();
                pt_write.append("\n");
            }
        }
        pt_write.append("\n\n");

        quicksort(test_array, 0, i);
        for (int z = 0; z <= i; z++) {
            format_out = z + ": " + test_array[z] + ",\t\t";
            pt_write.append(format_out);
            if (z > 0 && z % 5 == 0) {
                pt_write.flush();
                pt_write.append("\n");
            }

        }
        //Print last number of file after sorting
        //pt_write.append("78:"+test_array[78]+",\n\n");
        pt_write.flush();

        Random randnum = new Random(20150418);
        int[] small = new int[1000];
        int[] medium = new int[20000];
        int[] large = new int[50000];

        //individual loop for each array
        for (int o = 0; o < 1000; o++) {

            small[o] = randnum.nextInt(100001);
        }
        for (int o = 0; o < 20000; o++) {
            medium[o] = randnum.nextInt(100001);
        }

        for (int o = 0; o < 50000; o++) {
            large[o] = randnum.nextInt(100001);
        }

        //start and stop variable for each array sort
        long small_start;
        long small_stop;
        long medium_start;
        long medium_stop;
        long large_start;
        long large_stop;

        small_start = System.currentTimeMillis();
        quicksort(small, 0, small.length - 1);
        small_stop = System.currentTimeMillis();

        medium_start = System.currentTimeMillis();
        quicksort(medium, 0, medium.length - 1);
        medium_stop = System.currentTimeMillis();

        large_start = System.currentTimeMillis();
        quicksort(large, 0, large.length - 1);
        large_stop = System.currentTimeMillis();

        long small_time = small_stop - small_start;
        long medium_time = medium_stop - medium_start;
        long large_time = large_stop - large_start;

        String small_stat = new String();
        String small_content = new String();
        String medium_stat = new String();
        String medium_content = new String();
        String large_stat = new String();
        String large_content = new String();

        small_stat = "\nSize:" + small.length + ",\t\tTime:" + small_time + ",\t\t\n";
        small_content = "\n1st:" + small[0] + ",\t\t100th:" + small[99] + ",\t\t200th:" + small[199] + ",\t\t500th:" + small[499] + "\n\n";

        medium_stat = "\nSize:" + medium.length + ",\t\tTime:" + medium_time + ",\t\t\n";
        medium_content = "\n1st:" + medium[0] + ",\t\t100th:" + medium[99] + ",\t\t200th:" + medium[199] + ",\t\t500th:" + medium[499] + "\n\n";

        large_stat = "\nSize:" + large.length + ",\t\tTime:" + large_time + ",\t\t\n";
        large_content = "\n1st:" + large[0] + ",\t\t100th:" + large[99] + ",\t\t200th:" + large[199] + ",\t\t500th:" + large[499] + "\n\n";

        pt_write.append(small_stat + small_content);
        pt_write.flush();

        pt_write.append(medium_stat + medium_content);
        pt_write.flush();

        pt_write.append(large_stat + large_content);
        pt_write.flush();
    }

}

No comments:

Post a Comment

Thank you very much for viewing this entry and I hope you are able to return soon to continue to enjoy more of the site.
Please share your thoughts in the comment section.
Be blessed and enjoy life!