一些小函數,排序,隨機等

來源:互聯網
上載者:User
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. }  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.