/// <summary> /// 二分查找 /// </summary> /// <param name="arr"></param> /// <param name="num"></param> /// <returns></returns> public static int find(int[] arr, int num) { int l = 0, h = arr.Length - 1; while (l<=h) { int middle = (l + h) / 2; if (arr[middle] == num) return num; if (arr[middle] > num) { h = middle - 1; } else { l = middle + 1; } } return -1; } /// <summary> /// 快速排序 /// </summary> /// <param name="arr"></param> /// <param name="l"></param> /// <param name="r"></param> /// <returns></returns> public static int[] FastSort(int[] arr, int l, int r) { int i = l, j = r; int radom = arr[(l + r) / 2]; while (i <= j) { while (arr[i] < radom) { i++; } while (arr[j] > radom) { j--; } if (i <= j) { int temp; temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; i++; j--; } } if (l < j) { FastSort(arr, l, j); } if (r > i) { FastSort(arr, i, r); } return arr; } /// <summary> /// 冒泡排序 /// </summary> /// <param name="arr"></param> /// <returns></returns> public static int[] BubblingSort(int[] arr) { for (int i = 0; i < arr.Length - 1; i++) { for (int j = 0; j < arr.Length - 1 - i; j++) { if (arr[j] > arr[j + 1]) { int temp; temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } return arr; }