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>

快速排序

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

直接选择排序

<code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs ">import java.util.HashMap;
public class SelectionSort {
 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, 0);//打印
  select(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 select(HashMap<integer, integer=""> hashMap){
  System.out.println("开始选择排序:");
  //排序开始时间
  long start = System.currentTimeMillis();
  int count = 1;//只为统计执行次数
  for(int i=hashMap.size()-1; i>1; i--){//需要循环查询的次数 最后一个元素不用考虑
   int k = i;//记录本次查找序列最大值的下标 初始为该数应该要放的位置
   for(int j=1; j<i; code="" i="1;" int="" j=""></i;></integer,></integer,></integer,integer></integer,integer></code></code></code></code>

堆排序

<code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs ">import java.util.HashMap;
public class HeapSort {
 private static int contrastCount = 0;//对比次数
 private static int swapCount = 0;//交换次数
 private static int printCount = 1;//执行打印次数
 public static void main(String[] args) {
  System.out.println("堆排序:\n 首先建立一个堆(方法是首先把序列排成二叉树,然后从下往上,从右往左使得每一个小子树中的父节点大于子节点,然后从上往下,从左往右记录堆入序列),"
    + "\n 然后把堆的根节点和最底下 的孩子节点交换,整理堆,再重复交换,整理,时间复杂度O(nlgn),空间复杂度O(1),不稳定排序");
  HashMap<integer,integer> hashMap = new HashMap<integer,integer>();
  init(hashMap);//初始化
  System.out.println("初始序列为:");
  print(hashMap, 0);//打印
  heap(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 heap(HashMap<integer, integer=""> hashMap){
  System.out.println("开始建堆:");
  //排序开始时间87
  long start = System.currentTimeMillis();
  for(int i=(hashMap.size()-1)/2; i>=1; i--){//开始建堆
   sift(hashMap, i, hashMap.size()-1);//把所有的节点调好位置即可以
  }
  System.out.println("建堆成功");
  for(int j=hashMap.size()-1; j>=1; j--){//每次都把第一个元素与最后一个未排序的交换 然后再调整第一个节点即可
   hashMap.put(0, hashMap.get(1));
   hashMap.put(1, hashMap.get(j));
   hashMap.put(j, hashMap.get(0));
   sift(hashMap, 1, j-1);//剩下要排序的数位为j-1
   swapCount++;//发生一次交换
   swapCount++;//发生一次交换
   swapCount++;//发生一次交换
  }
  //排序结束时间
  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)+"毫秒");
 }
 /**
  * 把第i位的元素移动到合适位置 与其子孩子的最大值比较 如果有发生交换 则继续往下比较
  * @param hashMap
  * @param i 待移动的数下标
  * @param n 表示要查找的范围 从1到n个
  */
 private static void sift(HashMap<integer, integer=""> hashMap, int i, int n){
  int j = 2*i;//j为i的左孩子
  hashMap.put(0, hashMap.get(i));//当前被待排序的数
  while(j<=n){//如果有左孩子的
   if(hashMap.containsKey(j+1) && hashMap.get(j)<hashmap.get(j+1)){ else="" hashmap="" i="j;//转移根节点下标" integer="" j="" param="" private="" static="" void=""> hashMap, int j){
  if(j != 0)
   System.out.print("第 "+j+" 趟:\t");
  for(int i=1; i<hashmap.size(); code=""></hashmap.size();></hashmap.get(j+1)){></integer,></integer,></integer,></integer,integer></integer,integer></code></code></code></code></code>

归并排序

<code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs ">import java.util.HashMap;
public class MergeSort {
 private static int contrastCount = 0;//对比次数
 private static int swapCount = 0;//交换次数
 private static int printCount = 1;//只为统计执行次数
 public static void main(String[] args) {
  System.out.println("归并尔排序:\n 先将数据分为n组,然后没两组开始合并,相邻两个合并为一个新的有序队列,重复合并直到整个队列有序,时间复杂度O(nlgn),空间复杂度O(n),稳定排序");
  HashMap<integer,integer> hashMap = new HashMap<integer,integer>();//未排序
  HashMap<integer,integer> hashMapNew = new HashMap<integer,integer>();//已排序
  hashMapNew.put(0, null);//第一位置空
  init(hashMap);//初始化
  System.out.println("初始序列为:");
  print(hashMap, 0);//打印
  System.out.println("开始归并尔排序:");
  //排序开始时间
  long start = System.currentTimeMillis();
  merge(hashMap, hashMapNew, 1, hashMap.size()-1);//排序
  //排序结束时间
  long end = System.currentTimeMillis();
  System.out.println("结果为:");
  print(hashMapNew, 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 hashMapNew 已排序的表
  */
 private static void merge(HashMap<integer, integer=""> hashMap, HashMap<integer, integer=""> hashMapNew, int low, int high){
  if(low == high){
   hashMapNew.put(low, hashMap.get(low));
   swapCount++;//发生一次交换
  }else{
   int meddle = (int)((low+high)/2);//将这一序列数均分的中间值
   merge(hashMap, hashMapNew, low, meddle);//继续对左边的序列递归
   merge(hashMap, hashMapNew, meddle+1, high);//对右边的序列递归
   mergeSort(hashMap, hashMapNew, low, meddle, high);//把相邻的序列组合起来
   for(int i=low; i<=high; i++){//将已经排好序的hashMapNew【low,high】覆盖hashMap【low,high】以便进入下一次的递归归并
    hashMap.put(i, hashMapNew.get(i));
    swapCount++;//发生一次交换
   }
  }
 }
 /**
  * 进行归并尔排序 把【low,meddle】和【meddle+1,high】和并为一个有序的hashMapNew【low,high】
  * @param hashMap 待排序的表
  * @param hashMapNew 已排序的表
  * @param low 低位
  * @param meddle 中位
  * @param high 高位
  */
 private static void mergeSort(HashMap<integer, integer=""> hashMap, HashMap<integer, integer=""> hashMapNew, int low, int meddle, int high){
  int k = low;
  int j = meddle+1;
  while(low<=meddle && j<=high){//两个序列组合成一个序列 从小到达的顺序
   if(hashMap.get(low) < hashMap.get(j)){
    hashMapNew.put(k++, hashMap.get(low++));//放入合适的位置
   }else{
    hashMapNew.put(k++, hashMap.get(j++));//放入合适的位置
   }
   contrastCount++;//发生一次对比
   swapCount++;//发生一次交换
  }
  //如果有一方多出来了 则直接赋值
  while(low<=meddle){
   hashMapNew.put(k++, hashMap.get(low++));//放入合适的位置
   swapCount++;//发生一次交换
  }
  while(j<=high){
   hashMapNew.put(k++, hashMap.get(j++));//放入合适的位置
   swapCount++;//发生一次交换
  }
  print(hashMapNew, printCount++);
 }
 /**
  * 打印已排序好的元素
  * @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,></integer,></integer,></integer,></integer,></integer,></integer,integer></integer,integer></integer,integer></integer,integer></code></code></code></code></code></code>

最低位优先基数排序

<code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs "><code class="language-java hljs ">/**
 * 最低位优先基数排序
 * @author HHF
 *
 */
public class LSDSort {
 private static int contrastCount = 0;//对比次数
 private static int swapCount = 0;//交换次数
 private static int printCount = 1;//只为统计执行次数 

 public static void main(String[] args) {
  System.out.println("最低位优先基数排序:\n 按个位、十位、百位排序,不需要比较,只需要对数求余然后保存到相应下标的二维数组内,然后依次读取,每一进制重复依次 ,时间复杂度O(d(n+rd)),空间复杂度O(n+rd),稳定排序");
  int[] data = { 173, 22, 93, 43, 55, 14, 28, 65, 39, 81, 33, 100 };
  System.out.println("初始序列为:");
  print(data, 0);//打印
  LSD(data, 3);
 }
 public static void LSD(int[] number, int d) {// d表示最大的数有多少位
  int k = 0;//number的小标
  int n = 1;//当比较十位的时候 n=10 比较百位的时候 n=100 用来吧高位降低方便求余数
  int m = 1;//正在比较number中数据的倒数第几位
  int[][] temp = new int[10][number.length];// 数组的第一维表示可能的余数0-9 二维依次记录该余数相同的数
  int[] order = new int[10];// 数组orderp[i]用来表示该位的余数是i的数的个数
  //排序开始时间
  long start = System.currentTimeMillis();
  while (m <= d) {//d=5则比较五次
   for (int i = 0; i < number.length; i++) {//把number中的数按余数插入到temp中去
    int lsd = ((number[i] / n) % 10);//求得该数的余数
    temp[lsd][order[lsd]] = number[i];//保存到相应的地方
    order[lsd]++;//该余数有几个
    swapCount++;//发生一次交换
   }
   for (int i = 0; i < 10; i++) {//将temp中的数据按顺序提取出来
    if (order[i] != 0)//如果该余数没有数据则不需要考虑
     for (int j = 0; j < order[i]; j++) {//有给余数的数一共有多少个
      number[k] = temp[i][j];//一一赋值
      k++;
      swapCount++;//发生一次交换
     }
    order[i] = 0;//置零,以便下一次使用
   }
   n *= 10;//进制+1 往前走
   k = 0;//从头开始
   m++;//进制+1
   print(number, printCount++);
  }
  //排序结束时间
  long end = System.currentTimeMillis();
  System.out.println("结果为:");
  print(number, 0);//输出排序结束的序列
  System.out.print("一共发生了 "+contrastCount+" 次对比\t");
  System.out.print("一共发生了 "+swapCount+" 次交换\t");
  System.out.println("执行时间为"+(end-start)+"毫秒");
 }
 /**
  * 打印已排序好的元素
  * @param data 已排序的表
  * @param j 第j趟排序
  */
 private static void print(int[] data, int j){
  if(j != 0)
   System.out.print("第 "+j+" 趟:\t");
  for (int i = 0; i < data.length; i++) {
   System.out.print(data[i] + " ");
  }
  System.out.println();
 }
} </code></code></code></code></code></code></code>

本篇文章希望能帮助到您

(0)

相关推荐

  • java堆排序原理及算法实现

    从堆排序的简介到堆排序的算法实现等如下: 1. 简介 堆排序是建立在堆这种数据结构基础上的选择排序,是原址排序,时间复杂度O(nlogn),堆排序并不是一种稳定的排序方式.堆排序中通常使用的堆为最大堆. 2. 堆的定义 堆是一种数据结构,是一颗特殊的完全二叉树,通常分为最大堆和最小堆.最大堆的定义为根结点最大,且根结点左右子树都是最大堆:同样,最小堆的定义为根结点最小,且根结点左右子树均为最小堆. 最大堆满足其每一个父结点均大于其左右子结点,最小堆则满足其每一个父结点均小于其左右子结点. 3.

  • Java数据结构与算法之选择排序(动力节点java学院整理)

    每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序的数列的最后,直到全部待排序的数据元素排完. 代码 public class ChoseSort { //constructor without parameters public ChoseSort(){}; //constructor with parameters public int[] ChoseSort(int[] intArr){ for(int i=0;i<intArr.length-1;i++){ int

  • Java经典排序算法之希尔排序详解

    一.希尔排序(Shell Sort) 希尔排序(Shell Sort)是一种插入排序算法,因D.L.Shell于1959年提出而得名. Shell排序又称作缩小增量排序. 二.希尔排序的基本思想 希尔排序的中心思想就是:将数据进行分组,然后对每一组数据进行排序,在每一组数据都有序之后,就可以对所有的分组利用插入排序进行最后一次排序.这样可以显著减少交换的次数,以达到加快排序速度的目的. 希尔排序的中心思想:先取一个小于n的整数d1作为第一个增量,把文件的全部记录分成d1个组.所有距离为dl的倍数

  • Java数据结构和算法之冒泡排序(动力节点Java学院整理)

    冒泡排序(Bubble Sort)是一种简单的排序算法.它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成.这个算法的名字由来是因为越小的元素会经由交换慢慢"浮"到数列的顶端. 冒泡排序算法的运作如下: 1. 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 2. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数. 3. 针对所有

  • java冒泡排序和快速排序代码

    冒泡排序: 基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒.即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换. public class BubbleSorted{ public BubbleSorted(){ int a[]={49,38,65,97,76,13,27,49,78,34,12,64,5,4,62,99,98,54,56,17,18,23,34,15,35,25,53,5

  • Java经典排序算法之插入排序

    一.算法原理 插入排序法:所谓插入排序法乃是将一个数目插入该占据的位置. 假设我们输入的是 "53,27,36,15,69,  42" 我们从第二个数字开始,这个数字是27,我们的任务只要看看27有没有正确的位置,我们的做法是和这个数字左边的数字来比,因此我们比较27和53,27比53小,所以我们就交换27和53,原来的排列就变成了"27, 53, 36, 15, 69, 42 " 接下来,我们看第3个数字有没有在正确的位置.这个数字是36,它的左边数字是53,36

  • Java经典排序算法之二分插入排序详解

    一.折半插入排序(二分插入排序) 将直接插入排序中寻找A[i]的插入位置的方法改为采用折半比较,即可得到折半插入排序算法.在处理A[i]时,A[0]--A[i-1]已经按关键码值排好序.所谓折半比较,就是在插入A[i]时,取A[i-1/2]的关键码值与A[i]的关键码值进行比较,如果A[i]的关键码值小于A[i-1/2]的关键码值,则说明A[i]只能插入A[0]到A[i-1/2]之间,故可以在A[0]到A[i-1/2-1]之间继续使用折半比较:否则只能插入A[i-1/2]到A[i-1]之间,故可

  • 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(&q

  • Java for循环常见优化方法案例详解

    目录 方法一:最常规的不加思考的写法 方法二:数组长度提取出来 方法三:数组长度提取出来 方法四:采用倒序的写法 方法五:Iterator 遍历 方法六:jdk1.5后的写法 方法七:循环嵌套外小内大原则 方法八:循环嵌套提取不需要循环的逻辑 方法九:异常处理写在循环外面 前言 我们都经常使用一些循环耗时计算的操作,特别是for循环,它是一种重复计算的操作,如果处理不好,耗时就比较大,如果处理书写得当将大大提高效率,下面总结几条for循环的常见优化方式. 首先,我们初始化一个集合 list,如下

  • java 请求跨域问题解决方法实例详解

    java 请求跨域问题解决方法实例详解 新建Util类,在Util中添加下面方法: /* * response请求跨域公共设置 */ public static HttpServletResponse SetHttpServletResponse( HttpServletResponse response) { response.setHeader("Access-Control-Allow-Origin", "*"); response.setHeader(&qu

  • Java8 Comparator排序方法实例详解

    这篇文章主要介绍了Java8 Comparator排序方法实例详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Java8 中 Comparator 接口提供了一些静态方法,可以方便于我们进行排序操作,下面通过例子讲解下如何使用 对整数列表排序(升序) List<Integer> list = Arrays.asList(1, 4, 2, 6, 2, 8); list.sort(Comparator.naturalOrder()); Sys

  • Yii中CGridView关联表搜索排序方法实例详解

    本文实例讲述了Yii中CGridView关联表搜索排序方法.分享给大家供大家参考.具体实现方法如下: 在Yii CGridView 关联表搜索排序实现方法有点复杂,今天看了一老外写的了篇游戏,下面我整理一下与各位朋友分享一下,相信会对大家Yii框架的学习有所帮助. 首先,检查你的blog demo里的protectedmodelsComment.php,确保Comment模型有一个search的方法,如果没有,就用gii生成一个,我下载到的blog demo里倒是没有. 然后,写代码的时间到了,

  • Python函数装饰器常见使用方法实例详解

    本文实例讲述了Python函数装饰器常见使用方法.分享给大家供大家参考,具体如下: 一.装饰器 首先,我们要了解到什么是开放封闭式原则? 软件一旦上线后,对修改源代码是封闭的,对功能的扩张是开放的,所以我们应该遵循开放封闭的原则. 也就是说:我们必须找到一种解决方案,能够在不修改一个功能源代码以及调用方式的前提下,为其加上新功能. 总结:原则如下: 1.不修改源代码 2.不修改调用方式 目的:在遵循1和2原则的基础上扩展新功能. 二.什么是装饰器? 器:指的是工具, 装饰:指的是为被装饰对象添加

  • java解析Excel文件的方法实例详解

    目录 介绍 1.1 pom依赖 1.2 将数据流转化为可解析的Workbook类型文件 1.3 解析 1.4 Controller层接收前端传递的Excel文件(前端使用Element-ui的<el-upload>组件) 1.5 ServiceIMPL层解析Excel文件并将解析结果返回 1.6 前端VUE实现Excel文件的上传(使用Element-ui的<el-upload>组件) 总结 在做一个项目时,有很多原本保存在Excel文件中的基础数据,如此则需要一个Excel文件解

  • C#调用Java方法实例详解

    C#可以直接引用C++的DLL和转换JAVA写好的程序.最近由于工作原因接触这方面比较多,根据实际需求,我们通过一个具体例子把一个JAVA方法转换成可以由C#直接调用的DLL C#调用c++ C#调用C++的例子网上很多,以一个C++的具体方法为例. C++代码 // 获取一帧图像数据 MVSMARTCAMCTRL_API int __stdcall MV_SC_GetOneFrame(IN void* handle, IN OUT unsigned char *pData , IN unsig

  • java金钱处理方法实例详解

    java金钱处理方法实例详解 在支付行业中,涉及到对金钱的处理比较多.比如分转化成元.费率计算.手续费计算等等. 1.分转化成元 /** * 单位换算:分->元 * * @param amount * 分 * @param scale * 保留小数点位数 * @return */ public static String fenToYuan(long amount, int scale) { return new BigDecimal(amount).divide(new BigDecimal(

  • Java身份证验证方法实例详解

    Java身份证验证方法实例详解 身份证号码验证 1.号码的结构 公民身份号码是特征组合码,由十七位数字本体码和一位校验码组成.排列顺序从左至右依次为:六位数字地址码, 八位数字出生日期码,三位数字顺序码和一位数字校验码. 2.地址码(前六位数) 表示编码对象常住户口所在县(市.旗.区)的行政区划代码,按GB/T2260的规定执行. 3.出生日期码(第七位至十四位) 表示编码对象出生的年.月.日,按GB/T7408的规定执行,年.月.日代码之间不用分隔符. 4.顺序码(第十五位至十七位) 表示在同

随机推荐