我正在完成一个项目,但无法到达最终目的地。基本上我必须完成控制台应用程序,在我的代码中,有很多离线注释的行,它们给出了完成它的指令。
我对该项目有以下要求:
对名称以“sortMe…”开头的数组进行排序,并且 测量分别对每个数组进行排序的运行时间。种类 应该是升序。
在任何选定的数组(已排序或未排序)中查找最小值并进行测量 运行时间。
查找任意选定数组中特定数字的出现次数 (排序或未排序)并测量运行时间。
打印出所选数组的内容。
5.退出!
主类代码:
/*
* 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
}
ArraySorting 类的代码:
/*
* 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;
}
}
如果能得到您的帮助,我将不胜感激。
最佳答案
您必须编写自己的算法来对数组进行排序吗?否则,您可以使用 Arrays.sort()。 如果你想测量时间(毫秒):
long startTime = System.currentTimeMillis();
//here your treatment of sorting your array
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
关于java - 如何编写算法对数组进行升序排序并测量运行时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20318102/