• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > Java 7大常见排序方法实例详解

Java 7大常见排序方法实例详解

作者: 字体:[增加 减小] 来源:互联网 时间:2017-05-28

通过本文主要向大家介绍了java泛型详解,java集合类详解,java多线程详解,java线程池详解,java集合框架详解等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

直接插入排序

<code class="language-java hljs ">import java.util.HashMap; 
public class InsertSort { 
 private static int contrastCount = 0;//对比次数 
 private static int swapCount = 0;//交换次数 
 public static void main(String[] args) { 
  System.out.println("直接插入排序:\n 假设前面的序列都已经排序好了,把后面未排序的数往已排好序的序列内插入,时间复杂度O(n^2),空间复杂度O(1),稳定排序"); 
  HashMap<integer,integer> hashMap = new HashMap<integer,integer>(); 
  init(hashMap);//初始化  
  System.out.println("初始序列为:"); 
  print(hashMap, 0);//打印 
  insert(hashMap);//排序 
 } 
 /** 
  * 初始化函数 
  * @param hashMap 
  */ 
 private static void init(HashMap<integer, integer=""> hashMap) { 
  hashMap.put(0, null);//第一位置空 
  hashMap.put(1, 0); 
  hashMap.put(2, 5); 
  hashMap.put(3, 11); 
  hashMap.put(4, 12); 
  hashMap.put(5, 13); 
  hashMap.put(6, 4); 
  hashMap.put(7, 1); 
  hashMap.put(8, 5); 
  hashMap.put(9, 8); 
  hashMap.put(10, 6); 
  hashMap.put(11, 4); 
  hashMap.put(12, 8);  
 } 
 /** 
  * 进行插入排序 
  * @param hashMap 待排序的表 
  */ 
 private static void insert(HashMap<integer, integer=""> hashMap){ 
  System.out.println("开始插入排序:"); 
  int i,j; 
  //排序开始时间 
  long start = System.currentTimeMillis();  
  for(i=2; i<hashmap.size(); author="" code="" contrastcount="0;//对比次数" count="1;//只为统计执行次数" d="1,时间复杂度o(n^1.3),空间复杂度o(1),不稳定排序");" end="System.currentTimeMillis();" h2="" hashmap="" hhf="" hillsort="" i="1;" id="希尔排序" import="" int="" integer="" j="" long="" n="" param="" pre="" private="" public="" start="System.currentTimeMillis();" static="" swapcount="0;//交换次数" void="" x="1;x<=d;x++){//一共有d组"></hashmap.size();></integer,></integer,></integer,integer></integer,integer></code>
</div>

冒泡排序

<code class="language-java hljs "><code class="language-java hljs ">import java.util.HashMap; 
/** 
 * 冒泡排序 
 * @author HHF 
 * 2014年3月19日 
 */ 
public class BubbleSort { 
 private static int contrastCount = 0;//对比次数 
 private static int swapCount = 0;//交换次数 
 public static void main(String[] args) { 
  System.out.println("冒泡排序:\n 第一轮使最大值沉淀到最底下,采用从头开始的两两比较的办法,如果i大于i++则交换,下一次有从第一个开始循环,比较次数减一,然后依次重复即可," 
    + "\n 如果一次比较为发生任何交换,则可提前终止,时间复杂度O(n^2),空间复杂度O(1),稳定排序");   
  HashMap<integer,integer> hashMap = new HashMap<integer,integer>(); 
  init(hashMap);//初始化  
  System.out.println("初始序列为:"); 
  print(hashMap, 0);//打印 
  bubble(hashMap);//排序 
 } 
 /** 
  * 初始化函数 
  * @param hashMap 
  */ 
 private static void init(HashMap<integer, integer=""> hashMap) { 
  hashMap.put(0, null);//第一位置空 
  hashMap.put(1, 10); 
  hashMap.put(2, 5); 
  hashMap.put(3, 11); 
  hashMap.put(4, 12); 
  hashMap.put(5, 13); 
  hashMap.put(6, 4); 
  hashMap.put(7, 1); 
  hashMap.put(8, 5); 
  hashMap.put(9, 8); 
  hashMap.put(10, 6); 
  hashMap.put(11, 4); 
  hashMap.put(12, 8);  
 } 
 /** 
  * 进行冒泡排序 
  * @param hashMap 待排序的表 
  */ 
 private static void bubble(HashMap<integer, integer=""> hashMap){ 
  System.out.println("开始冒泡排序:"); 
  //排序开始时间 
  long start = System.currentTimeMillis(); 
  boolean swap = false;//是否发生交换 
  int count = 1;//只为了计数 
  for(int i=1; i<hashmap.size(); int="" j="1;" swap="false;">hashMap.get(j+1)){//需要发生交换j 和 j+1 
     hashMap.put(0, hashMap.get(j)); 
     hashMap.put(j, hashMap.get(j+1)); 
     hashMap.put(j+1, hashMap.get(0)); 
     swap = true; 
     contrastCount++;//发生一次对比 
     swapCount++;//发生一次交换 
     swapCount++;//发生一次交换 
     swapCount++;//发生一次交换 
    } 
    contrastCount++;//跳出if还有一次对比 
   } 
   print(hashMap, count++); 
   if(!swap) 
    break; 
  }  
  //排序结束时间 
  long end = System.currentTimeMillis(); 
  System.out.println("结果为:"); 
  print(hashMap, 0);//输出排序结束的序列 
  hashMap.clear();//清空 
  System.out.print("一共发生了 "+contrastCount+" 次对比\t"); 
  System.out.print("一共发生了 "+swapCount+" 次交换\t"); 
  System.out.println("执行时间为"+(end-start)+"毫秒"); 
 } 
 /** 
  * 打印已排序好的元素 
  * @param hashMap 已排序的表 
  * @param j 第j趟排序 
  */ 
 private static void print(HashMap<integer, integer=""> hashMap, int j){ 
  if(j != 0) 
   System.out.print("第 "+j+" 趟:\t"); 
  for(int i=1; i<hashmap.size(); code=""></hashmap.size();></integer,></hashmap.size();></integer,></integer,></integer,integer></integer,integer></code></code>
</div>

快速排序

<code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs ">import java.util.HashMap; 
public class QuickSort { 
 private static int contrastCount = 0;//对比次数 
 private static int swapCount = 0;//交换次数 
 public static void main(String[] args) { 
  System.out.println("快速排序:\n 任取一个数作为分界线,比它小的放到左边,比它大的放在其右边,然后分别对左右进行递归,时间复杂度O(nLgn),空间复杂度O(n),不稳定排序");  
  HashMap<integer,integer> hashMap = new HashMap<integer,integer>(); 
  init(hashMap);//初始化  
  System.out.println("初始序列为:"); 
  print(hashMap, 0, 0);//打印 
  System.out.println("开始快速排序:");  
  //排序开始时间 
  long start = System.currentTimeMillis(); 
  quick(hashMap, 1, hashMap.size()-1);//排序 
  //排序结束时间 
  long end = System.currentTimeMillis(); 
  System.out.println("结果为:"); 
  print(hashMap, 0, 0);//输出排序结束的序列 
  hashMap.clear();//清空 
  System.out.print("一共发生了 "+contrastCount+" 次对比\t"); 
  System.out.print("一共发生了 "+swapCount+" 次交换\t"); 
  System.out.println("执行时间为"+(end-start)+"毫秒"); 
  System.out.println("(注:此输出序列为代码执行过程的序列, 即先左边递归 再 右边递归, 而教科书上的递归序列往往是左右同时进行的结果,特此说明)"); 
 } 
 /** 
  * 初始化函数 
  * @param hashMap 
  */ 
 private static void init(HashMap<integer, integer=""> hashMap) { 
  hashMap.put(0, null);//第一位置空 
  hashMap.put(1, 10); 
  hashMap.put(2, 5); 
  hashMap.put(3, 11); 
  hashMap.put(4, 12); 
  hashMap.put(5, 13); 
  hashMap.put(6, 4); 
  hashMap.put(7, 1); 
  hashMap.put(8, 5); 
  hashMap.put(9, 8); 
  hashMap.put(10, 6); 
  hashMap.put(11, 4); 
  hashMap.put(12, 8);  
 } 
 /** 
  * 进行快速排序 
  * @param hashMap 待排序的表 
  * @param low 
  * @param high 
  */ 
 static int count = 1; 
 private static void quick(HashMap<integer, integer=""> hashMap, int low, int high){ 
  if(low<high){ hashmap="" high="" int="" integer="" k="quickOnePass(hashMap," low="" param="" private="" static=""> hashMap, int low, int high){  
  hashMap.put(0, hashMap.get(low));//选择一个分界值 此时第low位元素内的值已经无所谓被覆盖了 
  swapCount++;//发生一次交换 
  while(low<high){ high="">hashMap.get(low)){//开始从左往右走 直到有不对的数据 或者 到头了   
    low++; 
    contrastCount++;//发生一次对比 
   } 
   if(low<high){ hashmap="" integer="" j="" k="" param="" private="" return="" static="" void=""> hashMap, int j, int k){ 
  if(j != 0) 
   System.out.print("第 "+j+" 趟:(分界线为"+k+")\t"); 
  for(int i=1; i<hashmap.size(); code=""></hashmap.size();></high){></high){></high){></integer,></integer,></integer,integer></integer,integer></code></code></c



 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • Java 回调函数详解及使用
  • 常用Java排序算法详解
  • Java创建内部类对象实例详解
  • 详解Java动态加载数据库驱动
  • Java中泛型使用实例详解
  • Java中的对象和引用详解
  • java回调机制实例详解
  • java服务端微信APP支付接口详解
  • Java定时任务详解
  • Java实现一个达达租车系统的步骤详解

相关文章

  • 2017-05-28Eclipse智能提示及快捷键
  • 2017-05-28servlet监听实现统计在线人数功能 附源码下载
  • 2017-05-28servlet实现用户登录小程序
  • 2017-05-28Kotlin 基础语法详细介绍
  • 2017-05-28Java vector的详解及实例
  • 2017-05-28TreeSet详解和使用示例_动力节点Java学院整理
  • 2017-05-28Linux centos7环境下jdk安装教程
  • 2017-05-28JAVAEE中用Session简单实现购物车功能示例代码
  • 2017-05-28java中ArrayList与LinkedList对比详情
  • 2017-05-28Mybatis 一对多和多对一关联查询问题

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • java实现上传图片并压缩图片大小功能
    • Java高级特性
    • spring boot和mybatis集成分页插件
    • spring boot整合RabbitMQ实例详解(Fanout模式)
    • Java中Runnable和Thread的区别分析
    • 详解Spring MVC事务配置
    • spring MVC + bootstrap实现文件上传示例(带进度条)
    • Java 中的HashMap详解和使用示例_动力节点Java学院整理
    • SpringMVC接收页面表单参数
    • Java压缩解压zip技术_动力节点Java学院整理

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有