安静不平静  

1.冒泡排序

 1 public static void main(String[] args) {
3 int[] arr={6,3,8,2,9,1}; 4 System.out.println("排序前数组为:"); 5 for(int num:arr){ 6 System.out.print(num+" "); 7 } 8 /** 9 * 来源( SiberiaDante的博客) 10 * 11 * 原理:比较两个相邻的元素,将值大的元素交换至右端。 12 13 * 思路:依次比较相邻的两个数,将小数放在前面,大数放在后面。 14 * 即在第一趟:首先比较第1个和第2个数,将小数放前,大数放后。然后比较第2个数和第3个数,将小数放前,大数放后,如此继续,直至比较最后两个数,将小数放前,大数放后。 15 * 重复第一趟步骤,直至全部排序完成。 16 * 17 * N个数字要排序完成,总共进行N-1趟排序,每i趟的排序次数为(N-i)次 18 */ 19 for(int i=0;i<arr.length-1;i++){//外层循环控制排序趟数 20 for(int j=0;j<arr.length-1-i;j++){//内层循环控制每一趟排序多少次 21 if(arr[j]>arr[j+1]){ 22 int temp=arr[j]; 23 arr[j]=arr[j+1]; 24 arr[j+1]=temp; 25 } 26 } 27 } 28 System.out.println(); 29 System.out.println("排序后的数组为:"); 30 for(int num:arr){ 31 System.out.print(num+" "); 32 } 33 }

          控制台输出:

          排序前数组为:
          6 3 8 2 9 1
          排序后的数组为:
         1 2 3 6 8 9

 

2.快速排序

 1 public class Kuaisu {
 2 
 3     /**
 4      * 原理:选择一个基准元素,通常选择第一个元素或者最后一个元素
 5      *      通过一趟扫描,将待排序列分成两部分,一部分比基准元素小,一部分大于等于基准元素
 6      *      此时基准元素在其排好序后的正确位置,然后再用同样的方法递归地排序划分的两部分,直到序列中的所有记录均有序为止
 7      * @param args
 8      */
 9     public static void main(String[] args) {
10         // TODO Auto-generated method stub
11         int[] a = { 49, 38, 65, 97, 76, 13, 27, 49 };
12         System.out.print("排序前:");
13         for(int num:a){
14             System.out.print(num+" ");
15         }
16         quickSort(a);
17         System.out.println("\n排序后:");
18         for(int num:a){
19             System.out.print(num+" ");
20         }
21     }
22 
23     public static void sort(int[] a, int low, int hight) {
24         int i, j, index;
25         if (low > hight) {
26             return;
27         }
28         i = low;
29         j = hight;
30         index = a[i]; // 用子表的第一个记录做基准
31         while (i < j) { // 从表的两端交替向中间扫描
32             while (i < j && a[j] >= index)
33                 j--;
34             if (i < j)
35                 a[i++] = a[j];// 用比基准小的记录替换低位记录
36             while (i < j && a[i] < index)
37                 i++;
38             if (i < j) // 用比基准大的记录替换高位记录
39                 a[j--] = a[i];
40         }
41         a[i] = index;// 将基准数值替换回 a[i]
42         sort(a, low, i - 1); // 对低子表进行递归排序
43         sort(a, i + 1, hight); // 对高子表进行递归排序
44 
45     }
46 
47     public static void quickSort(int[] a) {
48         sort(a, 0, a.length - 1);
49     }
50 }

             控制台输出:

    排序前:49 38 65 97 76 13 27 49
              排序后:
              13 27 38 49 49 65 76 97

 

3.选择排序

public class Xuanze {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] a = { 49, 38, 65, 97, 76, 13, 27, 49 };
        System.out.println("排序前:");
        for(int num:a){
            System.out.print(num+" ");
        }
        selectSort(a);
        System.out.println();
        System.out.println("排序后:");
        for(int num:a){
            System.out.print(num+" ");
        }
    }
    /**
     * 在未排序序列中找到最小元素,存放到排序序列的起始位置  
     * 再从剩余未排序元素中继续寻找最小元素,然后放到排序序列末尾。 
     * 以此类推,直到所有元素均排序完毕。 
     * @param numbers
     */
    public static void selectSort(int[] numbers){
        int length = numbers.length; //数组长度
        int temp = 0;                //中间变量
        
        for(int i=0;i<length;i++){
            int k = i;               //待确定的位置
            //选择出应该在第i个位置的数
            for(int j = length-1;j>i;j--){
                if(numbers[j] < numbers[k]){
                    k = j;
                }
            }
            //交换两个数
            temp = numbers[i];
            numbers[i] = numbers[k];
            numbers[k] = temp;
        }
    }
}

              控制台输出:

    排序前:
    49 38 65 97 76 13 27 49
    排序后:
    13 27 38 49 49 65 76 97

