所以我正在编写一个程序,当用户在数组中输入元素的数量时,它将计算每种算法将使用的移动和比较的次数(快速排序,HeapSort,插入,选择,基数)。我创建了一个小型控制台,教授实际上为排序算法提供了代码,目的是让我们比较和对比基于排序算法的比较和交换次数。
当我尝试从插入类中的类中获取数据时遇到问题,并且不断收到异常线程。任何帮助都会有所帮助。
public class Project1
{
public static void main(String args[])
{
@SuppressWarnings("resource")
Scanner input = new Scanner(System.in);
int[] array = {};
int sortAlg;
int arrSize;
{
System.out.print("Select how many elements in your array (1 - 999999): ");
arrSize = input.nextInt();
if (arrSize < 1 || arrSize > 999999)
{
System.out.println("The size of your array is invalid");
System.out.print("Please select how many elements in your array: ");
arrSize = input.nextInt();
}
if(arrSize > 1 || arrSize < 999999)
{
{
System.out.println("Enter Preferred Sorting Algorithm: \n"
+ "1. Insertion Sort\n"
+ "2. Selection Sort\n"
+ "3. Quick Sort\n"
+ "4. Merge Sort\n"
+ "5. Heap Sort\n"
+ "6. Radix Sort\n"
+ "7. Exit");
System.out.print("Please Enter Your selection: ");
do
{
sortAlg = input.nextInt();
if (sortAlg > 6 || sortAlg < 1)
{
System.out.println("The number you have selected is invalid");
System.out.println("Please Enter Your selection: ");
}
}while(sortAlg > 7 || sortAlg < 1);
if (sortAlg == 1)
{
SortingAlgorithms sortAlgorithms;
sortAlgorithms = new SortingAlgorithms();
String[] info = sortAlgorithms.insertionSort();
System.out.print(info[0]);
}
else if(sortAlg == 2)
{
}
else if(sortAlg == 3)
{
}
else if(sortAlg == 4)
{
}
else if (sortAlg == 5)
{
}
else if (sortAlg == 6)
{
}
}
}
}
}
static class SortingAlgorithms
{
public static int comparisonsCount = 0;
public static int movementsCount = 0;
static String information[] = new String[6];
static long startTime = 0, endTime = 0;
//InsertionSort operations
public static String[] insertionSort(int array[], String arrayType)
{
comparisonsCount = 0;
movementsCount = 0;
information = new String[6];
startTime = System.currentTimeMillis();
array = insertionSort(array);
endTime = System.currentTimeMillis();
information[0] = String.valueOf(array.length);
information[1] = arrayType;
information[2] = "Insertion";
information[3] = String.valueOf(comparisonsCount);
information[4] = String.valueOf(movementsCount);
information[5] = String.valueOf(endTime - startTime);
return information;
}
线程“主”中的异常java.lang.NullPointerException 在sortingAlgorithms.Project1.main(Project1.java:51)