im试图进行气泡排序,但也要测量处理过程所花费的时间,我遇到的第一个问题是我无法调用bubbleSort方法,甚至不知道一切对我而言都是正确的。 我的第二个问题是很长的startTime = System.nanoTime();显示为未声明,但我确实在代码之上声明。
import java.util.Random;
import java.util.Scanner;
//import java.util.concurrent.TimeUnit;
// Program to calculate execution time or elapsed time in Java
class Main
{
private static Scanner scan;
public static void main(String[] args) throws InterruptedException {
long startTime = System.nanoTime();
// ... the code being measured starts ...
scan = new Scanner(System.in);
Random rand = new Random();
int size;
int num;
int values[];
System.out.println("What is the size of the array?");
size = scan.nextInt();
values = new int[size];
System.out.println("The " + size + " random numbers are:");
for(int c = 0; c < size; c++)
{
num = rand.nextInt(100);
System.out.print((values[c] = num) + " ");
}
System.out.println("In order:");
for (int count = 0; count < values.length; count++)
System.out.println(count + " = " + bubbleSort(values[count]));
}
public static void bubbleSort(int[] arr)
{
boolean swap;
do
{
swap = false;
int temp;
for (int count = 0; count < arr.length - 1; count++)
if (arr[count] > arr[count+1])
{
temp = arr[count];
arr[count] = arr[count+1];
arr[count+1] = temp;
swap = true;
}
} while (swap);
System.out.println("In order:");
for (int count = 0; count < arr.length; count++)
System.out.print(arr[count] + " ");
}
// ... the code being measured ends ...
long endTime = System.nanoTime();
long timeElapsed = endTime - startTime;
}
答案 0 :(得分:1)
您的变量在主函数内部声明,这意味着它仅在主函数内部可见。尝试这样声明它:
class Main
{
private static Scanner scan;
long startTime;
public static void main(String[] args) throws InterruptedException {
startTime = System.nanoTime();
// ... the code being measured starts ...
[...]
第二个问题: 您正在尝试将Bubble-Function当作字符串来打印,但这只是一个void函数。
编辑: 我稍微整理了一下代码:
import java.util.Random;
import java.util.Scanner;
//import java.util.concurrent.TimeUnit;
// Program to calculate execution time or elapsed time in Java
class Main {
private static Scanner scan;
static long startTime;
public static void main(String[] args) throws InterruptedException {
startTime = System.nanoTime();
// ... the code being measured starts ...
scan = new Scanner(System.in);
Random rand = new Random();
int size;
int num;
int values[];
System.out.println("What is the size of the array?");
size = scan.nextInt();
values = new int[size];
for (int c = 0; c < size; c++) {
values[c] = rand.nextInt(100);
}
// print the random values
System.out.println("The " + size + " random numbers are:");
printValues(values);
//Sort the array here:
bubbleSort(values);
//print the sorted values
System.out.println("The " + size + " random numbers in order are:");
printValues(values);
// ... the code being measured ends ...
long endTime = System.nanoTime();
long timeElapsed = endTime - startTime;
System.out.print("The algorithm took " + timeElapsed + " ns to terminate.");
}
public static void printValues(int[] values) {
for (int count = 0; count < values.length; count++) {
System.out.print(values[count] + " ");
}
System.out.println();
}
public static void bubbleSort(int[] arr) {
boolean swap;
do {
swap = false;
int temp;
for (int count = 0; count < arr.length - 1; count++)
if (arr[count] > arr[count + 1]) {
temp = arr[count];
arr[count] = arr[count + 1];
arr[count + 1] = temp;
swap = true;
}
} while (swap);
}
}
您知道,我添加了一个printValues()函数,该函数使打印数组更加容易。请查看此代码并尝试理解它,如果有疑问,该怎么办! :D
答案 1 :(得分:0)
我的第一个问题是我无法调用bubbleSort方法
您发布的代码正在像这样调用您的bubbleSort
方法:
System.out.println(count + " = " + bubbleSort(values[count]));
当我将其粘贴到IDE中时,它显示一个错误。 bubbleSort
的方法签名期望一个整数数组– bubbleSort(int[] arr)
–但上面的那一行试图传递单个整数值。
尝试将整个values
数组传递给bubbleSort()
,而不仅仅是传递values[count]
上的单个值。因此,请注意:
bubbleSort(values[count])
尝试一下:
bubbleSort(values)
另外,不清楚println语句的目的是什么– bubbleSort()
被定义为不返回任何内容(void
),但是您尝试将结果附加到{ {1}}个无效语法的呼叫:
println()
如果您想每次打印System.out.println(count + " = " + bubbleSort(...);
之类的东西,并分别对循环中的count = 4
进行排序,则可以这样做:
values