求相邻元素的最大差值,先按从小到大排序最后比较

 1 /**
 2      * 找出相邻元素的最大差值
 3      * @param a
 4      * @param n
 5      * @date 2016-10-7
 6      * @author shaobn
 7      */
 8     public static void findMaxDivision(int[] a,int n){
 9         int temp = 0;
10         int count = n;
11         int dir = Integer.MIN_VALUE;
12         for(int i = 0;i<count-1;i++){
13             if(a[i]>a[i+1]){
14                 temp = a[i];
15                 a[i]= a[i+1];
16                 a[i+1] = temp;        
17             }
18             if(i==count-2){
19                 count--;
20                 i=-1;
21             }
22         }
23         for(int i=0;i<a.length-1;i++){
24             if(a[i+1]-a[i]>dir){
25                 dir = a[i+1]-a[i];
26             }
27         }
28         System.out.println(dir);
29         
30     }
31     
原文地址:https://www.cnblogs.com/assassin666/p/5936330.html