4.插入排序

public class Charu {
    /**  
     * 插入排序
     * 
     * 从第一个元素开始,该元素可以认为已经被排序
     * 取出下一个元素,在已经排序的元素序列中从后向前扫描 
     * 如果该元素(已排序)大于新元素,将该元素移到下一位置  
     * 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置  
     * 将新元素插入到该位置中  
     * 重复步骤2  
     * @param numbers  待排序数组
     */  
    public static void insertSort(int[] numbers){
        int size = numbers.length;
        int temp = 0 ;
        int j =  0;
    
        for(int i = 0 ; i < size ; i++)
        {
            temp = numbers[i];
            //假如temp比前面的值小,则将前面的值后移
            for(j = i ; j > 0 && temp < numbers[j-1] ; j --)
            {
            numbers[j] = numbers[j-1];
            }
            numbers[j] = temp;
        }
    }
    
    public static void main(String[] args) {
        int[] a = { 49, 38, 65, 97, 76, 13, 27, 49 };
        System.out.println("排序前:");
        for(int num:a){
            System.out.print(num+" ");
        }
        insertSort(a);
        System.out.println();
        System.out.println("排序后:");
        for(int num:a){
            System.out.print(num+" ");
        }
    }
}

      控制台输出:

      排序前:
      49 38 65 97 76 13 27 49
      排序后:
      13 27 38 49 49 65 76 97

 

5.希尔排序

public class Xier {
    /**希尔排序的原理:根据需求,如果你想要结果从大到小排列,它会首先将数组进行分组,然后将较大值移到前面,较小值
     * 移到后面,最后将整个数组进行插入排序,这样比起一开始就用插入排序减少了数据交换和移动的次数,可以说希尔排序是加强
     * 版的插入排序
     * 拿数组5, 2, 8, 9, 1, 3,4来说,数组长度为7,当increment为3时,数组分为两个序列
     * 5,2,8和9,1,3,4,第一次排序,9和5比较,1和2比较,3和8比较,4和比其下标值小increment的数组值相比较
     * 此例子是按照从大到小排列,所以大的会排在前面,第一次排序后数组为9, 2, 8, 5, 1, 3,4
     * 第一次后increment的值变为3/2=1,此时对数组进行插入排序,
     *实现数组从大到小排
     */

        public static void shellSort(int[] data) 
        {
            int j = 0;
            int temp = 0;
            //每次将步长缩短为原来的一半
            for (int increment = data.length / 2; increment > 0; increment /= 2){
                for (int i = increment; i < data.length; i++) 
                {
                    temp = data[i];
                    for (j = i; j >= increment; j -= increment) {
                        //如想从大到小排只需修改这里   
                        if(temp < data[j - increment]){
                            data[j] = data[j - increment];
                        }
                        else
                        {
                            break;
                        }
                    } 
                    data[j] = temp;
                }
            }
        }
        
        public static void main(String[] args) {
            int[] a = { 49, 38, 65, 97, 76, 13, 27, 49 };
            System.out.println("排序前:");
            for(int num:a){
                System.out.print(num+" ");
            }
            shellSort(a);
            System.out.println();
            System.out.println("排序后:");
            for(int num:a){
                System.out.print(num+" ");
            }
        }
}

      控制台输出:

      排序前:
      49 38 65 97 76 13 27 49
      排序后:
      13 27 38 49 49 65 76 97

 

6.堆排序

public class Dui {

