数组的排序

数组排序:把一个无序数组,调整元素位置,最终得到一个有序的数组

时间复杂度:衡量算法的执行效率

空间复杂度:算法在执行过程中使用内存空间

稳定性:排序前后,相同的值顺序会不会交换

冒泡排序:

思想:相邻的元素进行比较,如果前面的元素大于后面的元素,交换位置

public class Demo13 {
    public static void main(String[] args) {
        int[] arr = {43,56,74,5,46,45,78,78} ;
        //冒泡排序
        for (int j = 0;j<arr.length-1;j++) {
            for (int i = 0; i < arr.length - 1 - j; i++) {
                if (arr[i] > arr[i + 1]) {
                    int temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;
                }
            }
        }
        for (int i : arr) {
            System.out.println(i);
        }
    }
}

选择排序 :

思想:先从数组中找到最大值,放到末尾,再从剩下的元素中找到最大值,放到剩下元素的末尾

//选择排序
public class Demo14 {
    public static void main(String[] args) {
        int[] arr = {43,56,74,5,46,45,78,78};
        for (int j=0;j<arr.length-1;j++) {
            //最大值的下标
            int maxIndex = 0;
            for (int i = 1; i < arr.length-j; i++) {
                if (arr[i] > arr[maxIndex]) {
                    maxIndex = i;
                }
            }

            //将最大的与尾部交换
            int temp = arr[maxIndex];
            arr[maxIndex] = arr[arr.length - 1-j];
            arr[arr.length-1-j] = temp;
        }
        for (int i :arr
             ) {
            System.out.println(i);
        }
    }
}

插入排序:

思想:先把第一个元素看做是一个有序数组,然后将第二个元素加入数组中,找到合适的位置形成新的有序的数组

//插入排序
public class Demo15 {
    public static void main(String[] args) {
        int[] arr = {43,56,74,5,46,45,78,78};
        for (int j = 1; j<arr.length;j++) {
            int i = j;
            int target = arr[i];
            while (i>0 && target < arr[i - 1]) {
                arr[i] = arr[i - 1];
                i--;
            }
            arr[i] = target;
        }
    }
}

快速排序:

思想:先选定数组中的第一个元素作为一个标准

将所有比它小的元素,放到前面,所有比它大的元素放到后面

将前后两个部分,分别进行快速排序

public class Demo16 {
    public static void main(String[] args) {
        int[] arr = {37,42,34,29,69,17};

        quick(arr,0 ,arr.length);
        for (int i : arr) {
            System.out.println(i);
        }
    }
    public static void quick(int[] arr,int start,int end){
        //当数组只剩下不超过一个元素时,就代表已经排好了
        if (start >= end){
            return;
        }
        //先选择一个标准
        int index = start;
        //将所有的比它小的放在前面
        //将所有的比它大的放在后面
        int left = start ;
        int right = end-1;
        while (left < right) {
            //从后往前对比
            while (left < right && arr[left] <= arr[right]) {
                right --;
            }
            if (left < right && arr[left] > arr[right]) {
                //交换位置
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
                left ++;
                index = right;

            }
            //从前往后对比
            while (left < right && arr[left] <= arr[right]) {
                left ++;
            }
            if (left < right && arr[left] > arr[right]) {
                //交换位置
                int temp = arr[left];
                arr[left] = arr[right];
                arr[right] = temp;
                right--;
                index = left;
            }
        }
        //前面一半的数组
        quick(arr,start,index);
        //后面一半的数组
        quick(arr,index+1,end);
    }
}

计数排序:

//计数排序
public class Demo17 {
    public static void main(String[] args) {
        int[] arr = {18,16,19,11,16,15,13,17};
        //不去比较元素之间的大小,排序
        //数组必须有一定的规律:元素差必是同一个数的倍数
        //选定一个最大值,再选定一个最小值,缩小集合
        //最小值11,最大值19
        int max = arr[0];
        int min = arr[0];
        //  这一步不是必须的,缩小集合范围
        for (int i = 1; i < arr.length; i++) {
            if (max < arr[i]){
                max = arr[i];
            }
            if (min > arr[i]){
                min = arr[i];
            }
        }
        int[] counts = new int[max-min+1];
        for (int i = 0; i < arr.length; i++) {
            counts[arr[i]-min] ++;
        }

        //统计某个数前面所有的数
        for (int i = 1; i < counts.length; i++) {
            counts[i] = counts[i] + counts[i-1];
        }
        int[] result = new int[arr.length];
        //两重for循环
//        int index = 0;
//        for (int i = 0; i < counts.length; i++) {
//            for (int j = 0;j < counts[i];j++ ){
//                //填充
//                result[index] = min +i;
//                index ++;
//            }
//        }
        for (int i = 0; i < arr.length; i++) {
            //arr[i] 应该放在哪个位置
            //1.名次->下标
            int order = --counts[arr[i] - min];
            result[order] = arr[i];
        }
        for (int i : result) {
            System.out.println(i);
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

澳气冲天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值