时代Java,与您同行!
关注微信公众号,关注前沿技术,微信搜索:nowjava或时代Java,也可点击这里扫码关注
时代Java
首页
集册
文章
实例
项目
快讯
时代+
手册
下载
Jar查找
登录
注册
Java 计算数组的中值。
Java 计算数组的中值。
欢马劈雪
工程师 (已认证)
原创分享签约作者
发表于
实例源码
订阅
418
查看 / 运行 实例源码
计算数组的中位数。
实例源码:
源代码:
执行
执行中...
//package com.nowjava;/**来 自 时 代 Java - nowjava.com**/ public class Main { public static void main(String[] argv) throws Exception { double[] v = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000, 37.1234, 67.2344, 68.34534, 69.87700 }; System.out.println(median(v)); } /** * Computes the median of an array. * If the array contains an even number of elements, * then the mean of the lower and upper medians is returned. */ public static double median(double[] v) { if (v.length == 3) { return median3(copy(v)); } else if (v.length == 5) { return median5(copy(v)); } else if (v.length == 7) { return median7(copy(v)); } else if (v.length % 2 == 1) { // odd return quickSelect(copy(v), false); } else { // even// from 时 代 J a v a - N o w J a v a . c o m double[] tmp = copy(v); double lowerMedian = quickSelect(tmp, false); double upperMedian = quickSelect(tmp, true); return (lowerMedian + upperMedian) / 2.0; } } /** * Computes the median of an array. * If the array contains an even number of elements, * the lower median is returned. */ public static int median(int[] v) { if (v.length == 3) { return median3(copy(v)); } else if (v.length == 5) { return median5(copy(v)); } else if (v.length == 7) { return median7(copy(v)); } else { return quickSelect(copy(v)); } } private static double median3(double[] v) { sortInPlace(v, 0, 1); sortInPlace(v, 1, 2); sortInPlace(v, 0, 1); return v[1]; } private static int median3(int[] v) { sortInPlace(v, 0, 1); sortInPlace(v, 1, 2); sortInPlace(v, 0, 1); return v[1]; } /** * Returns a copy of the array. */ //a call to array.clone() may also work although this is a primitive type. I haven't checked //it even may be faster public static int[] copy(int[] array) { int[] result; result = new int[array.length]; System.arraycopy(array, 0, result, 0, array.length); return result; } /** * Returns a copy of the array. */ //a call to array.clone() may also work although this is a primitive type. I haven't checked //it even may be faster public static long[] copy(long[] array) { long[] result; result = new long[array.length]; System.arraycopy(array, 0, result, 0, array.length); return result; } /** * Returns a copy of the array. */ //a call to array.clone() may also work although this is a primitive type. I haven't checked //it even may be faster public static float[] copy(float[] array) { float[] result; result = new float[array.length]; System.arraycopy(array, 0, result, 0, array.length); return result; } /** * Returns a copy of the array. */ //a call to array.clone() may also work although this is a primitive type. I haven't checked //it even may be faster public static double[] copy(double[] array) { double[] result; result = new double[array.length]; System.arraycopy(array, 0, result, 0, array.length); return result; } /** * Returns a copy of the array. */ public static double[][] copy(double[][] v) { double[][] ans = new double[v.length][]; for (int k = 0; k < v.length; k++) ans[k] = copy(v[k]); return (ans); } /** * Returns a copy of the array. */ public static int[][] copy(int[][] v) { int[][] ans = new int[v.length][]; for (int k = 0; k < v.length; k++) ans[k] = copy(v[k]); return (ans); } private static double median5(double[] v) { sortInPlace(v, 0, 1); sortInPlace(v, 3, 4); sortInPlace(v, 0, 3); sortInPlace(v, 1, 4); sortInPlace(v, 1, 2); sortInPlace(v, 2, 3); sortInPlace(v, 1, 2); return v[2]; } private static int median5(int[] v) { sortInPlace(v, 0, 1); sortInPlace(v, 3, 4); sortInPlace(v, 0, 3); sortInPlace(v, 1, 4); sortInPlace(v, 1, 2); sortInPlace(v, 2, 3); sortInPlace(v, 1, 2); return v[2]; } private static double median7(double[] v) { sortInPlace(v, 0, 5); sortInPlace(v, 0, 3); sortInPlace(v, 1, 6); sortInPlace(v, 2, 4); sortInPlace(v, 0, 1); sortInPlace(v, 3, 5); sortInPlace(v, 2, 6); sortInPlace(v, 2, 3); sortInPlace(v, 3, 6); sortInPlace(v, 4, 5); sortInPlace(v, 1, 4); sortInPlace(v, 1, 3); sortInPlace(v, 3, 4); return v[3]; } private static int median7(int[] v) { sortInPlace(v, 0, 5); sortInPlace(v, 0, 3); sortInPlace(v, 1, 6); sortInPlace(v, 2, 4); sortInPlace(v, 0, 1); sortInPlace(v, 3, 5); sortInPlace(v, 2, 6); sortInPlace(v, 2, 3); sortInPlace(v, 3, 6); sortInPlace(v, 4, 5); sortInPlace(v, 1, 4); sortInPlace(v, 1, 3); sortInPlace(v, 3, 4); return v[3]; } private static double quickSelect(double[] v, boolean upperMedian) { int low = 0, high = v.length - 1; final int median = upperMedian ? high / 2 + 1 : high / 2; int middle, ll, hh; for (;;) { if (high <= low) { /* One element only */ return v[median]; } if (high == low + 1) { /* Two elements only */ if (v[low] > v[high]) swap(v, low, high); return v[median]; } /* Find median of low, middle and high items; swap into position low */ middle = (low + high) / 2; if (v[middle] > v[high]) swap(v, middle, high); if (v[low] > v[high]) swap(v, low, high); if (v[middle] > v[low]) swap(v, middle, low); /* Swap low item (now in position middle) into position (low+1) */ swap(v, middle, low + 1); /* Nibble from each end towards middle, swapping items when stuck */ ll = low + 1; hh = high; for (;;) { do ll++; while (v[low] > v[ll]); do hh--; while (v[hh] > v[low]); if (hh < ll) break; swap(v, ll, hh); } /* Swap middle item (in position low) back into correct position */ swap(v, low, hh); /* Re-set active partition */ if (hh <= median) low = ll; if (hh >= median) high = hh - 1; } } private static int quickSelect(int[] v) { int low = 0, high = v.length - 1; final int median = high / 2; int middle, ll, hh; for (;;) { if (high <= low) { /* One element only */ return v[median]; } if (high == low + 1) { /* Two elements only */ if (v[low] > v[high]) swap(v, low, high); return v[median]; } /* Find median of low, middle and high items; swap into position low */ middle = (low + high) / 2; if (v[middle] > v[high]) swap(v, middle, high); if (v[low] > v[high]) swap(v, low, high); if (v[middle] > v[low]) swap(v, middle, low); /* Swap low item (now in position middle) into position (low+1) */ swap(v, middle, low + 1); /* Nibble from each end towards middle, swapping items when stuck */ ll = low + 1; hh = high; for (;;) { do /**代码未完, 请加载全部代码(NowJava.com).**/
编辑/阅读全部代码
执行结果:
37.1234
本文系作者在时代Java发表,未经许可,不得转载。如有侵权,请联系nowjava@qq.com删除。
编辑于
2020-03-26 09:23:27
2020-03-26 09:23:27
分享
分享文章到朋友圈
分享文章到 QQ
分享文章到微博
复制文章链接到剪贴板
扫描二维码
关注时代Java
实例源码
实例源码
订阅
订阅专栏
Java 判断文件是否为文本文件及获取文件编码格式的方法实例
bootstrap 实例演示下拉菜单(Dropdown)插件用法。
HashSet、LinkedHashSet、TreeSet类存储元素的自动排序规则实例测试
html css 对于 body和h1设置的实例源码
Java 获取在线网页的源代码
Java HashSet添加、迭代输出字符串的完整示例代码
Java 随机整数数组
html css 设置背景图片定位并且不平铺
扫描二维码
关注时代Java
返回顶部