java 算法

1.copy

public class Copy {
    public static void main(String[] args) {
        int[] a = {3,5,6,87,98,9};
        System.out.println("------手工复制-------");
        int[] b = new int[a.length];
        
        for (int i=0;i<a.length;i++) {
            b[i] = a[i];
        }
        
        System.out.println("-----Arrays.copyOf复制----");
        //1参数:源数组,2参数:复制的长度,从0位置开始复制
        int[] c = Arrays.copyOf(a, 4);
        for (int i : c) {
            System.out.println(i);
        }
        System.out.println("---System.arrayCopy-------");
        int[] d = new int[10];
        //源数组,源数组开始复制的索引位置,目标数组,目标数组的开始插入索引位置,要复制的长度
        System.arraycopy(a,1, d, 3, 3);
        for (int i : d) {
            System.out.println(i);
        }
        
        
    }

}
View Code

2.foreaach遍历

public class Demo {
    public static void main(String[] args) {
        int[] a = {1,2,3,4,7,65};
        
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        
        System.out.println("------foreach循环,用来对集合进行遍历(迭代)-----");
        for (int temp : a) {
            System.out.println(temp);
        }
        
        
        Student[] ss = new Student[3];
        ss[0]  =new Student(1,"fyt");
        ss[1] = new Student(2,"bbb");
        ss[2] = new Student(3,"ccc");
        for (Student stu : ss) {//声明一个Student类型的变量接收数组中每一个元素
            String name = stu.getName();
            int id = stu.getId();
            System.out.println(id+"--"+name);
        }
        
        
    }

}
View Code

3.查找

public class Query1 {
    public static void main(String[] args) {
        int[] a = {6,7,9,4,1,5,3};
        
        //查找某个元素在数组中的位置
        Query1 q  = new Query1();
        int index = q.query(a,10);
        if(index>=0)
            System.out.println("位置:"+index);
        else
            System.out.println("不存在");
    }

    public int query(int[] a,int target)
    {
        int result = -1;
        
        for (int i = 0; i < a.length; i++) {
            if(a[i]==target)
            {
                result = i;
                break;
            }
        }
        
        return result;
    }
}
View Code
/**
 * 二分查找
 * 前提是:数组已排序
 * @author Administrator
 *
 */
public class Query2 {
    public static void main(String[] args) {
        int[] a = { 6, 7, 9, 4, 1, 5, 3 };
        // 1,3,4,5,6,7,9
        Arrays.sort(a);
        // 去数组中查找target变量的值,是否已存在

        int index = query(a,1);
        System.out.println(index);
    }

    public static int query(int[] a, int target) {
        int index = -1;

        int begin = 0;
        int end = a.length - 1;

        while (begin <= end) {
            int middle = (begin + end) / 2;
            if (a[middle] > target) {
                end = middle - 1;
            } else if (a[middle] < target) {
                begin = middle + 1;
            } else {
                index = middle;
                break;
            }
        }

        return index;
    }

}
View Code
/**
 * 快速二分查找
 * @author Administrator
 *
 */
public class Query3 {
    public static void main(String[] args) {
        int[] a = { 6, 7, 9, 4, 1, 5, 3 };
        // 1,3,4,5,6,7,9
        Arrays.sort(a);
        
        
        // Arrays.binarySearch此方法返回值:找到的话返回索引号;找不到的话,查找数据的插入位置为n, 返回-n-1
        int index = Arrays.binarySearch(a, 0);
        System.out.println(index);
        
        //toString方法,返回格式化数组
        String s= Arrays.toString(a);
        System.out.println(s);
    }

}
View Code

4.排序

/**
 * 冒泡排序 外层n-1,内层n-1-i
 * 
 * @author Administrator
 *
 */
public class Sort1 {
    public static void main(String[] args) {
        int[] a = { 6, 4, 3, 7, 8, 2 };
        // 对a数组进行升序排列
        // 6,4,3,7,8,2
        // 4,6,3,7,8,2--4,3,6,7,8,2--4,3,6,7,8,2--4,3,6,7,8,2--4,3,6,7,2,8
        // 4,3,6,7,2,8
        // 3,4,6,7,2,8--3,4,6,7,2,8--3,4,6,7,2,8--3,4,6,2,7,8
        // 3,4,6,2,7,8
        // 3,4,6,2,7,8--3,4,6,2,7,8--3,4,2,6,7,8
        // 3,4,2,6,7,8
        // 3,4,2,6,7,8--3,2,4,6,7,8
        // 3,2,4,6,7,8
        // 2,3,4,6,7,8

        sort(a);

        for (int i : a) {
            System.out.println(i);
        }
    }

