user2989955
user2989955

Reputation:

How to write algorithm to sort an array in ascending order and also measure the running time?

I am on the halfway to complete a project and I can't reach my final destination. Basically I have to complete the console application and in my codes there are lot of commented off lines what gives the instruction to complete it.

I have the following requirements for the project:

  1. Sort the arrays which the names are starting with “sortMe…”, and measure the running time for sorting each of the arrays separately. Sort should be ascending.

  2. Find the minimum in any chosen array (sorted or unsorted) and measure the running time.

  3. Find number of occurrences of a specific number in any chosen array (sorted or unsorted) and measure the running time.

  4. Print out the content of a chosen array.

5.Quit!

Codes for the main class:

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package da522b_lab3_2013;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

/**
 * @author Stranger
 * Kristianstad University
 */

public class Main {


    //declaration
    Scanner myScanner;

    int[] sortMeHundredArr;
    int[] sortMeTenThousandArr;
    int[] sortMeMillionArr;

    int[] unSortedHundredArr;
    int[] unSortedTenThousandArr;
    int[] unSortedMillionArr;
    //You can have a currentArray variable here

    boolean firstTime;
    boolean sorted; //You can use the boolean firstTime instead.

    /** Creates a new instance of Main */
    public Main() {

        //Initialization
        myScanner = new Scanner(System.in);

        sortMeHundredArr = new int[100];
        sortMeTenThousandArr = new int[10000];
        sortMeMillionArr = new int[1000000];

        unSortedHundredArr = new int[100];
        unSortedTenThousandArr = new int[10000];
        unSortedMillionArr = new int[1000000];

        firstTime = true;

        //The method fills the arrays with the numbers form  "milliion.txt""
        fillAllArrays();

        //The arrays are identical before the user sort them, after sorting they are different in the order of contents
    }//End of constructor

    public void myMainMethod(){
        while(true){
            printMenu();
            //Only accepts numbers in range of signed integers. It does not take care of entries other than those numbers.
            switch(myScanner.nextInt()){
                case 1:
                    System.out.println("I'll write the code to sort three arrays which their names started with sortMe...");
                    //Write a separate class and some code here and sort the numbers ascending
                    SortingArray sa=new SortingArray();//I am not sure about it's accuracy
                    // - sortMeHundredArr
                    sa.arraySortingHundred(sortMeHundredArr);//I am not sure if this line is placed in the correct way
                    // - sortMeTenThousandArr
                    sa.arraySortingTenthousand(sortMeTenThousandArr);//I am not sure if this line is placed in the correct way
                    // - sortMeMillionArr
                    sa.arraySortingOneMillion(sortMeMillionArr);//I am not sure if this line is placed in the correct way
                    //Do not forget running time measurement sepaerately for each array
                    firstTime = false;
                    break;

                case 2:
                    //use "boolean sorted" in line 35 to prevent being activated before sorting
                    chooseArrayMenu();

                    System.out.println("I'll write the code to find the minimum in the chosen array ");
                    //write the code of finding minimum, better to implement method(s)/class(es)
                    //The user should have the option to choose any of the sortMe... or unsorted... arrays at a time
                    //Do not forget running time measurement
                    break;

                case 3:
                    //use "boolean sorted" in line 35 to prevent being activated before sorting
                    chooseArrayMenu();

                    System.out.println("I'll write the code to find how many times a specific number is repeated in the chosen array");
                    //Write the code of finding number of occurrences, better to implement method(s)/class(es)
                    //The user should have the option to choose any of the sortMe... or unsorted...  arrays at a time
                    //Do not forget running time measurement
                    break;

                case 4:
                    //use "boolean sorted" in line 36 to prevent being activated before sorting
                    chooseArrayMenu();

                    System.out.println("I'll write the code to print out the content of a chosen array");
                    System.out.println("Array with one million integers may take long time to print out");
                    break;

                case 5:
                    System.out.println("Welcome back");
                    System.exit(0);
                    break;

                default:
                    System.out.println("The input is out of range");
                    break;
            } //End of switch
        } //End of while loop
    } //End of myMainMethod method


    private String printArray(){
        return null; //Null should be changed by you
    } //End of printArray method





    public static void main(String[] args) {
        //One easy way to get rid of static keyword in main method
        Main m1 = new Main();
        m1.myMainMethod();
    }



    private void fillAllArrays(){

        writeToArrayFromFile("million.txt", sortMeHundredArr);
        writeToArrayFromFile("million.txt", sortMeTenThousandArr);
        writeToArrayFromFile("million.txt", sortMeMillionArr);

        writeToArrayFromFile("million.txt", unSortedHundredArr);
        writeToArrayFromFile("million.txt", unSortedTenThousandArr);
        writeToArrayFromFile("million.txt", unSortedMillionArr);
    } //End of fillAllArrays method

