我将这段代码放在一起对数字进行排序。它可以工作,如果可以的话,我想变得更短,更干净。我正在使用两个扫描仪来扫描同一文件。我不太确定如何使用方法来接受输入文件。
import java.io.*;
public class test {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(new File("/Users/WalterCueva/Desktop/4.txt")); //provide file name from outside
int counter = 0; //keep track of how many iargs[0]ntegers in the file
while (scan.hasNextInt()) {
counter++;
scan.nextInt();
}
Scanner scan2 = new Scanner(new File("/Users/WalterCueva/Desktop/4.txt"));
int[] numbers = new int[counter];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scan2.nextInt(); //fill the array with the integers
}
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
; //fill the array with the integers
}
new test().sort(numbers);
System.out.println(Arrays.toString(numbers));
}
public void sort(int[] data) {
for (int i = 0; i < data.length - 1; i++) {
for (int j = 0; j < data.length - 1 - i; j++) {
// do the swap if required
if (data[j] > data[j+1]) {
int tmp = data[j+1];
data[j+1] = data[j];
data[j] = tmp;
}
}
}
}
}
答案 0 :(得分:0)
替换这部分代码:
int[] numbers = new int[counter];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scan2.nextInt(); //fill the array with the integers
}
for (int i = 0; i < numbers.length; i++) {
System.out.println(numbers[i]);
; //fill the array with the integers
}
new test().sort(numbers);
与此:
int[] numbers = new int[counter];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = scan2.nextInt(); //fill the array with the integers
System.out.println(numbers[i]);
}
Arrays.sort(numbers);
并放下public void sort(int[] data)
函数。
答案 1 :(得分:0)
我相信这是最短的时间。
首先,我们使用List
,它会随着添加更多数据而动态增长。这样可以防止您需要对数据进行两次迭代。
第二,我们使用内置函数Collections.sort()
,该函数使用Tim-sort。 Tim sort能够以最坏的时间复杂度N log (N)
对数据进行排序(与您的bubbleort实现N²
相比)。
import java.io.File;
import java.util.*;
public class test {
public static void main(String[] args) throws Exception {
try (Scanner scan = new Scanner(new File("text.txt"))) {
List<Integer> list = new ArrayList<Integer>();
while (scan.hasNextInt())
list.add(scan.nextInt());
Collections.sort(list);
System.out.println(list);
}
}
}
答案 2 :(得分:0)
通过使用纯Java 8内容,可以更轻松地完成此操作。看看这个:
public class TestClass {
private static final String FILE_LOCATION = "C:\\tmp\\file.txt";
public static void main(String[] args) {
List<Integer> lines = new ArrayList<>();
try (Stream<String> stream = Files.lines(Paths.get(FILE_LOCATION))) {
lines = stream.map(Integer::parseInt).collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
}
lines.sort(Comparator.naturalOrder());
lines.forEach(System.out::println);
}
}
答案 3 :(得分:0)
import java.io.*;
import java.util.*;
public class test {
public static void main(String[] args) throws Exception {
File file = new File ("/Users/WalterCueva/Desktop/4.txt");
if (!file.exists()) {
System.out.println("The file does not exist ");
System.exit(0);
}
Scanner input = new Scanner(file);
ArrayList <Integer>numbers = new ArrayList<>();
while(input.hasNext()) {
numbers.add( input.nextInt()); //fill the array with the integers
}
input.close();
for (double e: numbers) {
System.out.println(e);
}
java.util.Collections.sort(numbers);
System.out.println(numbers);
}}