Java学习(六)基本排序算法

几种基本的排序算法

  • 冒泡排序
    冒泡排序
/**
 *   All rights Reserved, Designed By Mingyu Xiong
 *
 */

import java.util.Arrays;

/**
 * @Description:冒泡排序
 * @Author Mingyu Xiong
 * @Date 2019/07/11 22:47
 */
public class BubbleSort {
    public static void main(String[] args) {
        int a[] = {5, 4, 8, 7, 9, 3};
        BubbleSort bubbleSort = new BubbleSort();
        System.out.println(Arrays.toString(bubbleSort.bubble(a)));
    }

    public int[] bubble(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = arr.length - 1; j > i; j--) {
                if (arr[j - 1] > arr[j]) {
                    int temp;
                    temp = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        return arr;
    }
}

  • 选择排序
    选择排序
/**
 *   All rights Reserved, Designed By Mingyu Xiong
 *
 */


import java.util.Arrays;

/**
 * @Description:选择排序
 * @Author Mingyu Xiong
 * @Date 2019/07/11 16:51
 */
public class SelectionSort {
    //从数组中选择最小元素,将它与数组的第一个元素交换位置。
    // 再从数组剩下的元素中选择出最小的元素,将它与数组的第二个元素交换位置。
    // 不断进行这样的操作,直到将整个数组排序。

    public static void main(String[] args) {
        int a[] = {5, 4, 8, 7, 9, 3};
        SelectionSort selectionSort = new SelectionSort();
        System.out.println(Arrays.toString(selectionSort.selectSort(a)));
    }

    public int[] selectSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            // 用来记录最小值的索引位置,默认值为i
            int minIndex = i;

            for (int j = i + 1; j < arr.length; j++) {
                if (arr[j] < arr[minIndex]) {
                    // 遍历 i+1~length 的值,找到其中最小值的位置
                    minIndex = j;

                }
            }
            // 交换当前索引 i 和最小值索引 minIndex 两处的值
            if (i != minIndex) {
                int temp = arr[i];
                arr[i] = arr[minIndex];
                arr[minIndex] = temp;
            }
            // 执行完一次循环,当前索引 i 处的值为最小值,直到循环结束即可完成排序
        }
        return arr;
    }

}
  • 插入排序
    插入排序
    import java.util.Arrays;
    
    

/**

  • @Description:插入排序

  • @Author Mingyu Xiong

  • @Date 2019/07/12 11:16
    */
    public class InsertSort {
    public static void main(String[] args) {
    int a[] = {5, 4, 8, 7, 9, 3};
    InsertSort insertSort = new InsertSort();
    System.out.println(Arrays.toString(insertSort.insertSort(a)));
    }

    public int[] insertSort(int[] arr) {
    for (int i = 1; i < arr.length; i++) {
    int j;
    //使用中间变量temp记录arr[i]的值
    int temp = arr[i];
    //遍历数组的前i项
    for (j = i; j >0; j–) {
    if (arr[j-1] > temp) {
    //如果比temp记录的值要大,则让它的值向后移一位
    arr[j] = arr[j-1];
    } else {
    break;
    }
    }
    //直到arr[j]<=temp记录的值就将重复的值给换成temp
    arr[j] = temp;
    }
    return arr;
    }

}

```
  • 随机验证码
/**
 *   All rights Reserved, Designed By Mingyu Xiong
 *
 */

import java.util.Random;

/**
 * @Description:生成四位验证码
 * @Author Mingyu Xiong
 * @Date 2019/07/15 09:10
 */
public class RandomCode {
    public static void main(String[] args) {
        String str = "abcdefghijklmnopqrstuvwxyz"
                + "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random r = new Random();
        int index;
        String code="";
        for(int i=0;i<4;i++) {
            index = r.nextInt(str.length());
            code=code+str.charAt(index);
        }
        System.out.println(code);

    }
}
  • ArraysAPI排序
/**
 *   All rights Reserved, Designed By Mingyu Xiong
 *
 */

import java.util.Arrays;

/**
 * @Description:调用APIsort进行排序
 * @Author Mingyu Xiong
 * @Date 2019/07/12 11:14
 */
public class ArraysSort {
    public static void main(String[] args) {
        int a[] = {5, 4, 8, 7, 9, 3};
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值