java算法之插入排序
2016-12-06 16:06
218 查看
Android面试时经常也会被问到数据结构和算法的问题,最近有时间,写点实例。
我模拟的场景是对一个班上学生成绩进行排名。
插入排序原理:
将数组分成两部分,第一部分是有序的,第二部分是无序的。假设数组长度为N,那么把第一个元素当作是有序的第一部分,其他N-1个元素则为第二部分。从第二部分里依次取元素插到第一部分有序数组合适的地方。这样有序部分越来越长,无序部分越来越短,最后就都成有序的了。
1.Person.Java 实体类
4.排序结果演示
5. 拓展内容
我模拟的场景是对一个班上学生成绩进行排名。
插入排序原理:
将数组分成两部分,第一部分是有序的,第二部分是无序的。假设数组长度为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. 拓展内容
相关文章推荐
- 算法导论(一)2、算法基础-插入排序 java
- java常用排序算法之插入排序
- Java -- 算法小结之插入排序
- java基础算法之插入排序
- 一点一点的积累算法JAVA版之插入排序
- Java再学习-算法之插入排序
- 数据算法之插入排序(insertSort)的Java实现
- 算法之排序小结1---插入排序(Java)
- 算法学习-----插入排序(Java)
- JavaScript ,Python,Go,java,C#系列算法之【插入排序】篇
- java实现排序算法之插入排序(直接插入排序,折半插入排序,希尔排序)
- java基本算法总结(冒泡排序、选择排序、插入排序)
- 8大内部排序算法学习笔记--(1)插入排序java实现
- <四>java数据结构与算法 插入排序
- 【算法拾遗(java描写叙述)】--- 插入排序(直接插入排序、希尔排序)
- 算法实现Java之插入排序
- 疯狂的Java算法——插入排序,归并排序以及并行归并排序
- java 算法基础之六插入排序与冒泡排序
- 算法—插入排序(Java实现)
- 几种常见排序算法之Java实现(插入排序、希尔排序、冒泡排序、快速排序、选择排序、归并排序)