gpt4 book ai didi

java - 使用插入排序算法,使用对象数组中的多个字段

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:02:16 27 4
gpt4 key购买 nike

我必须对包含状态对象的数组进行排序,并按地区编号和人口对其进行排序。我从一个文本文件中导入了这些信息,所以这些都是字符串。我可以使用其中一个字段进行排序,但无法同时使用这两个字段。它总是只对方法中调用的最后一个排序进行排序。例如,在我的代码中,它只是对区域编号进行排序,从而对人口进行排序。反正有没有按人口排序,然后从那个排序,排序区域编号。另外,我不能使用 java.util 中的任何东西。

public void insertionSort()
{
int in, out;

for (out = 1; out < getElementCount(); out++)
{
State temp = states[out];
in = out;

while (in > 0 && states[in - 1].getPopulation().compareTo(temp.getPopulation()) > 0)
{
states[in] = states[in - 1];
--in;
}
states[in] = temp;

}
for (out = 1; out < getElementCount(); out++)
{
State temp = states[out];
in = out;

while (in > 0 && states[in - 1].getRegionNumber().compareTo(temp.getRegionNumber()) > 0)
{
states[in] = states[in - 1];
--in;
}
states[in] = temp;
}
}




public void Execute()
{
StateCollection sc = new StateCollection();

String filename = "States.Search.txt";
String file = "States.Input.txt";

String[] stateSearch = new String[15];
String[] state = new String[50];


stateSearch = readFile(filename, stateSearch);
state = readFile(file, state);

for (int i = 0; i < state.length; i++)
{

String stateName = state[i].substring(0, 15).trim();
String stateCapital = state[i].substring(15, 30).trim();
String abbr = state[i].substring(30, 32).trim();
String population = state[i].substring(32, 40).trim();
String region = state[i].substring(40, 55).trim();
String regionNumber = state[i].substring(55).trim();

State s = new State(stateName, stateCapital, abbr, population, region, regionNumber);

sc.add(i, s);


}


sc.bubbleSort();

最佳答案

将您的问题分解成更容易解决的小问题。

排序逻辑:

public void insertionSort() {
int in, out;

for (out = 1; out < getElementCount(); out++) {
State temp = states[out];
in = out;

while (in > 0 && compare(states[in-1], temp) > 0) {
states[in] = states[in - 1];
--in;
}
states[in] = temp;

}
}

比较逻辑:

int compare( State a, State b )
{
int comparePopulation = a.getPopulation().compareTo(b.getPopulation());
if ( comparePopulation!=0 )
return comparePopulation;
else
return a.getRegionNumber().compareTo(b.getRegionNumber());
}

首先按人口排序,然后按地区编号排序。

下面是一个Minimal, Complete, and Verifiable example

import java.util.concurrent.ThreadLocalRandom;

class Test {

static int compare(State a, State b) {
int comparePopulation = a.getPopulation().compareTo(b.getPopulation());
if (comparePopulation != 0) {
return comparePopulation;
} else {
return a.getRegionNumber().compareTo(b.getRegionNumber());
}
}

static void insertionSort() {
int in, out;

for (out = 1; out < getElementCount(); out++) {
State temp = states[out];
in = out;

while (in > 0 && compare(states[in - 1], temp) > 0) {
states[in] = states[in - 1];
--in;
}
states[in] = temp;

}
}

static class State {

private final Integer population;
private final Integer regionNumber;

public State(int population, int regionNumber) {
this.population = population;
this.regionNumber = regionNumber;
}

public static State getRandomState() {
return new State(ThreadLocalRandom.current().nextInt(10, 14),
ThreadLocalRandom.current().nextInt(1, 4));
}

public Integer getRegionNumber() {
return regionNumber;
}

public Integer getPopulation() {
return population;
}

@Override
public String toString() {
return "P=" + population + " R=" + regionNumber;
}
}

static int getElementCount() {
return states.length;
}

static State[] states;

public static void main(String[] args) {
// Create 10 random states
states = new State[10];
for (int n = 0; n < 10; ++n) {
states[n] = State.getRandomState();
}
// Print them
System.out.println("----States unsorted----");
for (State s : states) {
System.out.println(s);
}
// Sort
insertionSort();
// Print them sorted
System.out.println("----States sorted----");
for (State s : states) {
System.out.println(s);
}
}

它生成 10 个随机状态,打印它们,对它们进行排序并打印它们已排序。在输出中,我们可以看到各州按人口排在第一位,而在人口相同的州中,它们按地区编号进行“子排序”。

----States unsorted----
P=10 R=1
P=10 R=2
P=12 R=1
P=11 R=2
P=11 R=2
P=13 R=1
P=12 R=2
P=13 R=1
P=10 R=2
P=12 R=1
----States sorted----
P=10 R=1
P=10 R=2
P=10 R=2
P=11 R=2
P=11 R=2
P=12 R=1
P=12 R=1
P=12 R=2
P=13 R=1
P=13 R=1

关于java - 使用插入排序算法,使用对象数组中的多个字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32794313/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com