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

• December 3rd, 2013, 01:37 PM
jabirfatah2009
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.FileInputStream;
import java.io.IOException;
import java.util.Scanner;

/**
* @author Stranger
*/

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){
//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

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

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

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{
while (temp>0) {
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

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");

if(firstTime){
System.out.println("\n\n***** Welcome to our application *****");

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("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

}

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;
}
}
• December 3rd, 2013, 04:12 PM
GregBrannon
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.