编程之美2.10之寻找数组中的最大值和最小值
来源:互联网 发布:淘宝返利链接怎么做 编辑:程序博客网 时间:2024/06/07 01:36
问题描述:寻找一个数组中的最大值和最小值。
方法一:扫描一遍数组,找出最大和最小
方法二:将相邻两个数分为一组,较大的数放在偶数位、较小的数放在奇数位。再分别从这两部分中找出最大值和最小值。
方法三:将相邻两个数分为一组,用Max和Min变量存储当前的最大值和最小值。同一数组比较之后,不交换次序,而是将下一个两个数的较大值和Max比较,较小数和Min比较。以此往后执行。
方法四:采用分治思想,分别求前后N/2个数的Min,Max 。然后取较小的Min和较大的Max
package suda.alex.chapter2;import java.util.*;public class SearchMaxMin { public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); System.out.println("please input the size of array:"); int n = scanner.nextInt(); System.out.println("please input an array:"); int a[] = new int[n]; for(int i = 0;i < n;i++){ a[i] = scanner.nextInt(); }// System.out.println("Max is:" + searchMax(a));// System.out.println("Min is:" + searchMin(a)); System.out.println("Max is:" + searchMaxMin3(a,0,n-1)[0]); System.out.println("Min is:" + searchMaxMin3(a,0,n-1)[1]); } public static int searchMax(int[] a){ int max = a[0]; for(int i = 1;i < a.length;i++){ if(a[i] > max){ max = a[i]; } } return max; } public static int searchMin(int[] a){ int min = a[0]; for(int i = 1;i < a.length;i++){ if(a[i] < min){ min = a[i]; } } return min; } public static int[] searchMaxMin1(int[] a){ int len = a.length; int[] even; int[] odd = new int[len/2]; if(len%2 == 0){ even = new int[len/2]; int count = 0; for(int i = 0;i < len-1;i+=2){ if(a[i] < a[i+1]){ int t = a[i]; a[i] = a[i+1]; a[i+1] = t; } even[count] = a[i]; odd[count] = a[i+1]; count++; } } else{ even = new int[len/2 + 1]; int count = 0; for(int i = 0;i < len-2;i+=2){ if(a[i] < a[i+1]){ int t = a[i]; a[i] = a[i+1]; a[i+1] = t; } even[count] = a[i]; odd[count] = a[i+1]; count++; } even[len/2] = a[len-1]; } int max = searchMax(even); int min = searchMin(odd); int[] result = {max,min}; return result; } public static int[] searchMaxMin2(int[] a){ int len = a.length; int min,max; if(a[0] < a[1]){ min = a[0]; max = a[1]; } else{ min = a[1]; max = a[0]; } if (len % 2 == 0) { for (int i = 2; i < len; i += 2) { if (a[i] > a[i + 1]) { if (a[i] > max) { max = a[i]; } if (a[i + 1] < min) { min = a[i + 1]; } } else { if (a[i + 1] > max) { max = a[i + 1]; } if (a[i] < min) { min = a[i]; } } } } else { for (int i = 2; i < len-2; i += 2) { if (a[i] > a[i + 1]) { if (a[i] > max) { max = a[i]; } if (a[i + 1] < min) { min = a[i + 1]; } } else { if (a[i + 1] > max) { max = a[i + 1]; } if (a[i] < min) { min = a[i]; } } if(a[len-1] < min){ min = a[len-1]; } if(a[len-1] > max){ max = a[len-1]; } } } int[] results = {max,min}; return results; } //分治思想 分别求前后N/2个数的Min,Max 然后去较小的Min和较大的Max public static int[] searchMaxMin3(int[] a, int low, int high) { int[] result = new int[2]; if (high - low <= 1) { if (a[low] < a[high]) { result[0] = a[high]; result[1] = a[low]; return result; } else { result[0] = a[low]; result[1] = a[high]; return result; } } int[] resultLeft = searchMaxMin3(a, low, low + (high-low)/2); int[] resultRight = searchMaxMin3(a, low + (high-low)/2, high); if(resultLeft[0] > resultRight[0]){ result[0] = resultLeft[0]; } else{ result[0] = resultRight[0]; } if(resultLeft[1] < resultRight[1]){ result[1] = resultLeft[1]; } else{ result[1] = resultRight[1]; } return result; }}
0 0
- 编程之美2.10 寻找数组中的最大值和最小值
- 《编程之美》2.10 寻找数组中的最大值和最小值
- 编程之美2.10 寻找数组中的最大值和最小值
- 编程之美2.10 寻找数组中的最大值和最小值
- 编程之美2.10 寻找数组中的最大值和最小值
- 编程之美 - 寻找数组中的最大值和最小值
- 【编程之美】读书笔记:寻找数组中的最大值和最小值
- 编程之美_009寻找数组中的最大值和最小值
- 编程之美 寻找数组中的最大值和最小值
- 编程之美 -- 寻找数组中的最大值和最小值
- 编程之美--寻找数组中的最大值和最小值
- 【编程之美】寻找数组中的最大值和最小值
- 编程之美读书笔记-寻找数组中的最大值和最小值
- 寻找数组中的最大值和最小值(编程之美2.10,包括最大值和次大值)
- 编程之美2.10 寻找数组的最大值和最小值
- 读书笔记之编程之美 - 2.10 寻找数组中的最大值和最小值
- 编程之美:第二章 数字之魅 2.10寻找数组中的最大值和最小值
- 编程之美2.10之寻找数组中的最大值和最小值
- Auto packing the repository in background for optimum performance
- //设置键盘移动位置 一般用于适配
- PHP上传文件类型
- 支持向量机 - 5 - SMO算法
- RHEL系统部署网络yum源
- 编程之美2.10之寻找数组中的最大值和最小值
- 乐视生态O2O:全线出击,锋芒毕现
- Android从菜鸟到菜鸟:dp与px转换
- IOS 绘图实战之“图片添加文字或者水印”
- Android实现静默安装与卸载
- [转]UIScrollView的属性总结
- ubuntu下访问串口
- NSTimer
- 容易理解的视觉词袋模型的matlab编程实现