Java实现常见的排序算法

    public static void bubble_sort(int[] arry){
        int n = arry.length;
        for (int i = 0; i <n ; i++) {
            for (int j = 1; j <n ; j++) {
                if(arry[j-1]>arry[j]){
                    int tmp = arry[j];
                    arry[j] = arry[j-1];
                    arry[j-1] = tmp;
                }
            }
            System.out.print("第"+(i+1)+"躺排序: ");
            printArray(arry);
            System.out.println();
        }
    }
1躺排序: 1	3	1	4	5	2	6	92躺排序: 1	1	3	4	2	5	6	93躺排序: 1	1	3	2	4	5	6	94躺排序: 1	1	2	3	4	5	6	95躺排序: 1	1	2	3	4	5	6	96躺排序: 1	1	2	3	4	5	6	97躺排序: 1	1	2	3	4	5	6	98躺排序: 1	1	2	3	4	5	6	9	
    public static void select_sort(int[] array){
        int n = array.length;
        for (int i = 0; i <n ; i++) {
            int minIndex = i;
            //每一趟选出一个最小的与
            for (int j = i+1; j <n ; j++) {
                if(array[minIndex]>array[j]){
                    minIndex = j;
                }
            }
            int tmp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = tmp;

            System.out.print("第"+(i+1)+"躺排序: ");
            printArray(array);
            System.out.println();
        }
    }

1躺排序: 1	3	4	1	5	9	2	62躺排序: 1	1	4	3	5	9	2	63躺排序: 1	1	2	3	5	9	4	64躺排序: 1	1	2	3	5	9	4	65躺排序: 1	1	2	3	4	9	5	66躺排序: 1	1	2	3	4	5	9	67躺排序: 1	1	2	3	4	5	6	98躺排序: 1	1	2	3	4	5	6	9	
 public static void insert_sort(int[] array){
        int n = array.length;
        for (int i = 1; i <n ; i++) {
           if(array[i]<array[i-1]){
               int tmp = array[i];
               int index = i; //待插入的下标
               for (int j = i-1; j >=0 ; j--) {
                   if(array[j]>tmp){
                       array[j+1] = array[j];
                       index = j;
                   }else {
                       break;
                   }
               }
               array[index] = tmp;
           }
            System.out.print("第"+i+"躺排序: ");
            printArray(array);
            System.out.println();
        }
    }
1躺排序: 1	3	4	1	5	9	2	62躺排序: 1	3	4	1	5	9	2	63躺排序: 1	1	3	4	5	9	2	64躺排序: 1	1	3	4	5	9	2	65躺排序: 1	1	3	4	5	9	2	66躺排序: 1	1	2	3	4	5	9	67躺排序: 1	1	2	3	4	5	6	9	
public static void shell_sort(int[] array){
        int n = array.length;
        int gap = n/2;  // 初始步长
        while(gap > 0){
            for (int i = gap; i <n ; i++) {
                int tmp = array[i];
                int j = i;
                while( j >= gap && array[j-gap] > tmp){
                    array[j] = array[j-gap];
                    j = j - gap;
                }
                array[j] = tmp;
            }
            System.out.print("步长为"+gap+"的排序: ");
            printArray(array);
            System.out.println();

            gap = gap/2; //重新设置步长
        }
    }
步长为4的排序: 3	1	2	1	5	9	4	6	
步长为2的排序: 2	1	3	1	4	6	5	9	
步长为1的排序: 1	1	2	3	4	5	6	9	
public static int[] merge_sort(int[] a,int low,int high){
        int mid = (low+high)/2;
        System.out.print("mid为"+mid+"的排序: ");
        printArray(a);
        System.out.println();
        if(low<high){
            merge_sort(a,low,mid);
            merge_sort(a,mid+1,high);
            //左右归并
            merge(a,low,mid,high);
        }
        return a;
    }

    public static void merge(int[] a, 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(a[i]<a[j]){
                temp[k++] = a[i++];
            }else{
                temp[k++] = a[j++];
            }
        }
        // 把左边剩余的数移入数组
        while(i<=mid){
            temp[k++] = a[i++];
        }
        // 把右边边剩余的数移入数组
        while(j<=high){
            temp[k++] = a[j++];
        }
        // 把新数组中的数覆盖nums数组
        for(int x=0;x<temp.length;x++){
            a[x+low] = temp[x];
        }
    }
mid为3的排序: 3	1	4	1	5	9	2	6	
mid为1的排序: 3	1	4	1	5	9	2	6	
mid为0的排序: 3	1	4	1	5	9	2	6	
mid为0的排序: 3	1	4	1	5	9	2	6	
mid为1的排序: 3	1	4	1	5	9	2	6	
mid为2的排序: 1	3	4	1	5	9	2	6	
mid为2的排序: 1	3	4	1	5	9	2	6	
mid为3的排序: 1	3	4	1	5	9	2	6	
mid为5的排序: 1	1	3	4	5	9	2	6	
mid为4的排序: 1	1	3	4	5	9	2	6	
mid为4的排序: 1	1	3	4	5	9	2	6	
mid为5的排序: 1	1	3	4	5	9	2	6	
mid为6的排序: 1	1	3	4	5	9	2	6	
mid为6的排序: 1	1	3	4	5	9	2	6	
mid为7的排序: 1	1	3	4	5	9	2	6	
    public static void quickSort(int[] arr,int low,int high){
        System.out.print("low="+low+" high="+high+"的排序: ");
        printArray(arr);
        System.out.println();
        int i,j,temp,t;
        if(low>high){
            return;
        }
        i=low;
        j=high;
        //temp就是基准位
        temp = arr[low];

        while (i<j) {
            //先看右边,依次往左递减
            while (temp<=arr[j]&&i<j) {
                j--;
            }
            //再看左边,依次往右递增
            while (temp>=arr[i]&&i<j) {
                i++;
            }
            //如果满足条件则交换
            if (i<j) {
                t = arr[j];
                arr[j] = arr[i];
                arr[i] = t;
            }

        }
        //最后将基准为与i和j相等位置的数字交换
        arr[low] = arr[i];
        arr[i] = temp;
        //递归调用左半数组
        quickSort(arr, low, j-1);
        //递归调用右半数组
        quickSort(arr, j+1, high);
    }
low=0 high=7的排序: 3	1	4	1	5	9	2	6	
low=0 high=2的排序: 1	1	2	3	5	9	4	6	
low=0 high=-1的排序: 1	1	2	3	5	9	4	6	
low=1 high=2的排序: 1	1	2	3	5	9	4	6	
low=1 high=0的排序: 1	1	2	3	5	9	4	6	
low=2 high=2的排序: 1	1	2	3	5	9	4	6	
low=2 high=1的排序: 1	1	2	3	5	9	4	6	
low=3 high=2的排序: 1	1	2	3	5	9	4	6	
low=4 high=7的排序: 1	1	2	3	5	9	4	6	
low=4 high=4的排序: 1	1	2	3	4	5	9	6	
low=4 high=3的排序: 1	1	2	3	4	5	9	6	
low=5 high=4的排序: 1	1	2	3	4	5	9	6	
low=6 high=7的排序: 1	1	2	3	4	5	9	6	
low=6 high=6的排序: 1	1	2	3	4	5	6	9	
low=6 high=5的排序: 1	1	2	3	4	5	6	9	
low=7 high=6的排序: 1	1	2	3	4	5	6	9	
low=8 high=7的排序: 1	1	2	3	4	5	6	9	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值