Java常用的九种排序方法及代码实现

 

  1. package com.algorithm.Demo;
  2. import java.util.ArrayList;
  3. import java.util.Arrays;
  4. import java.util.List;
  5. public class SortRecoder {
  6.     public static void main(String[] args) {
  7.         SortRecoder SR = new SortRecoder();
  8.         int[] a={49,38,65,97,76,13,27,49,78,34,12,64,5,4,18,23,34,15,35,25,53};
  9.         System.out.println(" --1.直接插入排序--");
  10.         SR.insertSort(a);
  11.         System.out.println(" --2.希尔排序--");
  12.         SR.sheelSort(a);
  13.         System.out.println(" --3.简单选择排序--");
  14.         SR.selectSort(a);
  15.         System.out.println(" --4.堆排序--");
  16.         SR.heapSort(a);
  17.         System.out.println(" --5.冒泡排序--");
  18.         SR.bubbleSort(a);
  19.         System.out.println(" --6.快速排序--");
  20.         SR.FastSort(a,0,a.length-1);
  21.         SR.printAll(a);
  22.         System.out.println(" --7.归并排序--");
  23.         SR.mergingSort(a,0,a.length-1);
  24.         SR.printAll(a);
  25.         System.out.println(" --8.基数排序--");
  26.         SR.radixSort(a);
  27.         System.out.println(" --9.类方法排序--");
  28.         SR.ArraySort(a);
  29.     }
  30.     // 打印完整序列
  31.     public void printAll(int[] list) {
  32.         for (int value : list) {
  33.             System.out.print(value+"  ");
  34.         }
  35.         System.out.println();
  36.     }
  37.     //1.直接插入排序
  38.     //将大于要插入值的数整体后移一个单位 ,将需要插入的小数值向前移一位。
  39.     public void insertSort(int[] a){
  40.         for(int i=1;i<a.length;i++){
  41.             int insertNum=a[i];//要插入的数 38,65,97,76,13,27...
  42.             int j=i-1;
  43.             //依次比较更换位置
  44.             for(;j>=0&&insertNum<a[j];j--){ //将大于要插入值的数整体后移一个单位  ,j减少1。
  45.                 a[j+1]=a[j];
  46.             }
  47.             a[j+1]=insertNum;//将需要插入的数放在要插入的位置。
  48.         }
  49.         this.printAll(a);
  50.     }
  51.     //2.希尔排序(最小增量排序)  --对于直接插入排序问题,数据量巨大时。
  52.     //  算法先将要排序的一组数按某个增量d(n/2,n为要排序数的个数)分成若干组,
  53.     //  每组中记录的下标相差d.对每组中全部元素进行直接插入排序,然后再用一个较小的增量(d/2)对它进行分组,
  54.     //  在每组中再进行直接插入排序。当增量减到1时,进行直接插入排序后,排序完成。
  55.     public  void sheelSort(int[] a){
  56.         int d  = a.length;
  57.         while (d!=0) {
  58.             d=d/2;
  59.             for (int x = 0; x < d; x++) {//分的组数
  60.                 for (int i = x + d; i < a.length; i += d) {//组中的元素,从第二个数开始
  61.                     int j = i - d;//j为有序序列最后一位的位数
  62.                     int temp = a[i];//要插入的元素 
  63.                     for (; j >= 0 && temp < a[j]; j -= d) {
  64.                         a[j + d] = a[j];//向后移动d位
  65.                     }
  66.                     a[j + d] = temp;
  67.                 }
  68.             }
  69.         }
  70.         this.printAll(a);
  71.     }
  72.     //3.简单选择排序    --常用于取序列中最大最小的几个数时。
  73.     //遍历整个序列,将最小的数放在最前面。遍历剩下的序列,将最小的数放在最前面。
  74.     public void selectSort(int[] a) {
  75.         int length = a.length;
  76.         for (int i = 0; i < length; i++) {//循环次数
  77.             int key = a[i];
  78.             int position=i;
  79.             for (int j = i + 1; j < length; j++) {//选出最小的值和位置
  80.                 if (a[j] < key) {
  81.                     key = a[j];
  82.                     position = j;
  83.                 }
  84.             }
  85.             a[position]=a[i];//交换位置
  86.             a[i]=key;
  87.         }
  88.         this.printAll(a);
  89.     }
  90.     //4.堆排序  --是一种树形选择排序,是对直接选择排序的有效改进。
  91.     //将根节点与最后一个节点交换,然后断开最后一个节点。重复第一、二步,直到所有节点断开。根节点是最小值
  92.     public void heapSort(int[] a){
  93.         int arrayLength=a.length;
  94.         //循环建堆  
  95.         for(int i=0;i<arrayLength-1;i++){
  96.             //建堆  
  97.             buildMaxHeap(a,arrayLength-1-i);
  98.             //交换堆顶和最后一个元素  
  99.             swap(a,0,arrayLength-1-i);
  100.            //System.out.println(Arrays.toString(a));
  101.         }
  102.         this.printAll(a);
  103.     }
  104.     private void swap(int[] data, int i, int j) {
  105.         int tmp=data[i];
  106.         data[i]=data[j];
  107.         data[j]=tmp;
  108.     }
  109.     //对data数组从0到lastIndex建大顶堆  
  110.     private void buildMaxHeap(int[] data, int lastIndex) {
  111.         //从lastIndex处节点(最后一个节点)的父节点开始  
  112.         for(int i=(lastIndex-1)/2;i>=0;i--){
  113.             //k保存正在判断的节点  
  114.             int k=i;
  115.             //如果当前k节点的子节点存在  
  116.             while(k*2+1<=lastIndex){
  117.                 //k节点的左子节点的索引  
  118.                 int biggerIndex=2*k+1;
  119.                 //如果biggerIndex小于lastIndex,即biggerIndex+1代表的k节点的右子节点存在  
  120.                 if(biggerIndex<lastIndex){
  121.                     //若果右子节点的值较大  
  122.                     if(data[biggerIndex]<data[biggerIndex+1]){
  123.                         //biggerIndex总是记录较大子节点的索引  
  124.                         biggerIndex++;
  125.                     }
  126.                 }
  127.                 //如果k节点的值小于其较大的子节点的值  
  128.                 if(data[k]<data[biggerIndex]){
  129.                     //交换他们  
  130.                     swap(data,k,biggerIndex);
  131.                     //将biggerIndex赋予k,开始while循环的下一次循环,重新保证k节点的值大于其左右子节点的值  
  132.                     k=biggerIndex;
  133.                 }else{
  134.                     break;
  135.                 }
  136.             }
  137.         }
  138.     }
  139.     //5.冒泡排序
  140.     //自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒
  141.     public void bubbleSort(int[] a){
  142.         int temp;
  143.         for(int i=0;i<a.length;i++){
  144.             for(int j=0;j<a.length-i-1;j++){
  145.                 if(a[j]>a[j+1]){
  146.                     temp=a[j];
  147.                     a[j]=a[j+1];
  148.                     a[j+1]=temp;
  149.                 }
  150.             }
  151.         }
  152.         this.printAll(a);
  153.     }
  154.     //6.快速排序   --要求时间最快时。
  155.     //选择第一个数为p,小于p的数放在左边,大于p的数放在右边。递归的将p左边和右边的数都按照第一步进行,直到不能递归
  156.     public void FastSort(int[] a,int low,int high){
  157.         int start = low;
  158.         int end = high;
  159.         int key = a[low];
  160.         while(end>start){
  161.             //从后往前比较
  162.             while(end>start&&a[end]>=key)  //如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较
  163.                 end--;
  164.             if(a[end]<=key){
  165.                 int temp = a[end];
  166.                 a[end] = a[start];
  167.                 a[start] = temp;
  168.             }
  169.             //从前往后比较
  170.             while(end>start&&a[start]<=key)//如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置
  171.                start++;
  172.             if(a[start]>=key){
  173.                 int temp = a[start];
  174.                 a[start] = a[end];
  175.                 a[end] = temp;
  176.             }
  177.         //此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
  178.         }
  179.         //递归
  180.         if(start>low) FastSort(a,low,start-1);//左边序列。第一个索引位置到关键值索引-1
  181.         if(end<high) FastSort(a,end+1,high);//右边序列。从关键值索引+1到最后一个
  182.     }
  183.     //7.归并排序  --速度仅次于快排
  184.     //选择相邻两个数组成一个有序序列。 然后再把相邻有序子序列合并为整体有序序列。重复执行,直到全部组成一个有序序列
  185.     public void mergingSort(int[] data, int left, int right) {
  186.         if(left<right){
  187.             //找出中间索引  
  188.             int center=(left+right)/2;
  189.             //对左边数组进行递归  
  190.             mergingSort(data,left,center);
  191.             //对右边数组进行递归  
  192.             mergingSort(data,center+1,right);
  193.             //合并  
  194.             merge(data,left,center,right);
  195.         }
  196.     }
  197.     public void merge(int[] data, int left, int center, int right) {
  198.         int [] tmpArr=new int[data.length];
  199.         int mid=center+1;
  200.         //third记录中间数组的索引  
  201.         int third=left;
  202.         int tmp=left;
  203.         while(left<=center&&mid<=right){
  204.             //从两个数组中取出最小的放入中间数组  
  205.             if(data[left]<=data[mid]){
  206.                 tmpArr[third++]=data[left++];
  207.             }else{
  208.                 tmpArr[third++]=data[mid++];
  209.             }
  210.         }
  211.         //剩余部分依次放入中间数组  
  212.         while(mid<=right){
  213.             tmpArr[third++]=data[mid++];
  214.         }
  215.         while(left<=center){
  216.             tmpArr[third++]=data[left++];
  217.         }
  218.         //将中间数组中的内容复制回原数组  
  219.         while(tmp<=right){
  220.             data[tmp]=tmpArr[tmp++];
  221.         }
  222.         //System.out.println(Arrays.toString(data));  
  223.     }
  224.     //8.基数排序   --用于大量数,很长的数进行排序时。
  225.     //将所有待比较数值统一为同样的数位长度,数位较短的数前面补零。然后,从最低位开始,依次进行一次排序。这样从最低位排序一直到最高位排序完成以后,数列就变成一个有序序列。
  226.     public void radixSort (int[] array) {
  227.         //首先确定排序的趟数;     
  228.         int max = array[0];
  229.         for (int i = 1; i < array.length; i++) {
  230.             if (array[i] > max) {
  231.                 max = array[i];
  232.             }
  233.         }
  234.         int time = 0;
  235.         //判断位数;     
  236.         while (max > 0) {
  237.             max /= 10;
  238.             time++;
  239.         }
  240.         //建立10个队列;     
  241.         List<ArrayList> queue = new ArrayList<ArrayList>();
  242.         for (int i = 0; i < 10; i++) {
  243.             ArrayList<Integer> queue1 = new ArrayList<Integer>();
  244.             queue.add(queue1);
  245.         }
  246.         //进行time次分配和收集;     
  247.         for (int i = 0; i < time; i++) {
  248.             //分配数组元素;     
  249.             for (int j = 0; j < array.length; j++) {
  250.                 //得到数字的第time+1位数;   
  251.                 int x = array[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i);
  252.                 ArrayList<Integer> queue2 = queue.get(x);
  253.                 queue2.add(array[j]);
  254.                 queue.set(x, queue2);
  255.             }
  256.             int count = 0;//元素计数器;     
  257.             //收集队列元素;     
  258.             for (int k = 0; k < 10; k++) {
  259.                 while (queue.get(k).size() > 0) {
  260.                     ArrayList<Integer> queue3 = queue.get(k);
  261.                     array[count] = queue3.get(0);
  262.                     queue3.remove(0);
  263.                     count++;
  264.                 }
  265.             }
  266.         }
  267.         this.printAll(array);
  268.     }
  269.     //9.类方法降序排列
  270.     public void ArraySort(int[] a){
  271.         Arrays.sort(a);
  272.         //this.printAll(a);
  273.         for(int i=0,j=a.length-1;i<j;i++,j--){
  274.             int temp=a[i];
  275.             a[i]=a[j];
  276.             a[j]=temp;
  277.         }
  278.         this.printAll(a);
  279.     }
  280. }

猜你喜欢

转载自www.cnblogs.com/chen-hai-jia/p/9953261.html