java常见的算法

一、 排序
1.冒泡排序:数组中越大或越小的元素会经由交换慢慢“浮”到数列的顶端,故名冒泡排序。

// 冒泡排序
    public int[] bubbleSort(int[] a) {
        int temp = 0;
        for (int i = 0; i < a.length; i++) {

            for (int j = i + 1; j < a.length; j++) {
                if (a[i] < a[j]) {
                    temp = a[i];
                    a[i] = a[j];
                    a[j] = temp;

                }
            }
        }
        return a;

    }

2.选择排序:工作原理是每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完。

// 选择排序
    public int[] selectSort(int[] a) {
        int minIndex = 0;
        int temp = 0;

        for (int i = 0; i < a.length - 1; i++) {
            minIndex = i;// 无序区的最小数据数组下标
            for (int j = i + 1; j < a.length; j++) {
                // 在无序区中找到最小数据并保存其数组下标
                if (a[j] < a[minIndex]) {// 2 1 3 5
                    minIndex = j;

                }
            }

            if (minIndex != i) {
                // 如果不是无序区的最小值位置且不是默认的第一个数据,则交换之。
                temp = a[i];
                a[i] = a[minIndex];
                a[minIndex] = temp;

            }
        }
        return a;
    }

3.插入排序:每步将一个待排序的记录按其关键字的大小插到前面已经排序的序列中的适当位置,直到全部记录插入完毕为止。

// 直接插入排序
    public int[] insertSort(int[] a) {

        for (int i = 1; i < a.length; i++) {
            // 待插入元素
            int temp = a[i];// 2 4 1 5
                            // 2 4 4 5
                            // 1 2 4 5
            int j;
            for (j = i - 1; j >= 0; j--) {
                // 将大于temp的往后移动一位
                if (a[j] > temp) {
                    a[j + 1] = a[j];

                } else {
                    break;

                }
            }
            System.out.println("j:  " + j);
            a[j + 1] = temp;// 插入进来

        }
        return a;

    }


二、二分查找:其思想是将n个元素分成大致相等的两部分,取a[n/2]与x(需要查找的元素)做比较,如果x=a[n/2],则找到x,算法中止;如果x

public int binarySearch(int[] array, int elem) {
        if (array.length == 0) {
            return -1;

        }

        int low = 0;
        int high = array.length - 1;
        int middle = 0;
        while (low <= high) {
            middle = (high + low) / 2;
            if (array[middle] < elem) {
                low = middle + 1;// {1,2,3,6};

            } else if (array[middle] > elem) {
                high = middle - 1;

            } else {
                return middle;// 找到数据

            }

        }

        return -1;
    }

三、递归:函数在其定义或说明中有直接或间接调用自身的一种方法,以下运用了递归算法实现的二分查找:

// 利用递归的二分查找
    public int binarySearch(int[] array, int elem, int low, int high) {
        if (array.length == 0) {
            return -1;

        }
        int middle = (high + low) / 2;
        if (low <= high) {
            if (array[middle] < elem) {
                low = middle + 1;// {1,2,3,6};
                return binarySearch(array, elem, low, high);

            }
            if (array[middle] > elem) {
                high = middle - 1;
                return binarySearch(array, elem, low, high);

            }
            if (array[middle] == elem) {
                return middle;// 找到数据

            }

        }

        return -1;
    }

四、穷举:穷举法的基本思想是在一定范围内对所有可能的情况逐一验证,直到找到答案。以下是运用穷举法破解密码的实例:

/**
 * 穷举
 * 
 * @author zeng
 *
 */
public class Exhaustive {
    private char[] originPwd = new char[] { '1', '9', '4' };// 正确的密码,长度为三
    private StringBuilder sb = new StringBuilder();
    private char[] pwd = new char[3];

    public Exhaustive() {
        for (int i = 0; i <= 9; i++) {
            sb.append(i);

        }
    }

    // 穷举法,破解密码
    public int exhaustion() {

        for (int i = 0; i < sb.length(); i++) {
            pwd[0] = sb.charAt(i);
            for (int j = 0; j < sb.length(); j++) {
                pwd[1] = sb.charAt(j);
                for (int k = 0; k < sb.length(); k++) {
                    pwd[2] = sb.charAt(k);
                    //转化成字符串比较内容
                    String str1 = new String(pwd);
                    String str2 = new String(originPwd);
                    if (str1.equals(str2) ) {
                        System.out.print("找到密码");
                        return 0;// 找到密码

                    }

                }

            }

        }

        System.out.print("没有找到密码");
        return -1;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值