序言
前几篇文章中我们分别介绍了JAVA数据结构和算法-简单排序之冒泡排序、JAVA数据结构和算法-简单排序之选择排序和JAVA数据结构和算法-简单排序之插入排序。在实际使用过程当中,很少会使用单一基础数据类型,更多的是应用于对象排序。本文将基于Person对象以lastname(姓)为关键字来排序。实现的算法为以上三种算法中最高效的插入排序算法。
![b5d40e25b405af74f95639011c4d02f8.png](https://i-blog.csdnimg.cn/blog_migrate/fb0564d62f7cd8bbd10c50162a171f73.jpeg)
lastname关键字排序
注意:下文中没有Person.java文件的实现,各位看官可以自己创建一个Person类进行实操。Person类包含三个字段lastname(String),fasetname(String),age(Integer)。实现有参构造和getter、setter方法即可。
对象排序的JAVA代码
import org.apache.commons.lang3.StringUtils;import org.zhibo.commons.lang.test.algorithm.array.model.Person;/** * Created by Bruce on 2020/8/13 * 以Person对象为元素进行插入排序 **/public class InsertSortForPerson_09 { private Person[] a; private Integer nElems; /** * 数组初始化大小 * @param maxSize */ public InsertSortForPerson_09(int maxSize) { this.a = new Person[maxSize]; this.nElems = 0; } public Boolean insertPerson(String lastName, String firstName, Integer age){ if(StringUtils.isBlank(lastName) || StringUtils.isBlank(firstName) || age == null || age.intValue() < 0){ return false; } return insertPerson(new Person(lastName, firstName, age)); } /** * 插入成功返回true * 插入失败返回false * person is not Null and person.lastName is not Null(inclue "" or " ") * @param person * @return */ public Boolean insertPerson(Person person){ if(person == null || StringUtils.isBlank(person.getLastName())){ return false; } if(nElems.intValue() == a.length){//元素大小已经等于最大值因此不可以进行继续插入 return false; } a[nElems] = person; nElems++; return true; } public Integer getnElems(){ return nElems.intValue(); } public Integer getMaxSize(){ return a.length; } public void display(){ for(int i = 0; i < nElems.intValue(); i++){ a[i].displayPerson(); } System.out.println(" "); } public void insertionSort(){ if(nElems.intValue() <= 1){//只有一个元素不需要排序 return; } for(int i = 1; i < nElems; i++){ Person temp = a[i]; int j = i; while (j > 0 && a[j - 1].getLastName().compareTo(temp.getLastName()) > 0){ a[j] = a[j - 1]; j--; } a[j] = temp; } } public static void main(String[] args) { InsertSortForPerson_09 array = new InsertSortForPerson_09(100); array.insertPerson("Evans", "Patty", 24); array.insertPerson("Smith", "Doc", 59); array.insertPerson("Smith", "Lorraine", 37); array.insertPerson("Smith", "Paul", 37); array.insertPerson("Yee", "Tom", 43); array.insertPerson("Hashimoto", "Sato", 21); array.insertPerson("Stimson", "Henry", 29); array.insertPerson("Velasquez", "Jose", 72); array.insertPerson("Vang", "Minh", 22); array.insertPerson("Creswell", "Lucinda", 18); System.out.println("befor sorted"); array.display(); array.insertionSort(); System.out.println("after sorted"); array.display(); }}
输出结果:
befor sortedPerson{lastName='Evans', firstName='Patty', age=24}Person{lastName='Smith', firstName='Doc', age=59}Person{lastName='Smith', firstName='Lorraine', age=37}Person{lastName='Smith', firstName='Paul', age=37}Person{lastName='Yee', firstName='Tom', age=43}Person{lastName='Hashimoto', firstName='Sato', age=21}Person{lastName='Stimson', firstName='Henry', age=29}Person{lastName='Velasquez', firstName='Jose', age=72}Person{lastName='Vang', firstName='Minh', age=22}Person{lastName='Creswell', firstName='Lucinda', age=18} after sortedPerson{lastName='Creswell', firstName='Lucinda', age=18}Person{lastName='Evans', firstName='Patty', age=24}Person{lastName='Hashimoto', firstName='Sato', age=21}Person{lastName='Smith', firstName='Doc', age=59}Person{lastName='Smith', firstName='Lorraine', age=37}Person{lastName='Smith', firstName='Paul', age=37}Person{lastName='Stimson', firstName='Henry', age=29}Person{lastName='Vang', firstName='Minh', age=22}Person{lastName='Velasquez', firstName='Jose', age=72}Person{lastName='Yee', firstName='Tom', age=43}
单词排序
![68611372e3a29352611b3a5daa08e3f0.png](https://i-blog.csdnimg.cn/blog_migrate/ff6827235f5771652590c0cf13b3d314.jpeg)
单词排序
例如,s1是“cat”,s2是“dog”,方法返回值小于0.在实例代码程序中,这个方法用于比较a[n-1]的lastname和temp的lastname字段。
稳定性
有些时候,排序要考虑数据项拥有相关关键字的情况。例如,雇员数据按雇员的姓的字段序排序(排序以姓为关键字),现在又想按邮编排序,并希望具有相同邮政编码的数据仍然按姓排序。这种情况下,则只需要算法对需要排序的数据进行排序,让不需要的数据保持原来的顺序。某些算法满足这种的要求,它们就可以称为稳定的算法。
本文中的所有算法都是稳定的。例如在上面的实例代码中,有三个人的姓叫做Smith.顺序为Doc Smith、Lorraine Smith、Paul Smith。经过排序后,这个顺序还依然存在。但不同的Smith对象已经移动到了各自的新位置上。
![e2b297dab788faf769cf8a3365fd023c.png](https://i-blog.csdnimg.cn/blog_migrate/2d94f730cc2bcb11fffd1fdbf101155b.jpeg)
几种算法之间的比较
JAVA数据结构和算法-简单排序之冒泡排序 JAVA数据结构和算法-简单排序之选择排序 JAVA数据结构和算法-简单排序之插入排序 几种算法之间的比较。
![a9f337a0307c81fe1b2de92c76010def.png](https://i-blog.csdnimg.cn/blog_migrate/e54e397410125956ea1988b2184092df.jpeg)
本文摘要自《Java数据结构和算法(第二版)》