zoukankan      html  css  js  c++  java
  • java:Topic About(猴子吃桃,快速排序,选择排序,插入排序)

    1. 猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个 第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

    Method 1: 递归解决

    public static int peach(int day){
            int x=0;
            if(day==1){
                x=1;
            }else{
                x=2*(peach(day-1)+1);
                /* (昨天-1)的桃子=((今天-1)的桃子+1)*2s
                    大昨天的桃子=(昨天的桃子+1)*2
                 */    
            }
        return x;
        }
        public static void main(String[] args) {
            int x=peach(10);
            System.out.println(x);
        }

    Method 2:循环解决

    public static void main(String[] args) {
            int[] peach = new int[10]; 
            peach[9] = 1; 
            for (int i = 8; i >= 0; i--) {
                peach[i] = (peach[i + 1] + 1) * 2;  
                //前一天摘的桃=(剩下的桃+1)*2
            }
            System.out.println("猴子第一天摘:" + peach[0]);
    
        }

     

    2.快速排序:

      

    package zzsxt.cn;
    
    public class Test2 {
        /**
         * 
         * @description:
         * @param a
         * @param low
         * @param high
         * 
         * 快速排序的原理:选择一个关键值作为基准值。
         * 比基准值小的都在左边序列(一般是无序的),比基准值大的都在右边(一般是无序的)。
         * 一般选择序列的第一个元素。
         * 
         * 一次循环:从后往前比较,用基准值和最后一个值比较,如果比基准值小的交换位置,
         * 如果没有继续比较下一个,
         * 直到找到第一个比基准值小的值才交换。找到这个值之后,又从前往后开始比较,如果有比基准值大的,
         * 交换位置,如果没有继续比较下一个
         * ,直到找到第一个比基准值大的值才交换。直到从前往后的比较索引>从后往前比较的索引,
         * 结束第一次循环,此时,对于基准值来说,
         * 左右两边就是有序的了。
         * 
         * 接着分别比较左右两边的序列,重复上述的循环。
         * 
         */
        public static void sort(int[] a, int low, int high) {
            int start = low;
            int end = high;
            int key = a[low];
    
            while (end > start) {
                // 从后往前比较
                while (end > start && a[end] >= key){ // 如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较
                    end--;
                }
                if (a[end] <= key) {
                    int temp = a[end];
                    a[end] = a[start];
                    a[start] = temp;
                }
                // 从前往后比较
                while (end > start && a[start] <= key){// 如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置
                    start++;
                }
                
                if (a[start] >= key) {
                    int temp = a[start];
                    a[start] = a[end];
                    a[end] = temp;
                }
                // 此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
            }
            // 递归
            if (start > low)//0
                sort(a, low, start - 1);// 左边序列。第一个索引位置到关键值索引-1
            if (end < high)//arr.length-1
                sort(a, end + 1, high);// 右边序列。从关键值索引+1到最后一个
        }
        
        
        public static void main(String[] args) {
            int[] a = { 12, 20, 5, 16, 15, 1, 30, 45, 23, 9 };
            int start = 0;
            int end = a.length - 1;
            sort(a, start, end);
            for (int i = 0; i < a.length; i++) {
                System.out.println(a[i]);
            }
        }
    }

     3.选择排序:

    public class TestSelectSort {
        public static void sort(int arr[]) {
            int temp = 0;
            for (int i = 0; i < arr.length - 1; i++) {
                // 认为目前的数就是最小的, 记录最小数的下标
                int minIndex = i;
                for (int j = i + 1; j < arr.length; j++) {
                    if (arr[minIndex] > arr[j]) {
                        // 修改最小值的下标
                        minIndex = j;
                    }
                }
                // 当退出for就找到这次的最小值
                if (i != minIndex) {
                    temp = arr[i];
                    arr[i] = arr[minIndex];
                    arr[minIndex] = temp;
                }
            }
        }
    }

    4.插入排序:

      

    public class TestInsertSort {
        public static void sort(int arr[]) {
            int i, j;
            for (i = 1; i < arr.length; i++) {
                int temp = arr[i];
                for (j = i; j > 0 && temp < arr[j - 1]; j--) {
                    arr[j] = arr[j - 1];
                }
                arr[j] = temp;
            }
        }
    }
  • 相关阅读:
    Linux 共享库
    使用Visual Studio(VS)开发Qt程序代码提示功能的实现(转)
    ZOJ 3469 Food Delivery(区间DP)
    POJ 2955 Brackets (区间DP)
    HDU 3555 Bomb(数位DP)
    HDU 2089 不要62(数位DP)
    UESTC 1307 windy数(数位DP)
    HDU 4352 XHXJ's LIS(数位DP)
    POJ 3252 Round Numbers(数位DP)
    HDU 2476 String painter (区间DP)
  • 原文地址:https://www.cnblogs.com/kuangzhisen/p/7102475.html
Copyright © 2011-2022 走看看