使用插入排序算法,使用来自对象数组的多个字段

时间:2015-09-26 06:24:09

标签: java arrays algorithm sorting insertion-sort

我必须使用状态对象对数组进行排序,并按区域编号和填充对其进行排序。我已从文本文件中导入此信息,因此这些是字符串。我可以使用其中一个字段进行排序,但无法使用它们。它总是最终排序方法中调用的最后一种排序。例如,在我的代码中,它只是对区域编号进行排序,从而对人口进行排序。无论如何都要按人口排序,然后从那种排序中对区域编号进行排序。另外,我不能使用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();

2 个答案:

答案 0 :(得分:1)

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

排序逻辑:

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个随机状态,打印它们,对它们进行排序并打印它们。 在输出中我们可以看到状态按人口正确排序,在具有相同人口的状态中,按地区编号对其进行“子排序”。

  

----国家未分类----
  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
  ----国家排序----
  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

答案 1 :(得分:0)

您可以声明实现Comparator的State类,如下所示:

  public class State implements Comparator<State> {
     // your fields
     // getter and setter;
    @Override
    public int compare(State o1, State o2) {
      int sort1 = o1.getPopulation().compareTo(o2.getPopulation());
      if (sort1 != 0) {
         return sort1;
      }
      int sort2 = o1.getRegionNumber().compareTo(o2.getRegionNumber());
      if (sort2 != 0) {
         return sort2;
      }
      else{
       // other fields as you like. 
      }
   }

 }