java-用于计算比较的代码&;算法未正确输出的执行时间
在java分配中,我必须计算发生的比较次数,以及选择排序、插入排序、冒泡排序、快速排序和合并排序算法的总执行时间。下面的代码是一个驱动程序,用于测试运行算法的代码,同时计算算法的执行时间和比较。在代码中,我用值初始化了列表,并对其进行了遍历。我得到的唯一输出是“排序列表…”,这只是第一行。剩下的都没跑了,我迷路了。请帮忙java-用于计算比较的代码&;算法未正确输出的执行时间,java,sorting,quicksort,bubble-sort,execution-time,Java,Sorting,Quicksort,Bubble Sort,Execution Time,在java分配中,我必须计算发生的比较次数,以及选择排序、插入排序、冒泡排序、快速排序和合并排序算法的总执行时间。下面的代码是一个驱动程序,用于测试运行算法的代码,同时计算算法的执行时间和比较。在代码中,我用值初始化了列表,并对其进行了遍历。我得到的唯一输出是“排序列表…”,这只是第一行。剩下的都没跑了,我迷路了。请帮忙 public class SortingTest { //create 3 constant values static final int SIZE = 1
public class SortingTest {
//create 3 constant values
static final int SIZE = 1000;//number of values in the lists
static final int SORTED_ORDER = 0;//for sorted lists
static final int RANDOM_ORDER = 1;//for unsorted lists
//create the lists for each sorting algorithm
static Integer[] selArr = new Integer[SIZE];
static Integer[] insArr = new Integer[SIZE];
static Integer[] bubArr = new Integer[SIZE];
static Integer[] quiArr = new Integer[SIZE];
static Integer[] merArr = new Integer[SIZE];
public static void main(String[] args) {
//perform the sorting on sorted lists
performSorting(SORTED_ORDER);
//perform the sorting on unsorted lists
performSorting(RANDOM_ORDER);
//perform the sorting on unsorted lists
performSorting(RANDOM_ORDER);
}
private static void performSorting(int order) {
if(order == SORTED_ORDER)//display if the lists are in sorted order or not
System.out.println("Sorted Lists...");
else
System.out.println("Unsorted Lists...");
//initialize the lists in sorted order
initLists(order);
//initialize the number of comparisons and total execution time
initComparisonsAndExecutionTime();
//sort the lists
sortLists();
//display the number of comparisons and total execution time
printComparisonsAndExecutionTime();
}
//initializes the lists with the same values
private static void initLists(int order) {
int number;
for(int i=0;i<SIZE;i++) {
if(order == SORTED_ORDER)
number = new Integer(i);
else
number = new Integer((int)(Math.random()*SIZE));
selArr[i] = number;
insArr[i] = number;
bubArr[i] = number;
quiArr[i] = number;
merArr[i] = number;
}
}
/*initializes the number
of comparisons and total execution time for each algorithm*/
private static void initComparisonsAndExecutionTime() {
Sorting.selectionSortComparisons = 0;
Sorting.insertionSortComparisons = 0;
Sorting.bubbleSortComparisons = 0;
Sorting.quickSortComparisons = 0;
Sorting.mergeSortComparisons = 0;
Sorting.selectionSortExecutionTime = 0L;
Sorting.insertionSortExecutionTime = 0L;
Sorting.bubbleSortExecutionTime = 0L;
Sorting.quickSortExecutionTime = 0L;
Sorting.mergeSortExecutionTime = 0L;
}
/*calls each sorting algorithm with the
corresponding lists to sort the values*/
private static void sortLists() {
Sorting.selectionSort(selArr);
Sorting.insertionSort(insArr);
Sorting.bubbleSort(bubArr);
Sorting.quickSort(quiArr);
Sorting.mergeSort(merArr);
}
/*displays the number of comparisons and total execution time
for each algorithm*/
private static void printComparisonsAndExecutionTime() {
System.out.println("----------------------");
System.out.printf("%-15s%15s%15s\n", "SORTING METHOD",
"COMPARISONS", "EXEC.TIME(ns)");
System.out.println("----------------------");
System.out.printf("%-15s%15d%15d\n", "Selection sort",
Sorting.selectionSortComparisons,
Sorting.selectionSortExecutionTime);
System.out.printf("%-15s%15d%15d\n", "Insertion sort",
Sorting.insertionSortComparisons,
Sorting.insertionSortExecutionTime);
System.out.printf("%-15s%15d%15d\n", "Bubble sort",
Sorting.bubbleSortComparisons,
Sorting.bubbleSortExecutionTime);
System.out.printf("%-15s%15d%15d\n", "Quick sort",
Sorting.quickSortComparisons,
Sorting.quickSortExecutionTime);
System.out.printf("%-15s%15d%15d\n", "Merge sort",
Sorting.mergeSortComparisons,
Sorting.mergeSortExecutionTime);
System.out.println("-----------------------\n\n");
}
}
公共类排序测试{
//创建3个常量值
静态最终int SIZE=1000;//列表中的值数
静态最终整型排序\u顺序=0;//用于排序列表
静态final int RANDOM_ORDER=1;//用于未排序的列表
//为每个排序算法创建列表
静态整数[]selArr=新整数[大小];
静态整数[]insArr=新整数[大小];
静态整数[]bubArr=新整数[大小];
静态整数[]quiArr=新整数[大小];
静态整数[]merArr=新整数[大小];
公共静态void main(字符串[]args){
//对已排序的列表执行排序
执行排序(排序顺序);
//对未排序的列表执行排序
执行排序(随机顺序);
//对未排序的列表执行排序
执行排序(随机顺序);
}
私有静态无效性能排序(整数顺序){
if(order==SORTED\u order)//显示列表是否按排序顺序排列
System.out.println(“排序列表…”);
其他的
System.out.println(“未排序的列表…”);
//按排序顺序初始化列表
初始列表(顺序);
//初始化比较次数和总执行时间
initComparisonAndExecutionTime();
//对列表进行排序
分类表();
//显示比较次数和总执行时间
PrintComparisonAndExecutionTime();
}
//使用相同的值初始化列表
私有静态void initlist(整数顺序){
整数;
对于(inti=0;i我尝试添加一些打印来帮助调试代码。我发现您的快速排序方法运行在一个无限循环中
结果是:
Sorted Lists...
Lists Initialized!
Comparisons and Exec time initialized!
Selection done!
Insertion done!
Bubble done!
当我对这行排序.快速排序(quiArr);
进行注释时,我得到了以下输出:
----------------------
SORTING METHOD COMPARISONS EXEC.TIME(ns)
----------------------
Selection sort 499500 2726141
Insertion sort 243586 0
Bubble sort 499500 0
Quick sort 0 0
Merge sort 8697 0
-----------------------
这使我检查了您的代码,以设置每个排序方法的执行时间。看起来像所有方法都设置了排序。selectionSortExecutionTime,您可以在mergeSort的以下代码段中看到:
public static <T extends Comparable<T>> void mergeSort(T[] data) {
long startTime = System.nanoTime();// stores
// beginning
// of
// merge
// sort
mergeSort(data, 0, data.length - 1);
long endTime = System.nanoTime();// stores
// end
// of
// merge
// sort
/* Wrong variable */
selectionSortExecutionTime = endTime - startTime;// computes
// total
// execution
// time
// of
// merge
// sort
}
希望我能帮上忙!谢谢你建议编辑Gurtug。如果你能帮助解决这个问题,那就太好了:)我们帮不上忙,因为有一半重要的代码丢失了。请阅读我认为在所有算法实现的地方发布代码太长了。如果我应该的话,我仍然可以把它放进去。顺便说一句:我希望你理解你的代码几乎完全是“过程性的”。这不是“错误”但是Java的思想是通常使用面向对象的编程风格。您不必发布所有代码。您可以构建一个简化的示例,我们可以完全运行它来查看问题。我知道您的排序类一定太大了,因为它没有遵循OOP思想…我为所有这些类都修复了执行时间变量。a当我评论quick sort out时,它们似乎都很有效!这真是太棒了,谢谢你让我走到这一步!但我仍然停留在quick sort上。有什么办法可以解决这个问题吗?我会快速搜索并尝试找到解决方法。我会尽快告诉你的!我发现了问题并编辑了答案。我不确定这是否是预期的,但是mergeSortComparisons与其他人相比非常低,可能您遗漏了一些。(很抱歉,如果应该是这样的话,我真的不知道合并排序是如何工作的)在我看到你今天的答案之前,我昨晚幸运地找到了答案。据我所知,是的,合并排序应该很低。仍然非常感谢你的帮助。你是一个救命恩人:)
----------------------
SORTING METHOD COMPARISONS EXEC.TIME(ns)
----------------------
Selection sort 499500 2726141
Insertion sort 243586 0
Bubble sort 499500 0
Quick sort 0 0
Merge sort 8697 0
-----------------------
public static <T extends Comparable<T>> void mergeSort(T[] data) {
long startTime = System.nanoTime();// stores
// beginning
// of
// merge
// sort
mergeSort(data, 0, data.length - 1);
long endTime = System.nanoTime();// stores
// end
// of
// merge
// sort
/* Wrong variable */
selectionSortExecutionTime = endTime - startTime;// computes
// total
// execution
// time
// of
// merge
// sort
}
private static <T extends Comparable<T>> int partition(T[] data,
int min,
int max) {
T partitionelement;
int left, right;
int middle = (min + max) / 2;
// use the middle data value as the
// partition element
partitionelement = data[middle];
// move it out of the way for now
swap(data, middle, min);
left = min;
right = max;
while (left < right) {
// search for an element that is >
// the partition element
while (left < right
&& data[left].compareTo(partitionelement) <= 0){
// counts the number of comparisons in quick sort
quickSortComparisons++;
left++; // <--- this was not in the while loop
}
quickSortComparisons++; // <--- I believe you should have this one too
// search for an element that is <
// the partition element
while (data[right].compareTo(partitionelement) > 0){
// counts the number of comparisons in quick sort
quickSortComparisons++;
right--; // <--- this wasn't in the while loop neither
}
// counts the number of comparisons in quick sort
quickSortComparisons++;
// swap the elements
if (left < right)
quickSortComparisons++;// counts the number of comparisons in quick sort
swap(data, left, right);
}
// move the partition element into place
swap(data, min, right);
return right;
}