标签:print system math class bre dma rgs void i+1
1、对整型排序(这里使用堆排序)
//堆排序 private static void headSort(int arr[], int len) { int s = len / 2; for (int i = s; i >= 0; i--) { hSort(arr, i, len); } for (int i = len; i >0 ; i--) { swap(arr, 0 , i); hSort(arr, 0, i-1); } } private static void swap(int arr[], int s, int n) { int t = arr[s]; arr[s] = arr[n]; arr[n] = t; } private static void hSort(int arr[], int s, int len) { int v = arr[s]; for (int i = 2 *s + 1; i < len; i*=2) { if (i < len && arr[i] < arr[i+1]) { i = i + 1; } if (v >= arr[i]) break; arr[s] = arr[i]; s = i; } arr[s] = v; }
2、最大值只能是最小两个和最大一个或者最大三个值的乘积
private static int findMaxSub(int arr[], int len) { return Math.max(arr[0] * arr[1] * arr[len-1], arr[len - 3] * arr[len - 2] * arr[len - 1]); }
3、测试
public static void main(String[] args) { int arr[] = {-3, -5, -8, 0, 2, 4, 6}; headSort(arr, arr.length - 1); System.out.println(findMax(arr, arr.length)); }
标签:print system math class bre dma rgs void i+1
原文地址:https://www.cnblogs.com/junxing/p/11074803.html