Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

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

  1. #1
    Junior Member
    Join Date
    Aug 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post 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:

    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.

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

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

    Print out the content of a chosen array.

    5.Quit!

    main method:

    /*
    * 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 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 about it's accuracy
    //System.out.println(sortMeHundredArr);
    // - sortMeTenThousandArr
    sa.arraySortingTenthousand(sortMeTenThousandArr);//I am not sure about it's accuracy
    // - sortMeMillionArr
    sa.arraySortingOneMillion(sortMeMillionArr);//I am not sure about it's accuracy
    //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


    }



    SortingArray mehod:

    /*
    * 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;
    }
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,453
    My Mood
    Mellow
    Thanks
    146
    Thanked 607 Times in 594 Posts

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

    Please read the Announcement topic at the top of the sub-forum to learn how to post code in code or highlight tags and other useful info for newcomers.

    Too much code, no questions. Please fix your post so that your code is readable and then ask a specific question we can help you with.

Similar Threads

  1. ascending sort
    By my21 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 11th, 2013, 11:21 PM
  2. ascending order array
    By jamie1234 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 9th, 2013, 06:41 PM
  3. organizing linkedlist in ascending order
    By mia_tech in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 9th, 2012, 06:44 AM
  4. Replies: 3
    Last Post: June 1st, 2011, 12:47 AM
  5. Replies: 4
    Last Post: November 14th, 2010, 10:44 AM

Tags for this Thread