黑马程序员技术交流社区

标题: 数组排序 [打印本页]

作者: 黑马的味道    时间: 2015-8-20 22:34
标题: 数组排序
  1. public class MySort {

  2.     /**
  3.      * @param args
  4.      */
  5.     public static void main(String[] args) {
  6.         int array[] = {45,32,54,12,43,65,11,3,43,6,33,90,44,1,178};
  7.         MySort mySort = new MySort();
  8.         mySort.insertSort(array);
  9.         System.out.print("插入排序结果 :  ");
  10.         mySort.printArray(array);
  11.         System.out.println();
  12.         mySort.bubbleSort(array);
  13.         System.out.print("冒泡排序结果 :  ");
  14.         mySort.printArray(array);
  15.         mySort.qsort(array);
  16.         System.out.println();
  17.         System.out.print("快速排序结果 :  ");
  18.         mySort.printArray(array);
  19.         mySort.shellSort(array);
  20.         System.out.println();
  21.         System.out.print("希尔排序结果 :  ");
  22.         mySort.printArray(array);
  23.         mySort.selectSort(array);
  24.         System.out.println();
  25.         System.out.print("选择排序结果 :  ");
  26.         mySort.printArray(array);
  27.     }
  28.      
  29.     /**
  30.      * 直接插入排序
  31.      * 基本思想:在要排序的一组数中,假设前面(n-1)[n>=2] 个数已经是排好顺序的,现在要把第n个数插到前面的有序数中,使得这n个数也是排好顺序的。如此反复循环,直到全部排好顺序
  32.      */
  33.     public void insertSort(int[] array){
  34.         int temp=0;  
  35.         for(int i=1;i<array.length;i++){  
  36.            int j=i-1;  
  37.            temp=array[i];  
  38.            for(;j>=0&&temp<array[j];j--){  
  39.                array[j+1]=array[j];                       //将大于temp的值整体后移一个单位  
  40.            }  
  41.            array[j+1]=temp;  
  42.         }
  43.     }
  44.      
  45.     /**
  46.      * 冒泡排序
  47.      * 基本思想:在要排序的一组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较和调整,让较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现它们的排序与排序要求相反时,就将它们互换。
  48.      */
  49.     public void bubbleSort(int[] array) {
  50.         int temp;
  51.         for(int i=0;i<array.length;i++){//趟数
  52.           for(int j=0;j<array.length-i-1;j++){//比较次数
  53.             if(array[j]>array[j+1]){
  54.               temp=array[j];
  55.               array[j]=array[j+1];
  56.               array[j+1]=temp;
  57.             }
  58.           }
  59.         }
  60.     }
  61.      
  62.     /**
  63.      * 快速排序
  64.      * 基本思想:选择一个基准元素,通常选择第一个元素或者最后一个元素,通过一趟扫描,将待排序列分成两部分,一部分比基准元素小,一部分大于等于基准元素,此时基准元素在其排好序后的正确位置,然后再用同样的方法递归地排序划分的两部分。
  65.      * @param array
  66.      */
  67.     public void qsort(int array[]){
  68.         if(array.length>1){
  69.             _qsort(array,0,array.length-1);
  70.         }
  71.     }
  72.     /**
  73.      * 一趟快速排序
  74.      * @param array
  75.      */
  76.     private void _qsort(int[] array,int low,int high){
  77.         if(low < high){
  78.             int middle = getMiddle(array, low, high);
  79.             _qsort(array,low,middle-1);
  80.             _qsort(array, middle+1, high);
  81.         }
  82.     }
  83.     /**
  84.      * 得到中间值
  85.      */
  86.     private int getMiddle(int[] array,int low,int high){
  87.         int tmp = array[low];
  88.         while(low < high){
  89.             while(low < high && array[high] >= tmp)
  90.                 high--;
  91.             array[low] = array[high];
  92.             while(low<high && array[low]<=tmp)
  93.                 low++;
  94.             array[high] = array[low];
  95.         }
  96.         array[low] = tmp;
  97.         return low;
  98.     }
  99.      
  100.     /**
  101.      * 简单选择排序
  102.      * 基本思想:在要排序的一组数中,选出最小的一个数与第一个位置的数交换;然后在剩下的数当中再找最小的与第二个位置的数交换,如此循环到倒数第二个数和最后一个数比较为止。
  103.      * @param array
  104.      */
  105.     public void selectSort(int[] array){
  106.         int position=0;  
  107.         for(int i=0;i<array.length;i++){  
  108.                
  109.             int j=i+1;  
  110.             position=i;  
  111.             int temp=array[i];  
  112.             for(;j<array.length;j++){  
  113.             if(array[j]<temp){  
  114.                 temp=array[j];  
  115.                 position=j;  
  116.             }  
  117.             }  
  118.             array[position]=array[i];  
  119.             array[i]=temp;  
  120.         }
  121.     }
  122.      
  123.     /**
  124.      * 希尔排序(最小增量排序)
  125.      * 基本思想:算法先将要排序的一组数按某个增量d(n/2,n为要排序数的个数)分成若干组,每组中记录的下标相差d.对每组中全部元素进行直接插入排序,然后再用一个较小的增量(d/2)对它进行分组,在每组中再进行直接插入排序。当增量减到1时,进行直接插入排序后,排序完成。
  126.      * @param array
  127.      */
  128.     public  void shellSort(int[] array){  
  129.         double d1=array.length;  
  130.         int temp=0;  
  131.         while(true){  
  132.             d1= Math.ceil(d1/2);  
  133.             int d=(int) d1;  
  134.             for(int x=0;x<d;x++){  
  135.                 for(int i=x+d;i<array.length;i+=d){  
  136.                     int j=i-d;  
  137.                     temp=array[i];  
  138.                     for(;j>=0&&temp<array[j];j-=d){  
  139.                         array[j+d]=array[j];  
  140.                     }  
  141.                     array[j+d]=temp;  
  142.                 }  
  143.             }  
  144.             if(d==1)  
  145.                 break;  
  146.         }  
  147.     }  
  148.      
  149.     /**
  150.      * 打印数组中的所有元素
  151.      */
  152.      
  153.     public void printArray(int[] array){
  154.         for (int i = 0; i < array.length; i++) {
  155.             System.out.print(array[i]+" ");
  156.         }
  157.     }
  158. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2