• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

介绍七种Java的常用排序方法

java 搞代码 4年前 (2022-01-09) 71次浏览 已收录 0个评论

这篇文章主要通过实例介绍本文来源gaodai#ma#com搞*!代#%^码网%了Java中常用的7种排序方法,需要的朋友可以参考下

直接插入排序

<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>

冒泡排序

<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>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:介绍七种Java的常用排序方法
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址