    public void writeToArrayFromFile(String fileName, int[] inputArr) {
        int temp = inputArr.length;
        String s;
        try{
            BufferedReader b = new BufferedReader(new InputStreamReader(new FileInputStream(fileName)));
            while (temp>0) {
                s = b.readLine();
                if (s == null) break;
                inputArr[inputArr.length-temp--] = Integer.parseInt(s);
            }//End of try
            b.close();
        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }  //End of catch
    }//End of writeToArrayFromFile method

    private void chooseArrayMenu(){
        System.out.println("Enter a number between 1 to 6");

        System.out.println("1. Sorted array containing 100 integers");
        System.out.println("2. Sorted array containing 10,000 integers");
        System.out.println("3. Sorted array containing 1,000,000 integers");
        System.out.println("4. Unsorted array containing 100 integers");
        System.out.println("5. Unsorted array containing 10,000 integers");
        System.out.println("6. Unsorted array containing 1,000,000 integers");

    } //End of chooseArrayMenu

    private  void printMenu() {
        if(firstTime){
            System.out.println("\n\n***** Welcome to our application *****");
            System.out.println("******* Please follow the menu *******");
            System.out.println("\n**** Main menu ****");

            System.out.println("Select 1 or 5");
            System.out.println("1. Sort");
            System.out.println("2. (Unavailabe before sorting) Find minimum");
            System.out.println("3. (Unavailabe before sorting) Find number of occurrences");
            System.out.println("4. (Unavailabe before sorting) Print out one array");
            System.out.println("5. Quit");
        } //End of if

        else{
            System.out.println("\n**** Main menu ****");
            System.out.println("select a number between 1 to 5");
            System.out.println("1. Sort");
            System.out.println("2. Find minimum");
            System.out.println("3. Find number of occurrences");
            System.out.println("4. Print out one array");
            System.out.println("5. Quit");

        } //End of else
    } //End of printMenu method


}

Codes for the ArraySorting class:

    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package da522b_lab3_2013;

/**
 *
 * @author Jabir Al Fatah
 */
public class SortingArray {
    public int[] arraySortingHundred(int[] sortMeHundredArr){
    int HundredArrL=sortMeHundredArr.length;
        int temp;
        for(int i=0;i<HundredArrL;i++){
            for(int j=(HundredArrL-1);j>=(i+1);j--){
                if(sortMeHundredArr[j]<sortMeHundredArr[j-1]){
                    temp=sortMeHundredArr[j];
                    sortMeHundredArr[j]=sortMeHundredArr[j-1];
                    sortMeHundredArr[j-1]=temp;

                }
                return sortMeHundredArr;
            }
        }
        return null;
        }
     public int[] arraySortingTenthousand(int[] sortMeTenThousandArr){
         int TenThousandArrL=sortMeTenThousandArr.length;
         int temp;
          for(int i=0;i<TenThousandArrL;i++){
            for(int j=(TenThousandArrL-1);j>=(i+1);j--){
                if(sortMeTenThousandArr[j]<sortMeTenThousandArr[j-1]){
                    temp=sortMeTenThousandArr[j];
                    sortMeTenThousandArr[j]=sortMeTenThousandArr[j-1];
                    sortMeTenThousandArr[j-1]=temp;
     }
    return sortMeTenThousandArr;
}
          }
        return null;

     }
     public int[] arraySortingOneMillion(int[] sortMeMillionArr){
         int millionArrL=sortMeMillionArr.length; 
         int temp;
          for(int i=0;i<millionArrL;i++){
            for(int j=(millionArrL-1);j>=(i+1);j--){
                if(sortMeMillionArr[j]<sortMeMillionArr[j-1]){
                    temp=sortMeMillionArr[j];
                    sortMeMillionArr[j]=sortMeMillionArr[j-1];
                    sortMeMillionArr[j-1]=temp;
     }
    return sortMeMillionArr;
}

     }
   return null;
}
}

I would be very greatful if I get help from you.

Upvotes: 0

Views: 2625

Answers (2)

Fr0z3n7
Fr0z3n7

Reputation: 2668

Do you have to write your own algorithm to sort an array ? Otherwise you can use Arrays.sort(). If you want to measure the time (milliseconde):

long startTime = System.currentTimeMillis();
//here your treatment of sorting your array
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);

Upvotes: 1

chili_h
chili_h

Reputation: 1

You could also use JAMon to measure the time.

JaMon is a nice java API to monitor application performance. It allows to tracks hits and also execution times (total, avg, min, max, stddev).

Get more information about JaMon here: http://jamonapi.sourceforge.net/

Upvotes: 0

Related Questions