    public static void sort(int[] a) {
        int n = a.length;// 6
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1 - i; j++) {
                if (a[j] > a[j + 1]) // 左侧数据大于右侧,交换位置,大的放右边
                {
                    int temp = a[j];
                    a[j] = a[j + 1];
                    a[j + 1] = temp;
                }
            }
        }
    }

}
View Code
/**
 * 选择排序
 * 
 * @author Administrator
 *
 */
public class Sort2 {
    public static void main(String[] args) {
        int[] a = { 6, 4, 3, 7, 8, 2 };
        // 6, 4, 3, 7, 8, 2
        // k=0 --k=1--k=2--k=2--k=2--k=5 2, 4, 3, 7, 8, 6
        // 2, 4, 3, 7, 8, 6
        // k=1 --k=2--k=2--k=2--k=2 2,3,4,7,8,6
        // 2,3,4,7,8,6
        // k=2 --k=2--k=2--k=2 2,3,4,7,8,6
        // 2,3,4,7,8,6
        // k=3 --k=3--k=5 2,3,4,6,8,7
        // 2,3,4,6,8,7
        // k=4 --k=5 2,3,4,6,7,8
        
        sort(a);
        
        for (int i : a) {
            System.out.println(i);
        }
    }

    public static void sort(int[] a) {
        int n = a.length;
        for (int i = 0; i < n - 1; i++) {
            int k = i;

            for (int j = i + 1; j < n; j++) {// 1-2-3-4-5
                if (a[k] > a[j]) {
                    k = j;
                }
            }

            if (k != i) {
                int temp = a[i];
                a[i] = a[k];
                a[k] = temp;
            }
        }
    }
}
View Code
/**
 * 插入排序
 * @author Administrator
 *
 */
public class Sort3 {
    public static void main(String[] args) {
        int[] a = { 6, 4, 3, 7, 8, 2 };
        // yilun:6, 4, 3, 7, 8, 2
        // 4, 6, 3, 7, 8, 2 一次
        // erlun:4, 6, 3, 7, 8, 2
        // 4,3,6,7, 8, 2--3,4,6,7, 8, 2 二次
        // sanlun:3,4,6,7, 8, 2
        // 3,4,6,7, 8, 2 三次
        // ..wulun

        sort(a);
        for (int i : a) {
            System.out.println(i);
        }
    }

    public static void sort(int[] a) {
        int n = a.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j > 0; j--) {// 2
                if (a[j] < a[j - 1]) {
                    int temp = a[j];
                    a[j] = a[j - 1];
                    a[j - 1] = temp;
                } else
                    break;
            }
        }
    }

}
View Code
/**
 * 快速排序 使用数组工具类Arrays排序
 * 
 * @author Administrator
 *
 */
public class Sort4 {
    public static void main(String[] args) {
        int[] a = { 6, 4, 3, 7, 8, 2 };

        System.out.println("Arrays.sort()升序排");
        Arrays.sort(a);
        for (int i = a.length-1; i >=0; i--) {
            System.out.println(a[i]);
        }
        
        System.out.println("倒序");
        Integer[] a2 = { 6, 4, 3, 7, 8, 2 };// 对象数组
        Arrays.sort(a2, Collections.reverseOrder());
        for (int i : a2) {
            System.out.println(i);
        }
    }

}
View Code
/**
 * 对象数组,按照对象中某个属性进行排序
 * @author Administrator
 *
 */
public class Sort5 {
    public static void main(String[] args) {
        Person[] ps = new Person[4];
        ps[0] = new Person("b", 23);
        ps[1] = new Person("a", 21);
        ps[2] = new Person("d", 25);
        ps[3] = new Person("c", 19);

        //对ps数组,按照人的年龄进行升序排列
        int n = ps.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - 1 - i; j++) {
                Person p1 = ps[j];
                Person p2 = ps[j+1];
                if(p1.getAge()>p2.getAge())
                {
                    ps[j] = p2;
                    ps[j+1] = p1;
                }
            }
        }
        
        
        for (Person person : ps) {
            System.out.println(person.getName()+"--"+person.getAge());
        }

    }

}
View Code
今天多一点积累,明天少一分烦恼
原文地址:https://www.cnblogs.com/galibujianbusana/p/5712308.html