    public static void main(String[] args) {
        int[] a={49,38,65,97,76,13,27,49,78,34,12,64};
        int arrayLength=a.length;  
        //循环建堆  
        for(int i=0;i<arrayLength-1;i++){  
            //建堆  
            buildMaxHeap(a,arrayLength-1-i);  
            //交换堆顶和最后一个元素  
            swap(a,0,arrayLength-1-i);  
            //System.out.println(Arrays.toString(a));  
        }  
        System.out.println("最终排序结果"+Arrays.toString(a));
    }
    //对data数组从0到lastIndex建大顶堆
    public static void buildMaxHeap(int[] data, int lastIndex){
         //从lastIndex处节点(最后一个节点)的父节点开始 
        for(int i=(lastIndex-1)/2;i>=0;i--){
            //k保存正在判断的节点 
            int k=i;
            //如果当前k节点的子节点存在  
            while(k*2+1<=lastIndex){
                //k节点的左子节点的索引 
                int biggerIndex=2*k+1;
                //如果biggerIndex小于lastIndex,即biggerIndex+1代表的k节点的右子节点存在
                if(biggerIndex<lastIndex){  
                    //若果右子节点的值较大  
                    if(data[biggerIndex]<data[biggerIndex+1]){  
                        //biggerIndex总是记录较大子节点的索引  
                        biggerIndex++;  
                    }  
                }  
                //如果k节点的值小于其较大的子节点的值  
                if(data[k]<data[biggerIndex]){  
                    //交换他们  
                    swap(data,k,biggerIndex);  
                    //将biggerIndex赋予k,开始while循环的下一次循环,重新保证k节点的值大于其左右子节点的值  
                    k=biggerIndex;  
                }else{  
                    break;  
                }  
            }
        }
    }
    //交换
    private static void swap(int[] data, int i, int j) {  
        int tmp=data[i];  
        data[i]=data[j];  
        data[j]=tmp;  
    } 

}

  控制台输出:最终排序结果[12, 13, 27, 34, 38, 49, 49, 64, 65, 76, 78, 97]

7.归并排序

public class GuiBin {
    /**
     * 归并排序
     * 简介:将两个(或两个以上)有序表合并成一个新的有序表 即把待排序序列分为若干个子序列,每个子序列是有序的。然后再把有序子序列合并为整体有序序列
     * 时间复杂度为O(nlogn)
     * 稳定排序方式
     * @param nums 待排序数组
     * @return 输出有序数组
     */
    public static int[] sort(int[] nums, int low, int high) {
        int mid = (low + high) / 2;
        if (low < high) {
            // 左边
            sort(nums, low, mid);
            // 右边
            sort(nums, mid + 1, high);
            // 左右归并
            merge(nums, low, mid, high);
        }
        return nums;
    }

    /**
     * 将数组中low到high位置的数进行排序
     * @param nums 待排序数组
     * @param low 待排的开始位置
     * @param mid 待排中间位置
     * @param high 待排结束位置
     */
    public static void merge(int[] nums, int low, int mid, int high) {
        int[] temp = new int[high - low + 1];
        int i = low;// 左指针
        int j = mid + 1;// 右指针
        int k = 0;

        // 把较小的数先移到新数组中
        while (i <= mid && j <= high) {
            if (nums[i] < nums[j]) {
                temp[k++] = nums[i++];
            } else {
                temp[k++] = nums[j++];
            }
        }

        // 把左边剩余的数移入数组
        while (i <= mid) {
            temp[k++] = nums[i++];
        }

        // 把右边边剩余的数移入数组
        while (j <= high) {
            temp[k++] = nums[j++];
        }

        // 把新数组中的数覆盖nums数组
        for (int k2 = 0; k2 < temp.length; k2++) {
            nums[k2 + low] = temp[k2];
        }
    }
    
    
    public static void main(String[] args) {
        int[] a = { 49, 38, 65, 97, 76, 13, 27, 49 };
        sort(a,0,a.length-1);
        for(int num:a){
            System.out.print(num+" ");
        }
    }
}

    输出结果:13 27 38 49 49 65 76 97

posted on 2017-12-13 14:20  安静不平静  阅读(171)  评论(0)    收藏  举报