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

1 篇文章 0 订阅
1 篇文章 0 订阅
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);
                }
            }
        }
    }

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值