认为问题出在我的主要课程中,我试图添加一个扫描仪和一个文件阅读器。代码使用我已经证明的设置号码,一旦你删除我试图实现的扫描仪。我需要它读取不同的整数填充txt文件。
public class SortAll
{
private int[] sorArr;
private int[] MergArr;
private int length;
public static int[] SelectionSort(int[] OrigArr)
{
for (int i = 0; i < OrigArr.length - 1; i++)
{
int loc = i;
for (int j = i + 1; j < OrigArr.length; j++)
if (OrigArr[j] < OrigArr[loc])
loc = j;
int small = OrigArr[loc];
OrigArr[loc] = OrigArr[i];
OrigArr[i] = small;
}
return OrigArr;
}
public static int[] InsertionSort(int[] OrigArr)
{
int temp;
for (int i = 1; i < OrigArr.length; i++)
{
for(int j = i ; j > 0 ; j--)
{
if(OrigArr[j] < OrigArr[j-1])
{
temp = OrigArr[j];
OrigArr[j] = OrigArr[j-1];
OrigArr[j-1] = temp;
}
}
}
return OrigArr;
}
public void sort(int originalArr[]) {
this.sorArr = originalArr;
this.length = originalArr.length;
this.MergArr = new int[length];
MergeSorting(0, length - 1);
}
private void MergeSorting(int low, int high) {
if (low < high) {
int mid = low + (high - low) / 2;
// Sorting left side of sorArr in the below step
MergeSorting(low, mid);
// Sorting right side of sorArr in the below step
MergeSorting(mid + 1, high);
// Merging both parts
MergingParts(low, mid, high);
}
}
private void MergingParts(int low, int mid, int high) {
for (int i = low; i <= high; i++) {
MergArr[i] = sorArr[i];
}
int i = low;
int j = mid + 1;
int k = low;
while (i <= mid && j <= high) {
if (MergArr[i] <= MergArr[j]) {
sorArr[k] = MergArr[i];
i++;
} else {
sorArr[k] = MergArr[j];
j++;
}
k++;
}
while (i <= mid) {
sorArr[k] = MergArr[i];
k++;
i++;
}
}
public void quick(int[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.sorArr = inputArr;
length = inputArr.length;
quickSort(0, length - 1);
}
private void quickSort(int low, int high) {
int i = low;
int j = high;
//In the below step, we are calculating pivot number
int pivot = sorArr[low+(high-low)/2];
// Dividing the Array into two Arrays.
while (i <= j) {
while (sorArr[i] < pivot) {
i++;
}
while (sorArr[j] > pivot) {
j--;
}
if (i <= j) {
exchNum(i, j);
//Moving index to next position of the array
i++;
j--;
}
}
// calling quickSort() recursively
if (low < j)
quickSort(low, j);
if (i < high)
quickSort(i, high);
}
private void exchNum(int i, int j) {
int temp = sorArr[i];
sorArr[i] = sorArr[j];
sorArr[j] = temp;
}
public static void main(String[] args)
{
SortAll sortArr=new SortAll();
long start;
long end;
long total;
int[] outputArr;
int[] randomNumArr={5,32,23,47,89,11,19,36,77,66};
int[] equalNumArr={4,4,4,4,4,4,4,4,4,4};
int[] sortedIncArr={11,22,33,44,55,66,77,88,99,111};
int[] sortedDecArr={111,99,88,77,66,55,44,33,22,11};
//Running Selection Sort on Random Array
start=System.nanoTime();
outputArr=SelectionSort(randomNumArr);
end=System.nanoTime();
total=end-start;
System.out.println("Runtime For Selection Sort :\n");
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Random Array : "+total+" nanoseconds.\n");
//Running Selection Sort on Equal Array
start=System.nanoTime();
outputArr=SelectionSort(equalNumArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Equal Array : "+total+" nanoseconds.\n");
//Running Selection Sort on Sorted Array Increasing order
start=System.nanoTime();
outputArr=SelectionSort(sortedIncArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Increasing Order Array : "+total+" nanoseconds.\n");
//Running Selection Sort on Sorted Array Decreasing order
start=System.nanoTime();
outputArr=SelectionSort(sortedDecArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Decreasing Order Array : "+total+" nanoseconds.\n");
//Running Insertion Sort
start=System.nanoTime();
outputArr=InsertionSort(randomNumArr);
end=System.nanoTime();
total=end-start;
System.out.println("\nRuntime For Insertion Sort :\n");
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Random Array : "+total+" nanoseconds.\n");
//Running Selection Sort on Equal Array
start=System.nanoTime();
outputArr=InsertionSort(equalNumArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Equal Array : "+total+" nanoseconds.\n");
//Running Selection Sort on Sorted Array Increasing order
start=System.nanoTime();
outputArr=InsertionSort(sortedIncArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Increasing Order Array : "+total+" nanoseconds.\n");
//Running Selection Sort on Sorted Array Decreasing order
start=System.nanoTime();
outputArr=InsertionSort(sortedDecArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Decreasing Order Array : "+total+" nanoseconds.\n");
//Runtime for Merge Sort
System.out.println("\nRuntime For Merge Sort :\n");
outputArr=randomNumArr;
start=System.nanoTime();
sortArr.sort(outputArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Random Array : "+total+" nanoseconds.\n");
outputArr=randomNumArr;
start=System.nanoTime();
sortArr.sort(outputArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Equal Array : "+total+" nanoseconds.\n");
outputArr=randomNumArr;
start=System.nanoTime();
sortArr.sort(outputArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Increasing Order Array : "+total+" nanoseconds.\n");
outputArr=randomNumArr;
start=System.nanoTime();
sortArr.sort(outputArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Decreasing Order Array : "+total+" nanoseconds.\n");
//Runtime FOr QuickSort
System.out.println("\nRuntime For Quick Sort :\n");
outputArr=randomNumArr;
start=System.nanoTime();
sortArr.quick(outputArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Random Array : "+total+" nanoseconds.\n");
outputArr=randomNumArr;
start=System.nanoTime();
sortArr.quick(outputArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Equal Array : "+total+" nanoseconds.\n");
outputArr=randomNumArr;
start=System.nanoTime();
sortArr.quick(outputArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Increasing Order Array : "+total+" nanoseconds.\n");
outputArr=randomNumArr;
start=System.nanoTime();
sortArr.quick(outputArr);
end=System.nanoTime();
total=end-start;
for(int i:outputArr){
System.out.print(i);
System.out.print(" ");
}
System.out.println();
System.out.print("--Sorted in Decreasing Order Array : "+total+" nanoseconds.\n");
}
}
答案 0 :(得分:0)
// provide file object to Scanner
Scanner s = new Scanner(new File("C:\\Users\\dsingh70\\Desktop\\temp\\SortDatafile.txt"));
// if you have inputs (like 5,32,23,47,89,11,19,36,77,66) in file(comma
// separated)
// you need to reset the delimiter using below method;
// s.useDelimiter(",");
// if you have a numbers file with space separated (like 5 32 23 47 89
// 11 19 36 77 66) you don't need above s.useDelimiter(",") method,
// because space is default Delimiter in Scanner
// iterate while file have integers
int num;
while (s.hasNextInt()) {
num = s.nextInt();
// provide your business logic
System.out.println(num);
}