常见排序算法Java版(待续)

  1. 冒泡排序O(n^2)

    public class Main {
        public static void main(String[] args) {
            Random random = new Random();
            int[] nums = new int[]{random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100)};
            for (int i = nums.length - 1; i >= 0; i--) {
                for (int j = 0; j < i; j++) {
                    if (nums[j] > nums[j + 1]) {
                        int temp = nums[j];
                        nums[j] = nums[j + 1];
                        nums[j + 1] = temp;
                    }
                }
            }
            for (int num : nums) {
                System.out.print(num + " ");
            }
        }
    }
    

    冒泡排序动图演示

  2. 选择排序O(n^2),

    public class Main {
    
        static Random random = new Random();
        static int[] nums = new int[]{random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100)};
    
    
        public static void main(String[] args) {
            //选择排序:核心就是记录每一轮遍历的最小值的索引,进行交换
            for (int i = 0; i < nums.length; i++) {
                int idx = i;
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[j] < nums[idx]) {
                        idx = j;
                    }
                }
                swap(nums, i, idx);
            }
            for (int num : nums) {
                System.out.print(num + " ");
            }
        }
    
        private static void swap(int[] nums, int i, int j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
    }
    

    排序算法——选择排序_选择排序语句-CSDN博客

  3. 插入排序O(n^2)

       public class Main {
    
        static Random random = new Random();
        static int[] nums = new int[]{random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100)};
    
    
        public static void main(String[] args) {
            for (int i = 0; i < nums.length; i++) {
                int idx = i;
                for (int j = i - 1; j >= 0; j--) {
                    if (nums[j] > nums[i]) {
                        nums[idx] = nums[j];
                        idx = j;
                    } else {
                        break;
                    }
                }
                nums[idx] = nums[i];
            }
            for (int num : nums) {
                System.out.print(num + " ");
            }
        }
    }
    

    img

  4. 快速排序O(nlogn)

    	public class Main {
    
        static Random random = new Random();
        static int[] nums = new int[]{random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100), random.nextInt(100)};
    
    
        public static void main(String[] args) {
            quickSort(nums, 0, nums.length - 1);
            for (int num : nums) {
                System.out.print(num + " ");
            }
        }
    
        public static void quickSort(int[] nums, int left, int right) {
            if (left >= right) {
                return;
            }
            //选择一个元素做基准值
            int base = nums[left];
            int i = left;
            int j = right;
            while (i < j) {
                //注意while的顺序不能颠倒,要确保找到比基准值小的数
                while (i < j && nums[j] >= base) {
                    j--;
                }
                while (i < j && nums[i] <= base) {
                    i++;
                }
                swap(nums, i, j);
            }
            swap(nums, left, i);
            quickSort(nums, left, i - 1);
            quickSort(nums, i + 1, right);
        }
    
        private static void swap(int[] nums, int i, int j) {
            int temp = nums[i];
            nums[i] = nums[j];
            nums[j] = temp;
        }
    }
    
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值