现在的位置: 首页 > 综合 > 正文

一些小函数,排序,随机等

2013年08月24日 ⁄ 综合 ⁄ 共 1538字 ⁄ 字号 评论关闭
Code:
  1. public class MyMath {   
  2.     /**  
  3.     *   随机生成n个在down和up之间的随机数  
  4.     */  
  5.     public static int[] Random(int down, int up, int n) {   
  6.         int[] num = new int[n];   
  7.         for(int i=0; i<n; i++) {   
  8.             num[i] = (int)(Math.random()*(up-down))+down;   
  9.         }   
  10.         return num;   
  11.     }   
  12.     /*  
  13.     *   BS Bubble Sort  
  14.     */  
  15.     public static void bubbleSort(int[] arr) {   
  16.         int temp = 0;   
  17.         for(int i=0; i<arr.length-1; i++) {   
  18.             for(int j=i+1; j<arr.length; j++) {   
  19.                 if(arr[i] > arr[j]) {   
  20.                     temp = arr[i];   
  21.                     arr[i] = arr[j];   
  22.                     arr[j] = temp;   
  23.                 }   
  24.             }   
  25.         }   
  26.     }   
  27.     /*  
  28.     *   SS Selection Sort  
  29.     */  
  30.     public static void selectSort(int[] arr) {   
  31.         int temp = 0;   
  32.         for(int i=0; i<arr.length-1; i++) {   
  33.             int k = i;   
  34.             int curNum = arr[i];   
  35.             for(int j=i+1; j<arr.length; j++) {   
  36.                 if(curNum > arr[j]) {   
  37.                     curNum = arr[j];   
  38.                     k = j;   
  39.                 }   
  40.             }   
  41.             if(k != i) {   
  42.                 temp = arr[i];   
  43.                 arr[i] = arr[k];   
  44.                 arr[k] = temp;   
  45.             }   
  46.         }   
  47.     }   
  48.     /*  
  49.     *   Returns a binary expression of the number of the opposite sequence  
  50.     */  
  51.     public static String denaryToBinary(long n) {   
  52.         String rs = "";   
  53.         while(n != 0) {   
  54.             rs += n%2;   
  55.             n /= 2;   
  56.         }   
  57.         return rs;   
  58.     }   
  59. }  

抱歉!评论已关闭.