zoukankan      html  css  js  c++  java
  • 常用算法复习

    package suanfa;

    import java.lang.reflect.Array;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;

    public class Maopao {
        public static void main(String[] args) {
            int a[] = { 6, 5, 2 ,29,9,567};
            // charu(a);
            // guibinpaixu(a, 0, 2, new int [3]);
            //kuaisupaixu(a, 0, 2);
            sort(a);
            System.out.print(a[0]);
            System.out.print(a[1]);
            System.out.print(a[2]);
            System.out.print(a[3]);
            System.out.print(a[4]);
            System.out.print(a[5]);
        }

        // 基数排序 将全部数统一位数,然后按个位、十位等等排序并放在相应位置上(数组加列表);位数次循环后数组有序
        public static void sort(int[] array) {
            // 首先确定排序的趟数;
            int max = array[0];
            for (int i = 1; i < array.length; i++) {
                if (array[i] > max) {
                    max = array[i];
                }
            }
            int time = 0;
            // 判断位数;
            while (max > 0) {
                max /= 10;
                time++;
            }
            // 建立10个队列;
            List<ArrayList> queue = new ArrayList<ArrayList>();
            for (int i = 0; i < 10; i++) {
                ArrayList<Integer> queue1 = new ArrayList<Integer>();
                queue.add(queue1);
            }
            // 进行time次分配和收集;
            for (int i = 0; i < time; i++) {
                // 分配数组元素;
                for (int j = 0; j < array.length; j++) {
                    // 得到数字的第time+1位数;
                    int x = array[j] % (int) Math.pow(10, i + 1) / (int) Math.pow(10, i);
                    ArrayList<Integer> queue2 = queue.get(x);
                    queue2.add(array[j]);
                    queue.set(x, queue2);
                }
                int count = 0;// 元素计数器;
                // 收集队列元素;
                for (int k = 0; k < 10; k++) {
                    while (queue.get(k).size() > 0) {
                        ArrayList<Integer> queue3 = queue.get(k);
                        array[count] = queue3.get(0);
                        queue3.remove(0);
                        count++;
                    }
                }
            }
        }
        //冒泡排序 从第一个数开始,两两比较并交换位置,第一次选出最大数,第二次选出二大数;循环(n-1)*(n-2)...次后数组有序
        public static int[] maopaopaixu(int[] array) {
            for (int i = 0; i < array.length - 1; i++) {
                for (int j = 0; j < array.length - 1 - i; j++) {
                    if (array[j] > array[j + 1]) {
                        int temp = array[j + 1];
                        array[j + 1] = array[j];
                        array[j] = temp;
                    }
                }
            }
            return null;
        }
        //归并排序 将数组分成两个数组,将两个数组再分成四个数组,直到单个数,两两排序
        public static int[] guibinpaixu(int[] array, int first, int last, int temp[]) {
            if (first < last) {
                int mid = (first + last) / 2;
                guibinpaixu(array, first, mid, temp);
                guibinpaixu(array, mid + 1, last, temp);
                guibinSort(array, first, mid, last, temp);
            }
            return null;
        }
        //归并排序 对两个有序数组进行排序
        public static void guibinSort(int[] array, int first, int mid, int last, int temp[]) {
            int i = first, j = mid + 1;
            int m = mid, n = last;
            int k = 0;
            while (i <= m && j <= n) {
                if (array[i] <= array[j]) {
                    temp[k++] = array[i++];
                } else {
                    temp[k++] = array[j++];
                }
            }
            while (i <= m) {
                temp[k++] = array[i++];
            }
            while (j <= n) {
                temp[k++] = array[j++];
            }
            for (int k2 = 0; k2 < k; k2++) {
                array[first + k2] = temp[k2];
            }
        }
        //快速排序 选择一个基数,将大于它的排在左,小于它的排在右,经过递归,得到有序数组
        public static int[] kuaisupaixu(int[] array, int low, int hight) {
            if (low < hight) {
                int moddle = getmiddle(array, low, hight);
                kuaisupaixu(array, low, moddle - 1);
                kuaisupaixu(array, moddle + 1, hight);
            }
            return null;
        }
        //快速排序 选基准;按基准将数组排序
        public static int getmiddle(int[] array, int low, int hight) {
            int temp = array[low];
            while (low < hight) {
                while (low < hight && array[hight] >= temp) {
                    hight--;
                }
                array[low] = array[hight];
                while (low < hight && array[low] < temp) {
                    low++;
                }
                array[hight] = array[low];
            }
            array[low] = temp;
            return low;
        }
        //选择排序 选择一个最小的数为基准放在第一位,第二小放在第二位,依次排序
        public static int[] xuanzepaixu(int[] array) {
            for (int i = 0; i < array.length; i++) {
                int k = i;
                for (int j = i + 1; j < array.length; j++) {
                    if (array[j] < array[k]) {
                        k = j;
                    }
                }
                if (i != k) {
                    int temp = array[i];
                    array[i] = array[k];
                    array[k] = temp;
                }
            }
            return null;
        }
        //插入排序 第一个数为有序数组,从第二个数开始,与前面的数组进行比较插入排序,知道最后一个数
        public static int[] charu(int[] array) {
            int temp = 0;
            for (int i = 1; i < array.length; i++) {
                int j = i - 1;
                temp = array[i];
                for (; j >= 0 && temp < array[j]; j--) {
                    array[j + 1] = array[j]; // 将大于temp的值整体后移一个单位
                }
                array[j + 1] = temp;
            }
            return null;
        }

    }

  • 相关阅读:
    IOS Application生命周期
    IOS ViewController 生命周期
    nsinteger 与 int 区别
    判断日期是昨天,今天,明天,后天,其他的显示星期
    改变图片颜色(灰色显示)
    Linux系统管理
    PHP 相关软件下载
    检查域名是否正确解析网站
    定时任务
    BZOJ 3534 [Sdoi2014]重建
  • 原文地址:https://www.cnblogs.com/bigmonkeys/p/7896845.html
Copyright © 2011-2022 走看看