zoukankan      html  css  js  c++  java
  • 第2章 数字之魅——寻找数组中的最大值和最小值

    寻找数组中的最大值和最小值

    问题描述

      对于一个由N个整数组成的数组,需要比较多少次才能把最大和最小的数找出来呢?

    分析与解法

    【解法一】

      可以把寻找数组中的最大值和最小值看成是两个独立的问题,我们只要分别求出数组的最大值和最小值即可解决问题。最直接的做法是先扫描一遍数组,找出最大的数以及最小的数。这样,我们需要比较2*(N-1)次才能找出最大的数和最小的数。代码如下:

     1 package chapter2shuzizhimei.findminmax;
     2 /**
     3  * 寻找数组中的最大值和最小值
     4  * 【解法一】
     5  * @author DELL
     6  *
     7  */
     8 public class FindMinAndMax {
     9     //寻找数组a中的最大值和最小值
    10     public static void findMinMax(int[] a){
    11         int min = a[0];
    12         int max = a[0];
    13         for(int i=0;i<a.length;i++){
    14             if(a[i]<min)
    15                 min = a[i];
    16             else if(a[i]>max)
    17                 max = a[i];
    18         }
    19         System.out.println("最小值为:"+min);
    20         System.out.println("最大值为:"+max);
    21     }
    22     public static void main(String[] args) {
    23         int a[] = {5, 6, 8, 3, 7, 9};
    24         findMinMax(a);
    25     }
    26 
    27 }

    程序运行结果如下:

    最小值为:3
    最大值为:9

    【解法二】

     

    代码如下:

     1 package chapter2shuzizhimei.findminmax;
     2 /**
     3  * 寻找数组中的最大值和最小值
     4  * 【解法二】
     5  * @author DELL
     6  *
     7  */
     8 public class FindMinAndMax2 {
     9     //寻找数组a中的最大值和最小值
    10     public static void findMinMax(int[] a){
    11         int temp;         //实现相邻的偶数位比奇数位大
    12         for(int i=0;i<a.length;i=i+2){
    13             if(a[i]<a[i+1]){
    14                 temp = a[i];
    15                 a[i] = a[i+1];
    16                 a[i+1] = temp;
    17             }
    18         }
    19         int min = a[1];
    20         int max = a[0];
    21         for(int i=0;i<a.length;i=i+2){
    22             if(a[i]>max)
    23                 max = a[i];
    24             if(a[i+1]<min)
    25                 min = a[i+1];        
    26         }
    27         System.out.println("最小值为:"+min);
    28         System.out.println("最大值为:"+max);
    29     }
    30     public static void main(String[] args) {
    31         int a[] = {5, 6, 8, 3, 7, 9};
    32         findMinMax(a);
    33     }
    34 
    35 }

    程序运行结果如下:

    最小值为:3
    最大值为:9

     【解法三】

    代码如下:

     1 package chapter2shuzizhimei.findminmax;
     2 /**
     3  * 寻找数组中的最大值和最小值
     4  * 【解法三】
     5  * @author DELL
     6  *
     7  */
     8 public class FindMinAndMax3 {
     9     //寻找数组a中的最大值和最小值
    10     public static void findMinMax(int[] a){
    11         int min = a[0];
    12         int max = a[0];
    13         for(int i=0;i<a.length;i=i+2){
    14             if(a[i]>=a[i+1]){
    15                 if(a[i]>max)
    16                     max = a[i];
    17                 if(a[i+1]<min)
    18                     min = a[i+1];    
    19             }else{
    20                 if(a[i]<min)
    21                     min = a[i];
    22                 if(a[i+1]>max)
    23                     max = a[i+1];    
    24             }
    25     
    26         }
    27         System.out.println("最小值为:"+min);
    28         System.out.println("最大值为:"+max);
    29     }
    30     public static void main(String[] args) {
    31         int a[] = {5, 6, 8, 3, 7, 9};
    32         findMinMax(a);
    33     }
    34 
    35 }

    程序运行结果如下:

    最小值为:3
    最大值为:9

     【解法四】分治思想

    具体代码如下:

     1 package chapter2shuzizhimei.findminmax;
     2 /**
     3  * 寻找数组中的最大值和最小值
     4  * 【解法四】分治思想
     5  * @author DELL
     6  *
     7  */
     8 public class FindMinAndMax4 {
     9     //定义类二元组
    10     public static class Tuple{
    11         public int min;
    12         public int max;
    13         public Tuple(int min, int max){
    14             this.min = min;
    15             this.max = max;
    16         }
    17     }
    18     //寻找数组a中的最大值和最小值
    19     public static Tuple findMinMax(int[] a, int first, int last){
    20         if(last-first<=1){
    21             if(a[first]<a[last])
    22                 return new Tuple(a[first],a[last]);
    23             else
    24                 return new Tuple(a[last],a[first]);
    25         }
    26         Tuple lTuple = findMinMax(a,first,first+(last-first)/2);
    27         Tuple rTuple = findMinMax(a,first+(last-first)/2+1,last);
    28         int min,max;
    29         if(lTuple.min<rTuple.min)
    30             min = lTuple.min;
    31         else
    32             min = rTuple.min;
    33         if(lTuple.max>rTuple.max)
    34             max = lTuple.max;
    35         else
    36             max = rTuple.max;
    37         return new Tuple(min,max);
    38     }
    39     public static void main(String[] args) {
    40         int a[] = {5, 6, 8, 3, 7, 9};
    41         Tuple t = findMinMax(a,0,a.length-1);
    42         System.out.println("最小值为:"+t.min);
    43         System.out.println("最大值为:"+t.max);
    44     }
    45 
    46 }

    程序运行结果如下:

    最小值为:3
    最大值为:9
  • 相关阅读:
    二分 || UOJ 148 跳石头
    等边n边型
    激光样式
    n个数中选k个数和为sum
    引爆炸弹
    光盘行动
    (二分)分蛋糕问题
    总结
    个人测试
    第三次团队作业
  • 原文地址:https://www.cnblogs.com/gaopeng527/p/4624176.html
Copyright © 2011-2022 走看看