Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1589008
  • 博文数量: 43
  • 博客积分: 169
  • 博客等级: 入伍新兵
  • 技术积分: 1162
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-08 15:35
文章分类

全部博文(43)

文章存档

2021年(1)

2019年(4)

2016年(6)

2015年(8)

2013年(19)

2012年(5)

分类: Java

2013-07-01 19:48:13

LinkedList实现的插入式升/降序排序算法

如果有更好的见解,请不吝赐教,我将无比的感激~

点击(此处)折叠或打开

  1. /**
  2.  * 2013-7-1 下午07:18:53
  3.  */
  4. package com.cn.fanjg.arithmetic;

  5. import java.util.LinkedList;

  6. /**
  7.  * @function:
  8.  *


  9.  *      此排序算法是通过 java 中LinkedList实现的插入式升/降序排序算法
  10.  * 所谓插入式:一边向LinkedList中插入数据一边排序
  11.  * 以Integer类型为例
  12.  *


  13.  * com.cn.fanjg.arithmetic
  14.  * @author fanjg
  15.  * @date : 2013-7-1 下午07:18:53
  16.  */
  17. public class InsertSortOfLinkedList {
  18.     public static void main(String[] args) {
  19.         
  20.     }
  21.     /**
  22.      *
  23.      * @function:
  24.      *


  25.      *     升序算法含义:
  26.      * 1、首先判断新增元素和元素集合不能为null;
  27.      * 2、判断元素集合是否为空,如果为空则将当前新增元素插入到链表的表头
  28.      * 3、如果不是第二种情况则需要遍历整个链表,查看是否有已存在元素比当前新增元素大,
  29.      * 如果有则将当前元素插入到已存在元素的位置
  30.      * 4、如果遍历到最后一个位置没有比当前新增元素大,那么就将新增元素插入到链表的最后一个位置
  31.      *


  32.      * InsertSortOfLinkedList.java
  33.      * @param element
  34.      * @param elemList
  35.      * @throws Exception
  36.      * @author: fanjg
  37.      * @date : 2013-7-1 下午07:32:03
  38.      */
  39.     public static void upSort(Integer element,LinkedList<Integer> elemList) throws Exception{
  40.         if(element == null)
  41.         {
  42.             throw new Exception("插入元素为null");
  43.         }
  44.         if(elemList == null)
  45.         {
  46.             throw new Exception("元素集合为null");
  47.         }
  48.         if(elemList.size() == 0)
  49.         {
  50.             elemList.add(element);
  51.         }else{
  52.             if(element < elemList.getFirst())
  53.             {
  54.                 elemList.addFirst(element);
  55.             }else {
  56.                 boolean insertFlag = false;
  57.                 for (Integer e : elemList)
  58.                 {
  59.                     if(element < e)
  60.                     {
  61.                         int index = elemList.indexOf(e);
  62.                         elemList.add(index, element);
  63.                         insertFlag = true;
  64.                     }
  65.                 }
  66.                 if(!insertFlag)
  67.                 {
  68.                     elemList.addLast(element);
  69.                 }
  70.             }
  71.         }
  72.     }
  73.     /**
  74.      *
  75.      * @function:
  76.      *


  77.      * 降序同升序的设计思想相同
  78.      *


  79.      * InsertSortOfLinkedList.java
  80.      * @param element
  81.      * @param elemList
  82.      * @author: fanjg
  83.      * @date : 2013-7-1 下午07:37:37
  84.      */
  85.     public static void downSort(Integer element,LinkedList<Integer> elemList) throws Exception
  86.     {
  87.         if(element == null)
  88.         {
  89.             throw new Exception("插入元素为null");
  90.         }
  91.         if(elemList == null)
  92.         {
  93.             throw new Exception("元素集合为null");
  94.         }
  95.         if(elemList.size() == 0)
  96.         {
  97.             elemList.add(element);
  98.         }else{
  99.             if(element > elemList.getFirst())
  100.             {
  101.                 elemList.addFirst(element);
  102.             }else {
  103.                 boolean insertFlag = false;
  104.                 for (Integer e : elemList)
  105.                 {
  106.                     if(element > e)
  107.                     {
  108.                         int index = elemList.indexOf(e);
  109.                         elemList.add(index, element);
  110.                         insertFlag = true;
  111.                     }
  112.                 }
  113.                 if(!insertFlag)
  114.                 {
  115.                     elemList.addLast(element);
  116.                 }
  117.             }
  118.         }
  119.     }
  120. }

 

阅读(15191) | 评论(2) | 转发(1) |
给主人留下些什么吧!~~

hello_ketty2019-05-21 15:08:33

bluecase:elemList.indexOf(e);

这个操作的代价不小吧

这个函数代价却是非常大,而且取决于List中存储的对象对于equals方法的实现

回复 | 举报

bluecase2014-05-07 23:01:43

elemList.indexOf(e);

这个操作的代价不小吧