Java+冒泡排序+选择排序+插入排序+快速排序+实现

排序总结

冒泡排序

// 冒泡排序
    public static int[] bubbleSort(int[] nums){
        int i,j,temp,n = nums.length;
        boolean flag;
        for(i=0;i<n-1;i++){
            flag = true;
            for(j=0;j<n-i-1;j++){
                if(nums[j]>nums[j+1]){
                    temp = nums[j];
                    nums[j]=nums[j+1];
                    nums[j+1]=temp;
                    flag = false;
                }
            }
            if(flag){
                break;
            }
        }
        return nums;
    }

选择排序

// 选择排序
    public static int[] selectionSort(int[] nums){
        int i,j,tempIndex,temp,n = nums.length;
        for(i=0;i<n-1;i++){
            tempIndex = i;
            for(j=i+1;j<n;j++){
                if(nums[tempIndex]>nums[j]){
                    tempIndex = j;
                }
            }
            temp = nums[i];
            nums[i] = nums[tempIndex];
            nums[tempIndex] = temp;
        }
        return nums;
    }

插入排序

// 插入排序
    public static int[] insertionSort(int[] nums){
        int i,j,temp,indexTemp,n=nums.length;
        for(i=1;i<n;i++){
            temp = nums[i];
            indexTemp = i-1;
            while (indexTemp>=0 && nums[indexTemp]>temp){
                nums[indexTemp+1]=nums[indexTemp];
                indexTemp--;
            }
            nums[indexTemp+1]=temp;
        }
        return nums;
    }

快速排序

 // 快速排序
    public static int[] quickSort(int[] nums,int left,int right){
        int l = left,r = right;
        int temp;
        if(left>=right){
            return nums;
        }
        temp = nums[left];
        while (left<right){
            while(nums[right]>=temp && left<right){
                right--;
            }
            // 交换值
            if(left<right){
                nums[left++]=nums[right];
            }
            while(nums[left]<=temp && left<right){
                left++;
            }
            if(left<right){
                nums[right--]=nums[left];
            }
        }
        nums[left]=temp;
        nums = quickSort(nums,l,right-1);
        nums = quickSort(nums,left+1,r);
        return nums;
    }

结果

    public static void main(String[] args) {
        int[] nums = {9,8,7,5,6,1,2,8,0,3,10,1,6};;
        //printArray(quickSort(nums,0,nums.length-1));
        //printArray(insertionSort(nums));
        //printArray(selectionSort(nums));
        //printArray(bubbleSort(nums));
        // 0 1 1 2 3 5 6 6 7 8 8 9 10
        // 0 1 1 2 3 5 6 6 7 8 8 9 10
        // 0 1 1 2 3 5 6 6 7 8 8 9 10
        // 0 1 1 2 3 5 6 6 7 8 8 9 10
        // 0 1 1 2 3 5 6 6 7 8 8 9 10
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值