学习记录
public class InsertSort {
public static void main(String[] args) {
Entity entity = new Entity(1);
Entity entity1 = new Entity(71);
Entity entity2 = new Entity(81);
Entity entity3 = new Entity(81);
Entity entity4 = new Entity(1456);
Entity entity5 = new Entity(41);
Entity entity6 = new Entity(156);
Entity entity7 = new Entity(14);
Entity entity8 = new Entity(781);
Entity entity9 = new Entity(781);
Entity entity10 = new Entity(85861);
List<Entity> entities = new ArrayList<>();
entities.add(entity);
entities.add(entity1);
entities.add(entity2);
entities.add(entity3);
entities.add(entity4);
entities.add(entity5);
entities.add(entity6);
entities.add(entity7);
entities.add(entity8);
entities.add(entity9);
entities.add(entity10);
InsertSort(entities);
System.out.println();
}
public static void InsertSort(List<Entity> entities){
if (entities == null || entities.size() == 0){
return;
}
int i = 1;
int j = 0;
while (i < entities.size()){
j = i;
while (j>0){
if (entities.get(j).compareTo(entities.get(j-1)) < 0){
Entity max = entities.get(j - 1);
Entity min = entities.get(j);
entities.set(j - 1,min);
entities.set(j,max);
}else {
break;
}
j--;
}
i++;
}
}
}
public class Entity implements Comparable<Entity> {
private int order;
Entity(int order){
this.order = order;
}
@Override
public int compareTo(Entity o) {
if (o == null){
return 0;
}
return this.order - o.order;
}
}