package myPackage; import java.util.LinkedList; /** * 此排序算法是通过 java 中LinkedList实现的插入式升/降序排序算法 * 所谓插入式:一边向LinkedList中插入数据一边排序 * <b>以Integer类型为例</b> * */ public class InsertSortLinkedList { public static void main(String[] args) { LinkedList<Integer> elemList = new LinkedList<Integer> (); try { long start = System.currentTimeMillis(); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(31, elemList); upSort(21, elemList); upSort(1, elemList); upSort(81, elemList); upSort(16, elemList); upSort(5, elemList); upSort(14, elemList); upSort(3, elemList); upSort(2, elemList); upSort(21, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(41, elemList); upSort(3, elemList); upSort(2, elemList); upSort(1, elemList); upSort(18, elemList); upSort(6, elemList); upSort(5, elemList); upSort(4, elemList); upSort(3, elemList); upSort(12, elemList); upSort(1, elemList); upSort(8, elemList); upSort(61, elemList); upSort(5, elemList); upSort(4, elemList); upSort(332, elemList); upSort(2, elemList); upSort(13, elemList); upSort(8, elemList); upSort(6, elemList); upSort(5, elemList); upSort(2344, elemList); upSort(3423, elemList); upSort(2, elemList); upSort(1, elemList); upSort(8, elemList); long end = System.currentTimeMillis(); System.out.println(end - start); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } for(Integer intgeter: elemList){ System.out.println(intgeter); } } /** * 升序算法含义: * 1、首先判断新增元素和元素集合不能为null; * 2、判断元素集合是否为空,如果为空则将当前新增元素插入到链表的表头 * 3、如果不是第二种情况则需要遍历整个链表,查看是否有已存在元素比当前新增元素大, * 如果有则将当前元素插入到已存在元素的位置 * 4、如果遍历到最后一个位置没有比当前新增元素大,那么就将新增元素插入到链表的最后一个位置 * */ public static void upSort(Integer element,LinkedList<Integer> elemList) throws Exception{ if(element == null) { throw new Exception("插入元素为null"); } if(elemList == null) { throw new Exception("元素集合为null"); } if(elemList.size() == 0) { elemList.add(element); }else{ if(element < elemList.getFirst()) { elemList.addFirst(element); }else { boolean insertFlag = false; for (Integer e : elemList) { if(element < e) { int index = elemList.indexOf(e); elemList.add(index, element); insertFlag = true; break; } } if(!insertFlag) { elemList.addLast(element); } } } } /** ** 降序同升序的设计思想相同 * */ public static void downSort(Integer element,LinkedList<Integer> elemList) throws Exception { if(element == null) { throw new Exception("插入元素为null"); } if(elemList == null) { throw new Exception("元素集合为null"); } if(elemList.size() == 0) { elemList.add(element); }else{ if(element > elemList.getFirst()) { elemList.addFirst(element); }else { boolean insertFlag = false; for (Integer e : elemList) { if(element > e) { int index = elemList.indexOf(e); elemList.add(index, element); insertFlag = true; } } if(!insertFlag) { elemList.addLast(element); } } } } }
LinkedList实现的插入式升/降序排序算法
最新推荐文章于 2024-07-31 14:20:29 发布