在我的程序中,我有一个类排序,它输入一个名为CountryUnsortedFormat的文件,其中包含国家及其人口的随机列表。该类应该使用 shellsort 按人口对国家/地区进行排序并将它们显示在屏幕上。
这是我的代码:
package assignment3;
import java.io.PrintWriter;
import java.io.File;
import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;
public class Assignment3 {
public static void main(String[] args) throws Exception{
//Array for handling list of countries
String[] line = new String[238];
//read list of countries into array
readInArray(line);
//unsort the array
unSort(line);
}
static void readInArray(String[] line) throws Exception{
Scanner stdIn = new Scanner(new File("C:/Users/Vicki/Desktop/CountrySortedFormat.txt"));
//Read in countries from sorted file into an array
int k=0;
while (stdIn.hasNextLine()){
line[k]=stdIn.nextLine();
k++;
}
}
static void unSort(String[] line) throws Exception{
PrintWriter out = new PrintWriter("C:/Users/Vicki/Desktop/CountryUnsortedFormat.txt");
//Pick a random int from 1 to 238 called where
//Write where into Unsorted Country Format file
//Make where null
//Repeat until all 238 countries are written in random order
int j = line.length-1;
Random r = new Random();
while (j > 0){
int where = r.nextInt(j)+1;
out.println(line[where]);
line = pop(where, line);
j--;
}
out.close();
}
static String[] pop(int index, String[] line){
String[] newLine = new String[line.length-1];
int offset = 0;
for (int i = 0; i<line.length; i++){
if(i == index){
offset = 1;
continue;
}
newLine[i - offset] = line[i];
}
return newLine;
}
}
class Sorting {
public static void main(String[] args) throws Exception{
//Array for handling list of countries
String[] line = new String[238];
readInArray(line);
shellsort(line);
System.out.println(Arrays.toString(line));
}
static void readInArray(String[] line) throws Exception{
Scanner stdIn = new Scanner(new File("C:/Users/Vicki/Desktop/CountryUnsortedFormat.txt"));
//Read in countries from unsorted file into an array
int k=0;
while (stdIn.hasNextLine()){
line[k]=stdIn.nextLine();
k++;
}
}
static void shellsort(String[] line){
int j;
for(int gap = line.length-1/2; gap > 0; gap /= 2){
for(int i = gap; i < line.length-1; i++){
String tmp = line[i];
for (j = i; j >= gap && getPopulation(line, j-gap) > getPopulation(line, i); j -= gap){
line[j] = line[j -gap];
}
line[j] = tmp;
}
}
}
static int getPopulation(String[] line, int index){
String populationString = line[index].substring(50,65).trim().replaceAll(",","");
int population = Integer.parseInt(populationString);
return population;
}
}
我的课程是分开工作的,但是当把我的程序放在一起时,我的程序不会打印到屏幕上。它显示的只是"构建成功(总时间:0 秒(">
我做错了什么?
这显然是一个家庭作业,进一步帮助你是错误的。我会首先将您的代码组织成更具可读性的格式。例如:
public class Country{
private int population;
public Country(String line){
// parse and set population
}
public int getPopulation(){
return population;
}
}
然后实现排序
# Sort an array countires[0...n-1].
# Start with the largest gap and work down to a gap of 1
int j;
for(int gap = countries.length/2; gap > 0; gap /= 2){
# Do a gapped insertion sort for this gap size.
# The first gap elements countries[0..gap-1] are already in gapped order
# keep adding one more element until the entire array is gap sorted
for(int i = gap; i < line.length; i++){
# add countries[i] to the elements that have been gap sorted
# save countries[i] in temp and make a hole at position i
int temp = countries[i].getPopulation();
# shift earlier gap-sorted elements up until the correct location for countries[i] is found
for (j = i; j >= gap && a[j - gap].getPopulation() > temp; j -= gap){
countries[j] = countries[j - gap]
}
# put temp (the original countries[i]) in its correct location
countries[j] = temp
}
}
这或多或少是直接从维基百科中撕下来的,而你拥有什么......
这看起来像是需要更好调试的情况。在填充line
中删除一些断点或一些println
。Netbeans 可能没有打印任何内容,因为没有要打印的内容。或者你可能只是搞砸了你的解析。您的子字符串中有硬编码的值,很可能这就是它中断的地方。使用正则表达式或扫描器可以解决这个问题,只需不是一个邪恶的函数集群。我不知道你的输入是什么样的,所以我不能告诉你。但是,我知道您的排序有效,因为我使用一些随机值复制和粘贴并自己运行它:
class Sorting {
public static void main(String[] args) throws Exception{
int[] line = new int[]{97,95,66,91,33,91,73,63,67,84,40,34,85,43,73,8,45,14,86,23,74,22,50,33,4,75,12,28,44,43,20,69,95,28,8,44,5,21,50,53,83,53,93,4,62,45,24,57,41,30,32,21,44,76,42,85,35,36,20,96,95,35,5,49,21,43,29,97,69,15,40,15,82,73,24,30,53,50,73,2,86,25,35,50,83,15,66,80,36,22,46,34,89,18,15,59,99,85,12,65};
int j;
for(int gap = line.length/2; gap > 0; gap /= 2){
for(int i = gap; i < line.length; i++){
int population = line[i];
for (j = i; j >= gap && line[j - gap] > population; j -= gap){
line[j] = line[j -gap];
}
line[j] = population;
}
}
for (int l : line) {
System.out.println(l);
}
}
}
缺少错误日志听起来像是一个环境问题,或者可能归结为您未排序的文件是什么样子的,这很可能是错误的,因为我试图回答您之前的问题,您已经删除了这个问题
使用随机 int 遍历数组并取消文件排序
因此,代码的困难在于您最终不会得到一个混乱的数组。相反,您可能会有重复项。让我们用一个包含 4 个lines = ["Argentina","Barbados","Canada","Dominica"]
的数组来运行它
迭代 1。 j = 4
,让我们有where = 2
,因此out = "Canada"
迭代 2. j = 3
,没有什么可以阻止where = 2
,out = "Canada, Canada"
为了正确打乱数组,我建议弹出所选值以防止重复。鉴于您使用的是普通的旧数组而不是 ArrayList,您应该有一个函数弹出(尽管我建议使用 ArrayList(:
// pop(2,["Argentina","Barbados","Canada","Dominica"]) == ["Argentina","Barbados","Dominica"]
function String[] pop(int index, String[] list){
newList = new String[list.length - 1]
int offset = 0;
for(int i; i < list.length; i++){
if(i == index){
offset = 1; // Start skipping
continue;
}
newList[i - offset] = list[i];
}
return newList;
}
您的新取消排序应如下所示:
public static void unSort(String[] line) throws Exception{
Scanner stdIn = new Scanner(new File("C:/Users/Vicki/Desktop/CountryUnsortedFormat.txt"));
PrintWriter out = new PrintWriter("C:/Users/Vicki/Desktop/CountryUnsortedFormat.txt");
int j = line.length; // Opposed to hardcoding 238
Random r = new Random(); // Let's put random out so we don't have to continuously initialize
while (j > 0){
int where = r.nextInt(j);
out.println(line[where]);
line = pop(where, line); // get rid of what we just printed
j--;
}
out.close();
}
当然还有其他洗牌,例如:你可以在line
上表演费舍尔-耶茨,然后打印出来。我不完全明白你直接要求什么,但我想它是这样的:
public static void unSort(String[] line) throws Exception{
Scanner stdIn = new Scanner(new File("C:/Users/Vicki/Desktop/CountryUnsortedFormat.txt"));
PrintWriter out = new PrintWriter("C:/Users/Vicki/Desktop/CountryUnsortedFormat.txt");
int j = line.length; // Opposed to hardcoding 238
Random r = new Random(); // Let's put random out so we don't have to continuously initialize
while (j > 0){
int where = r.nextInt(line.length);
if(line[where] != null){
out.println(line[where]);
j--;
line[where] = null;
}
}
out.close();
}
这保证没有重复。但是,这的最佳运行时间为 O(n(,最坏情况是无限循环。