Sorting 当我有一个包含很多数字的ArrayList时,程序会终止而不给我任何错误消息

Sorting 当我有一个包含很多数字的ArrayList时,程序会终止而不给我任何错误消息,sorting,Sorting,我的任务是做7种排序方法。当我用数字和字符串测试我的所有排序方法时,它们都有效,但当我将教授在文本文件中给我的50000多个数字添加到arrayList时,程序终止,甚至不会告诉我我的错误。我做错了什么?为什么会这样?(他给了我5个文本文件,一个有50000个随机数,一个有250000个随机数) 导入java.awt.List; 导入java.time.Duration; 导入java.time.Instant; 导入java.io.File; 导入java.io.FileNotFoundExc

我的任务是做7种排序方法。当我用数字和字符串测试我的所有排序方法时,它们都有效,但当我将教授在文本文件中给我的50000多个数字添加到arrayList时,程序终止,甚至不会告诉我我的错误。我做错了什么?为什么会这样?(他给了我5个文本文件,一个有50000个随机数,一个有250000个随机数)

导入java.awt.List;
导入java.time.Duration;
导入java.time.Instant;
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.io.FileReader;
导入java.util.ArrayList;
导入java.util.Scanner;
公共班机{
公共静态void main(字符串[]args)引发FileNotFoundException{
//TODO自动生成的方法存根
java.io.File file1=新的java.io.File(“50000.txt”);
java.io.File file2=新的java.io.File(“100000.txt”);
java.io.File file3=新的java.io.File(“150000.txt”);
java.io.File file4=新的java.io.File(“200000.txt”);
java.io.File file5=新的java.io.File(“250000.txt”);
扫描仪输入1=新扫描仪(文件1);
扫描仪输入2=新扫描仪(文件2);
扫描仪输入3=新扫描仪(文件3);
扫描仪输入4=新扫描仪(文件4);
扫描仪输入5=新扫描仪(文件5);
ArrayList ListofInteger50000=新的ArrayList();
ArrayList ListofIntegrater100000=新的ArrayList();
ArrayList ListofInteger150000=新的ArrayList();
ArrayList ListofIntegrater200000=新的ArrayList();
ArrayList ListofInteger250000=新的ArrayList();
while(input1.hasNext()){
整数=input1.nextInt();
整数列表50000。添加(数字);
}
while(input2.hasNext()){
整数=input2.nextInt();
整数列表100000。添加(数字);
}
while(input3.hasNext()){
整数=input3.nextInt();
整数列表150000。添加(数字);
}
while(input4.hasNext()){
整数=input4.nextInt();
整数列表200000。添加(数字);
}
while(input5.hasNext()){
整数=input5.nextInt();
积分列表250000。添加(数字);
}
System.out.println(“开始对文件50000.txt使用所有七种sorth方法”);
即时开始=即时。现在();
排序。插入排序(整数列表50000);
瞬间结束=瞬间。现在();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序。bubbleSort(积分列表50000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序。选择排序(整数列表50000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序。合并排序(整数列表50000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序。快速排序(整数列表50000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序。计数排序(整数列表50000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序。radixSort(整数列表50000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
System.out.println(“这是文件50000.txt使用的所有七种排序方法和时间”);
System.out.println(“开始对文件100000.txt使用所有七种sorth方法”);
start=Instant.now();
排序。插入排序(整数列表100000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序:bubbleSort(10万个整数列表);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序。选择排序(整数列表100000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序。合并排序(整数列表100000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序。快速排序(整数列表100000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序。计数排序(整数列表100000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
start=Instant.now();
排序。radixSort(整数列表100000);
end=Instant.now();
System.out.println(持续时间间隔(开始、结束));
System.out.println(“这是一个
import java.awt.List;
    import java.time.Duration;
    import java.time.Instant;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Main {
    
        public static void main(String[] args) throws FileNotFoundException {
            // TODO Auto-generated method stub
            
            java.io.File file1 = new java.io.File("50000.txt");
            java.io.File file2 = new java.io.File("100000.txt");
            java.io.File file3 = new java.io.File("150000.txt");
            java.io.File file4 = new java.io.File("200000.txt");
            java.io.File file5 = new java.io.File("250000.txt");
            
            
            Scanner input1 = new Scanner(file1);
            Scanner input2 = new Scanner(file2);
            Scanner input3 = new Scanner(file3);
            Scanner input4 = new Scanner(file4);
            Scanner input5 = new Scanner(file5);
            
            ArrayList<Integer> listOfIntegers50000 = new ArrayList<>();
            ArrayList<Integer> listOfIntegers100000 = new ArrayList<>();
            ArrayList<Integer> listOfIntegers150000 = new ArrayList<>();
            ArrayList<Integer> listOfIntegers200000 = new ArrayList<>();
            ArrayList<Integer> listOfIntegers250000 = new ArrayList<>();
    
            while (input1.hasNext()) {
                Integer numbers = input1.nextInt();
                listOfIntegers50000.add(numbers);
                
            }
            
            while (input2.hasNext()) {
                Integer numbers = input2.nextInt();
                listOfIntegers100000.add(numbers);
                
            }
            
            while (input3.hasNext()) {
                Integer numbers = input3.nextInt();
                listOfIntegers150000.add(numbers);
                
            }
            
            while (input4.hasNext()) {
                Integer numbers = input4.nextInt();
                listOfIntegers200000.add(numbers);
                
            }
            
            while (input5.hasNext()) {
                Integer numbers = input5.nextInt();
                listOfIntegers250000.add(numbers);
                
            }
            
            
            System.out.println("Beginning to use all seven of the sorth methods for the file 50000.txt");
        
            Instant start = Instant.now();
            Sorting.insertionSort(listOfIntegers50000);
            Instant end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.bubbleSort(listOfIntegers50000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.selectionSort(listOfIntegers50000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.mergeSort(listOfIntegers50000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.quickSort(listOfIntegers50000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.countingSort(listOfIntegers50000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.radixSort(listOfIntegers50000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            System.out.println("This is all seven of the sort methods used and time for the file 50000.txt");
            
            
            System.out.println("Beginning to use all seven of the sorth methods for the file 100000.txt");
            
            start = Instant.now();
            Sorting.insertionSort(listOfIntegers100000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.bubbleSort(listOfIntegers100000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.selectionSort(listOfIntegers100000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.mergeSort(listOfIntegers100000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.quickSort(listOfIntegers100000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.countingSort(listOfIntegers100000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.radixSort(listOfIntegers100000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            System.out.println("This is all seven of the sort methods used and time for the file 10000.txt");
            
            System.out.println("Beginning to use all seven of the sorth methods for the file 150000.txt");
            
            start = Instant.now();
            Sorting.insertionSort(listOfIntegers150000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.bubbleSort(listOfIntegers150000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.selectionSort(listOfIntegers150000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.mergeSort(listOfIntegers150000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.quickSort(listOfIntegers150000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.countingSort(listOfIntegers150000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.radixSort(listOfIntegers150000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            System.out.println("This is all seven of the sort methods used and time for the file 150000.txt");
            
            System.out.println("Beginning to use all seven of the sorth methods for the file 200000.txt");
            
            start = Instant.now();
            Sorting.insertionSort(listOfIntegers200000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.bubbleSort(listOfIntegers200000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.selectionSort(listOfIntegers200000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.mergeSort(listOfIntegers200000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.quickSort(listOfIntegers200000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.countingSort(listOfIntegers200000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.radixSort(listOfIntegers200000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            System.out.println("This is all seven of the sort methods used and time for the file 200000.txt");
            
            System.out.println("Beginning to use all seven of the sorth methods for the file 250000.txt");
            
            start = Instant.now();
            Sorting.insertionSort(listOfIntegers250000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.bubbleSort(listOfIntegers250000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.selectionSort(listOfIntegers250000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.mergeSort(listOfIntegers250000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.quickSort(listOfIntegers250000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.countingSort(listOfIntegers250000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            start = Instant.now();
            Sorting.radixSort(listOfIntegers250000);
            end = Instant.now();
            System.out.println(Duration.between(start, end));
            
            System.out.println("This is all seven of the sort methods used and time for the file 250000.txt"); 
            
            
            
            
            
            
            
        
            
    
            
            
            
            
            
        
            
            
            
    
        }
    
    }
    
    
    
    
    import java.util.ArrayList;
    import java.util.Collections;
    
    import com.sun.tools.classfile.Opcode.Set;  
    
    
    
    public class Sorting {
    
        //constructor
        private Sorting() {
        }
        
        static <E extends Comparable <E>> void insertionSort(ArrayList<E> arrayList){
            int i;
            int j;
            E temp;
        
            
            
            //index 0 has nothing to compare to so we start at 1
            for(i = 1; i <= arrayList.size()-1; i++) {
                
                //holds the value starting at index 1
                temp = arrayList.get(i);
                j = i;
                
                while (j > 0 && temp.compareTo(arrayList.get(j-1)) < 0) {
                    arrayList.set(j, arrayList.get(j-1));
                    j--;
                    
                }
                
                arrayList.set(j, temp);
                
            }
            System.out.println("Sorted by Insertion Sort: " + arrayList);
        }
        
        static <E extends Comparable <E>> void bubbleSort(ArrayList<E> arrayList){
            int i;
            int j;
            boolean swapped;
            
            
            for(i = 1; i <= arrayList.size()-1; i++) {
                swapped = false;
                for(j =0; j <arrayList.size()-2; j++) {
                    
                    if (arrayList.get(j).compareTo(arrayList.get(j+1))>0) {
                        swap(arrayList, j, j+1);
                        swapped=true;
                    }
                    
                }
                if (!swapped) {
                    break;
                }
            }
            
            System.out.println("Sorted by Bubble Sort: " + arrayList);
        }
        
        static <E extends Comparable <E>> void selectionSort(ArrayList<E> arrayList){
            int i;
            int j;
            int min;
            
            for(i=0; i< (int)arrayList.size(); i++) {
                min=i;
                for(j=i+1; j < arrayList.size(); j++) {
                    if (arrayList.get(j).compareTo(arrayList.get(min))<0){
                        min = j;
                    }
                }
                swap(arrayList, i, min);
            }
            
            System.out.println("Sorted by Selection Sort: " + arrayList);
        }
        
        static <E extends Comparable <E>> void mergeSort(ArrayList<E> arrayList){
            ArrayList<E> left = new ArrayList<>();
            ArrayList<E> right = new ArrayList<>();
            int mid;
            int size=arrayList.size();
            
            if (arrayList.size()<=1) {
                return;
            }else {
                mid = (arrayList.size()/2);
                
                
                
                for (int count = 0; count == mid; count++) {
                    left.add(arrayList.get(count));
                }
                mergeSort(left);
                
                
                for (int count = mid+1; count < arrayList.size(); count++) {
                    right.add(arrayList.get(count));
                }
                mergeSort(right);
                
                merge(left, right, arrayList);
    
            }
            
    
            System.out.println("Sorted by Merge Sort: " + arrayList);
            
            
        }
        
        static <E extends Comparable <E>> void merge(ArrayList<E> list1, ArrayList<E> list2, ArrayList<E> result) {
            int i = 0;
            int j =0;
            int k = 0;
            
            
            
            while (i < list1.size() && j < list2.size()) {
                if (list1.get(i).compareTo(list2.get(j))>0) {
                    result.set(k,list1.get(i));
                }else {
                    result.set(k,list2.get(j));
                    j++;
                }
                k++;
            }
            
            while (i < list1.size()) {
                result.set(k, list1.get(i));
                    i++;
                    k++;
            }
            
            while (i < list1.size()) {
                result.set(k, list2.get(j));
                j++;
                k++;
            }
            
    
            
            
        
        }
        
        
        static <E extends Comparable <E>> void quickSort(ArrayList<E> arrayList){
            quickSort(arrayList, 0, arrayList.size()-1);
            System.out.println("Sorted by Quick Sort:" + arrayList);
        }
        
        static <E extends Comparable <E>> void quickSort(ArrayList<E> arrayList, int low, int high){
            if (low >= high){
                return;
            }else{
                if (low<high) {
                    int p = partition(arrayList, low, high);
                    quickSort(arrayList, low, p-1);
                    quickSort(arrayList,p+1, high);
                }
            }
            
                    
        }
        
        static <E extends Comparable <E>> int partition(ArrayList<E> arrayList, int low, int high){
            E pivot = arrayList.get(high);
            int i= low-1;
            
            for(int j=low; j > high-1; j++) {
                if (arrayList.get(j).compareTo(pivot) >0) {
                    i=i +1;
                    swap(arrayList, i, j);
                }
            swap(arrayList, i+1,high);
    
            }
            return i+1;
        
        }
        
        //counting and radix will not use generics //Hint think if there are any required generic bounds
        //k should be ten since our max value is 9
        public static void countingSort(ArrayList<Integer> arrayList){
            int k = Collections.max(arrayList)+1;
            
            ArrayList<Integer> result = new ArrayList<>();
            ArrayList<Integer> counts = new ArrayList<>();
            int num=0; 
            
            for (int i = 0; i < arrayList.size() ; i++) {
                result.add(i, 0);
            }
            
            //so we make the size of it k-1
            for (int i = 0; i <= k-1; i++) {
                counts.add(i, 0);
                
            }
    
            
            for (int i = 0; i < arrayList.size(); i++) {
                //this number will be use as an index for count
                int listNum = arrayList.get(i);
                //default the entire list should be zero and for every occurence of a number an increment of 1
                counts.set(listNum, counts.get(listNum)+1);
            } 
            
            
            for (int i = 1; i <= k-1; i++) {
                int prevNum = counts.get(i-1);
                int currentNum = (counts.get(i));
                int added = prevNum+currentNum;
    
                counts.set(i, added);
            }
            
            for (int i = arrayList.size()-1; i >= 0; i--) {
                int listIndex = arrayList.get(i);
                int countIndex = counts.get(listIndex);
                int resultIndex = countIndex-1;
    
                result.set(resultIndex, listIndex);
                counts.set(listIndex, counts.get(listIndex)-1);
                
                
            }
            System.out.println("Sorted by Counting Sort: " + result);
        }
        
        public static void radixSort(ArrayList<Integer> arrayList){
            ArrayList<Integer> bucket0 = new ArrayList<>();
            ArrayList<Integer> bucket1 = new ArrayList<>();
            ArrayList<Integer> bucket2 = new ArrayList<>();
            ArrayList<Integer> bucket3 = new ArrayList<>();
            ArrayList<Integer> bucket4 = new ArrayList<>();
            ArrayList<Integer> bucket5 = new ArrayList<>();
            ArrayList<Integer> bucket6 = new ArrayList<>();
            ArrayList<Integer> bucket7 = new ArrayList<>();
            ArrayList<Integer> bucket8 = new ArrayList<>();
            ArrayList<Integer> bucket9 = new ArrayList<>();
            
    
            
    
            
            //max value in list
            int max = Collections.max(arrayList);
            int countMaxDigit = 0;
            //convert to string
            String s = Integer.toString(max);
            
            //count and get number of digits
            for (int i = 0, len = s.length(); i < len; i++) {
                if (Character.isDigit(s.charAt(i))) {
                    countMaxDigit++;
                }
            }
            
    
            int key;
            int pow =1;
            
            
            for (int i = 1; i <= countMaxDigit; i++) {
                
                for (int j = 0; j <= arrayList.size()-1;j++) {
                    //the lowest significant digit will be put in key
                    key = getLSD(arrayList.get(j), pow);
                    
                    //so if depending on the number of the key is where the number will go
                    //in the bucket
                    switch(key) {
                        case 0:
                            bucket0.add(arrayList.get(j));
                            break;
                        case 1:
                            bucket1.add(arrayList.get(j));
                            break;
                        case 2:
                            bucket2.add(arrayList.get(j));
                            break;
                        case 3:
                            bucket3.add(arrayList.get(j));
                            break;
                        case 4:
                            bucket4.add(arrayList.get(j));
                            break;
                        case 5:
                            bucket5.add(arrayList.get(j));
                            break;
                        case 6:
                            bucket6.add(arrayList.get(j));
                            break;
                        case 7:
                            bucket7.add(arrayList.get(j));
                            break;
                        case 8:
                            bucket8.add(arrayList.get(j));
                            break;
                        case 9:
                            bucket9.add(arrayList.get(j));
                            break;
                            
                    }
    
                }
                pow++;
                
                //clears the arrayList becaue we are going to add the numbers from the buckets back but in different order 
                //this time
                arrayList.clear();
        
                
                //the plan is to go thru the entire list and add the element into the arrayList.
                for(int count = 0; count < bucket0.size();count++) {
                    if (bucket0.size() == 0) {
                        break;
                    }else {
                        arrayList.add(bucket0.get(count));
                    }
                    
                }
                bucket0.clear();
                
                for(int count = 0; count < bucket1.size();count++) {
                    if (bucket1.size() == 0) {
                        break;
                    }else {
                        arrayList.add(bucket1.get(count));
                    }
                    
                }
                bucket1.clear();
                
                for(int count = 0; count < bucket2.size();count++) {
                    if (bucket2.size() == 0) {
                        break;
                    }else {
                        arrayList.add(bucket2.get(count));
                    }
                    
                }
                bucket2.clear();
                
                for(int count = 0; count < bucket3.size();count++) {
                    if (bucket3.size() == 0) {
                        break;
                    }else {
                        arrayList.add(bucket3.get(count));
                    }
                    
                }
                bucket3.clear();
                
                for(int count = 0; count < bucket4.size();count++) {
                    if (bucket4.size() == 0) {
                        break;
                    }else {
                        arrayList.add(bucket4.get(count));
                    }
                    
                }
                bucket4.clear();
                
                for(int count = 0; count < bucket5.size();count++) {
                    if (bucket5.size() == 0) {
                        break;
                    }else {
                        arrayList.add(bucket5.get(count));
                    }
                    
                }
                bucket5.clear();
                
                for(int count = 0; count < bucket6.size();count++) {
                    if (bucket6.size() == 0) {
                        break;
                    }else {
                        arrayList.add(bucket6.get(count));
                    }
                    
                }
                bucket6.clear();
                
                for(int count = 0; count < bucket7.size();count++) {
                    if (bucket7.size() == 0) {
                        break;
                    } else {
                        arrayList.add(bucket7.get(count));
                    }
                    
                }
                bucket7.clear();
                
                for(int count = 0; count < bucket8.size();count++) {
                    if (bucket8.size() == 0) {
                        break;
                    }else {
                        arrayList.add(bucket8.get(count));
                    }
                    
    
                }
                bucket8.clear();
                
                for(int count = 0; count < bucket9.size();count++) {
                    if (bucket9.size() == 0) {
                        break;
                    }
                    arrayList.add(bucket9.get(count));
                }
                bucket9.clear();
                
                }
            
            System.out.println("Sorted by Radix Sort: " + arrayList);
        }
            
        //my swap method so i can save time
        static <E extends Comparable <E>>void swap(ArrayList<E> arrayList, int i, int j){
            E temp = arrayList.get(i);
            arrayList.set(i,arrayList.get(j));
            arrayList.set(j, temp);
            
        }
        
        public static int getLSD(int number, int pow){
            
            //so every loop it increase the number by one and is pass thru this method. the increment goes to the second least sig dif and so on.
            return (int)((number % (Math.pow(10,pow)))/(Math.pow(10, pow-1)));
        }
        
        
    }