简单算法

算法

冒泡排序

package argorithm;

import java.util.Arrays;

public class Bubble {
    public static void main(String[] args) {
        int[] xx=new int[]{10,2,3,44,32,34,24,65,787,23};
        Bubble.bubble(xx);

    }


    public static void bubble(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = nums.length - 1 ; j > i ; j --) {
                if (nums[j] < nums[j - 1]) {
                    int temp = nums[j];
                    nums[j] = nums[j - 1];
                    nums[j - 1] = temp;

                }


            }

        }
        System.out.println(Arrays.toString(nums));


    }
}

插入排序

package argorithm;

import java.util.Arrays;

public class InsertSort {
    public static void main(String[] args) {
        insertSort(new int[]{321, 3214, 235, 3, 6234, 345, 45, 623, 4456, 123});
    }
    public static void insertSort(int[] nums) {
        for (int i = 1; i < nums.length; i++) {
            int temp = nums[i];
            int j = i;
            while (j > 0 && nums[j-1] >= temp) {
                nums[j] = nums[j - 1];
                j--;
            }
            nums[j] = temp;
        }
        System.out.println(Arrays.toString(nums));
    }
}

package argorithm;

import java.util.Arrays;

public class StraightSectionSort {
    public static void main(String[] args) {
        sectionSort(new int[]{100,3,4,59,235,56,234,45,234,456123,4546,34});
    }


    public static void sectionSort(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            int  k=i;
            for (int j = i +1; j < nums.length; j++) {
                if (nums[k] < nums[j]) {
                    k = j;
                }
            }
            int temp = nums[i];
            nums[i]=nums[k];
            nums[k]=temp;
        }
        System.out.println(Arrays.toString(nums));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值