您的位置:首页 > 编程语言 > Java开发

java算法之插入排序

2016-12-06 16:06 218 查看
Android面试时经常也会被问到数据结构算法的问题,最近有时间,写点实例。

我模拟的场景是对一个班上学生成绩进行排名。

插入排序原理:

     将数组分成两部分,第一部分是有序的,第二部分是无序的。假设数组长度为N,那么把第一个元素当作是有序的第一部分,其他N-1个元素则为第二部分。从第二部分里依次取元素插到第一部分有序数组合适的地方。这样有序部分越来越长,无序部分越来越短,最后就都成有序的了。

1.Person.Java 实体类

package com.demo.sort;

public class Person {

/**
* 名字
*/
public String name;

/**
* 分数
*/
public int score;
public Person(String name, int score) {
super();
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public String toString() {
return ""+score+"";
}
}
2.数据准备

ackage com.demo.sort;
public class DataUtil {
Person person1 = new Person("张一",60);
Person person2 = new Person("张二",82);
Person person3 = new Person("张三",23);
Person person4 = new Person("张四",31);
Person person5 = new Person("张五",55);
Person person6 = new Person("张六",89);
Person person7 = new Person("张七",92);
Person person8 = new Person("张八",48);
Person person9 = new Person("张九",50);
Person person10 = new Person("张十",23);

Person [] persons = {
person1,person2,person3,person4,person5,
person6,person7,person8,person9,person10
};
public Person[] getData(){
return persons;
}
}
3. 插入排序

package com.demo.sort;
/**
* 冒泡排序
* @author http://blog.csdn.net/nnmmbb *
*/
public class SortDemo {
public static void main(String args[]){
Person [] persons = new DataUtil().getData();
beforeSort(persons);
//插入排序
inSertSort(persons);
}
/**
* 直接插入排序
*/
private static void inSertSort(Person [] persons){
for(int i = 1;i<persons.length;i++){
//取第i个元素,插到0->i-1个元素的数组中
Person temp = persons[i];

for(int j = i-1;j>=0;j--){
if(temp.score>=persons[j].score){
System.out.println("insert "+ temp.score+" to "+(j+1)+" position");
persons[j+1]= temp;
break;
}else{
System.out.println("move "+ persons[j]+" to "+(j+1)+" position");
persons[j+1] = persons[j];

//		    		System.out.println("position "+ j+" = "+temp);
//		    		persons[j]=temp;
}

if(j == 0){
persons[j] = temp;
}
}
afterSort(i,persons);
System.out.println();

}
}

private static void afterSort(int index,Person[] persons) {
System.out.print("round " +index+ " sort:");
for(int i=0;i<persons.length;i++){
if(i == index){
System.out.print(" ["+ persons[i].toString() +"]");
}else{
System.out.print(" "+persons[i].toString());
}
}
System.out.println();
}
private static void beforeSort(Person[] persons) {
System.out.print("before sort:");
for(int i=0;i<persons.length;i++){
System.out.print(" "+persons[i].toString());
}
System.out.println();
System.out.println();
}
}


4.排序结果演示

before sort: 60 82 23 31 55 89 92 48 50 23

insert 82 to 1 position
round 1 sort: 60 [82] 23 31 55 89 92 48 50 23

move 82 to 2 position
move 60 to 1 position
round 2 sort: 23 60 [82] 31 55 89 92 48 50 23

move 82 to 3 position
move 60 to 2 position
insert 31 to 1 position
round 3 sort: 23 31 60 [82] 55 89 92 48 50 23

move 82 to 4 position
move 60 to 3 position
insert 55 to 2 position
round 4 sort: 23 31 55 60 [82] 89 92 48 50 23

insert 89 to 5 position
round 5 sort: 23 31 55 60 82 [89] 92 48 50 23

insert 92 to 6 position
round 6 sort: 23 31 55 60 82 89 [92] 48 50 23

move 92 to 7 position
move 89 to 6 position
move 82 to 5 position
move 60 to 4 position
move 55 to 3 position
insert 48 to 2 position
round 7 sort: 23 31 48 55 60 82 89 [92] 50 23

move 92 to 8 position
move 89 to 7 position
move 82 to 6 position
move 60 to 5 position
move 55 to 4 position
insert 50 to 3 position
round 8 sort: 23 31 48 50 55 60 82 89 [92] 23

move 92 to 9 position
move 89 to 8 position
move 82 to 7 position
move 60 to 6 position
move 55 to 5 position
move 50 to 4 position
move 48 to 3 position
move 31 to 2 position
insert 23 to 1 position
round 9 sort: 23 23 31 48 50 55 60 82 89 [92]

5. 拓展内容

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息