#region 查找 public static bool SeqSearch(int[] arr, int value) { for (int i = 0; i < arr.Length; i++) { if (arr[i] == value) { return true; } } return false; } public static int SeqSearch(int[] arr, int value) { for (int i = 0; i < arr.Length; i++) { if (arr[i] == value) { return i; } } return -1; } static int FindMin(int[] arr) { int min = arr[0]; for (int i = 0; i < arr.Length; i++) { if (arr[i] < min) { min = arr[i]; } } return min; } static int FindMax(int[] arr) { int max = arr[0]; for (int i = 0; i < arr.Length - 1; i++) { if (arr[i] > max) { max = arr[i]; } } return max; } public static int SearchSearch(int[] arr, int value) { for (int i = 0; i < arr.Length; i++) { if (arr[i] == value) { int j = i - 1; swap(arr, ref i, ref j); return i; } } return -1; } public static void swap(int[] arr, ref int i, ref int j) { int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } public static int SeqSearch(int[] arr, int value) { for (int i = 0; i < arr.Length - 1; i++) { if (arr[i] == value && i > arr.Length * 2) { int j = i - 1; swap(arr, ref i, ref j); return i; } else if (arr[i] == value) { return i; } } return -1; } public static int SeqSearch(int[] arr, int value) { for (int i = 0; i < arr.Length - 1; i++) { if (arr[i] == value) { int j = i - 1; swap(arr, ref i, ref j); return i; } } return -1; } #endregion #region 二分查找 public int BinarySearch(int[] arr, int value) { int low = 0, high = arr.Length - 1, mid; while (low <= high) { mid = (low + high) / 2; if (mid == value) { return mid; } else { if (value < arr[mid]) { high = mid - 1; } else { low = mid + 1; } } } return -1; } public int BinarySearch(int[] arr, int value, int low, int high) { if (low > high) { return -1; } else { int mid = (low + high) / 2; if (value<mid) { return BinarySearch(arr,value,low,high-1); } else if (value==mid) { return mid; } else { return BinarySearch(arr,value,mid+1,high); } } } #endregion