JAVA实现数据结构7种排序算法代码

共总结了冒泡排序、选择排序、插入排序、快速排序、基数排序、归并排序、堆排序,全部使用Java实现。


/**
 * @author Ealiser
 * @data 2022/3/18 - 9:49
 * 排序集合
 */
public class Sort {

//    公共方法:转换数组内两个数字的位置
    public static void swap(int[] a, int i, int j){
        int temp;
        temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }

//    冒泡排序
//    依次前后比较
    public static int[] bubbleSort(int[] result){
        int temp;
        for(int i = 0;i < result.length - 1;i++){
            for(int j = 0;j < result.length - i - 1;j++){
                if(result[j] > result[j+1]){
                    swap(result,j,j+1);
                }
            }
        }

        return result;
    }

//    选择排序
//    每轮选择最小的在前面
    public static int[] selectionSort(int[] result){
        int temp;
        for(int i = 0;i <result.length;i++){
            int min = i;
            for (int j = i;j < result.length;j++){
                if(result[j] < result[min]){
                    min = j;
                }
            }
            swap(result,i,min);
        }
        return result;
    }

//    插入排序(又名直接插入法)
//     假定前n-1个数为有序,从第n个数开始排。
    public static int[] insertionSort(int[] result){
        for (int i = 0;i < result.length-1;i++){
            for(int j = i+1;j > 0;j--){
                if(result[j] < result[j-1]){
                    swap(result,j,j-1);
                }else{
                    break;
                }
            }
        }

        return result;
    }

//    快速排序
//      详细百度,双指针
    public static int[] quickSort(int[] result,int left,int right){
        if(left < right){
            int tempLeft = left;
            int tempRight = right;
            int temp = result[left];
            while(left < right) {
                while (result[right] > temp && left < right) {
                    right--;
                }
                if (result[right] <= temp) {
                    swap(result, left, right);
                }
                while(result[left] <= temp && left < right){
                    left++;
                }
                if(result[left] > temp){
                    swap(result,left,right);
                }
            }

            quickSort(result,tempLeft,left-1);
            quickSort(result,right+1,tempRight);
        }


        return result;
    }

//    计数排序
//      详细百度,无须比较的排序
    public static int[] countingSort(int[] arr){
        int max = arr[0];
        for(int i = 0;i < arr.length;i++){
            if(arr[i] > max) max = arr[i];
        }

        int[] temp = new int[max+1];
        for(int i = 0;i < arr.length;i++){
            temp[arr[i]] += 1;
        }
        int[] result = new int[arr.length];
        int j = 0;
        for(int i = 0;i < temp.length;i++){
            if(temp[i] != 0){
                result[j] = i;
                temp[i] -= 1;
                j++;
                i--;
            }
        }
        return result;
    }

//    归并排序
//      先分后治
    public static int[] megreSort(int[] result,int left,int right){
        int len = right - left + 1;
        int tempLeft = left;
        if(left == right){
            return result;
        }else if(left != right - 1){
            megreSort(result,left,left+len/2-1);
            megreSort(result,left+len/2,right);
        }else{
            if(result[left] > result[right]) swap(result,left,right);
            return result;
        }

        int[] temp = new int[len];
        int i = 0;
        int mid = left+len/2;
        int signLeft = left;
        while(left != signLeft+len/2 && mid != right+1){
            if(result[left] >= result[mid]){
                temp[i] = result[mid];
                mid++;
            }else{
                temp[i] = result[left];
                left++;
            }
            i++;
        }
        if(left != signLeft + len/2){
            while(left != signLeft + len/2){
                temp[i] = result[left];
                left++;
                i++;
            }
        }
        else{
            while(mid != right+1){
                temp[i] = result[mid];
                mid++;
                i++;
            }
        }
        for(int j = 0;j < temp.length;j++,tempLeft++){
            result[tempLeft] = temp[j];
        }

        return result;
    }

//    堆排序 小顶堆
//    二叉树,思路有点乱了,i,j使用的有点问题,看着不爽。
    public static int[] headSort(int[] result){
        int len = result.length;
        int root = 0;
        int i;
        int j = len - 1;
        while(j > 0){
            i = j;
            while (i > 0){
                if((i-2)%2 == 1 || i ==1) {
                    root = (i - 1) / 2;
                    i -= 1;
                    if(result[root] < result[root*2 + 1]) swap(result,root,root*2+1);
                }
                else {
                    root = (i - 2) / 2;
                    i -= 2;
                    if(result[root*2+1] < result[root*2+2]){
                        if(result[root] < result[root*2+2]) swap(result,root,root
                                *2+2);
                    }else if(result[root] < result[root*2+1]) swap(result,root,root
                            *2+1);
                }
            }
            swap(result,0,j);
            j--;
        }


        return result;
    }


    public static void main(String[] args) {
//        随机数组
        int[] arr=new int[10];
        for(int i=0;i<arr.length;i++){
            arr[i]=(int)(Math.random()*10);
        }

//        1.冒泡排序
//        bubbleSort(arr);

//        2.选择排序
//        selectionSort(arr);

//        3.插入排序
//        insertionSort(arr);

//        4.快速排序
//        quickSort(arr,0,arr.length-1);

//        5.计数排序
//        返回值也不是原数组
//        arr = countingSort(arr);

//        6.归并排序
//        megreSort(arr,0,arr.length-1);

//        7.堆排序
//        headSort(arr);


        for(int i = 0;i < arr.length;i++){
            System.out.print(arr[i]);
            System.out.print(" ");
        }

    }
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值