- public class MyMath {
- /**
- * 隨機生成n個在down和up之間的隨機數
- */
- public static int[] Random(int down, int up, int n) {
- int[] num = new int[n];
- for(int i=0; i<n; i++) {
- num[i] = (int)(Math.random()*(up-down))+down;
- }
- return num;
- }
- /*
- * BS Bubble Sort
- */
- public static void bubbleSort(int[] arr) {
- int temp = 0;
- for(int i=0; i<arr.length-1; i++) {
- for(int j=i+1; j<arr.length; j++) {
- if(arr[i] > arr[j]) {
- temp = arr[i];
- arr[i] = arr[j];
- arr[j] = temp;
- }
- }
- }
- }
- /*
- * SS Selection Sort
- */
- public static void selectSort(int[] arr) {
- int temp = 0;
- for(int i=0; i<arr.length-1; i++) {
- int k = i;
- int curNum = arr[i];
- for(int j=i+1; j<arr.length; j++) {
- if(curNum > arr[j]) {
- curNum = arr[j];
- k = j;
- }
- }
- if(k != i) {
- temp = arr[i];
- arr[i] = arr[k];
- arr[k] = temp;
- }
- }
- }
- /*
- * Returns a binary expression of the number of the opposite sequence
- */
- public static String denaryToBinary(long n) {
- String rs = "";
- while(n != 0) {
- rs += n%2;
- n /= 2;
- }
- return rs;
- }
- }
一些小函數,排序,隨機等
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.