`
gmleegmlee
  • 浏览: 116784 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
文章列表
public class ShellSort { public static void shellSort(Comparable[] data){ int j; for(int gap = data.length / 2; gap > 0; gap /= 2){ for(int i = gap; i < data.length; i ++){ Comparable tmp = data[i]; for(j = i; j >= gap && tmp.compareTo(data[j - gap] ...
public class InsertionSort2 { public static void insertSort(Comparable[] data){ int j; for(int p = 1; p < data.length; p++){ Comparable key = data[p]; for(j = p; j > 0 && (key.compareTo(data[j-1]) < 0); j--) data[j] = data[j-1]; data[j] = key; } } ...
//计算两个随机小于等于n的互异正整数互素 的 概率, public class ProbRelPrim { public static double probRelPrim(int n){ int rel =0; int tot = 0; for(int i = 1; i <= n; i++) for(int j = i + 1; j <= n; j++){ tot++; if(gcd(i, j) == 1)//最大公约数为一,则互素, rel++; } return (double ...
//幂运算, public class POW { public static long pow(long x, int n){ if(n == 0) return 1; if(n == 1) return x; if(isEven(n))//是否 偶数, return pow(x * x, n / 2); else return pow(x * x, (n - 1) / 2) * x; } public static boolean isEven(int n){ if(n % 2 == 0) ...
//欧几里得算法,计算最大公因数, public class GCD { public static long gcd(long m, long n){ while(n != 0){ long rem = m % n; m = n; n = rem; } return m; } public static void main(String[] args){ long x = 1989; long y = 1590; long z = gcd(x,y); System.out.pri ...
//折半查找,二分查找, public class BinarySearch { public static int binarySearch(int[] a, int x){ int low = 0; int high = a.length - 1; while(low <= high){ int mid = (low + high) / 2; if(a[mid] > x) high = mid - 1; else if(a[mid] < x) low = mid + 1; ...
//求最大子序列和问题, public class MaxSubSum{ public static int maxSubSum(int[] a){ int maxSum = 0, thisSum = 0; for(int j = 0; j < a.length; j++){ thisSum += a[j]; if(thisSum > maxSum) maxSum = thisSum; else if(thisSum < 0) thisSum = 0; } re ...
Global site tag (gtag.js) - Google Analytics