这里是一些c#的基本算法 排序查找之类 小白必看 大佬绕道....
我会持续更新.....
/// <summary>
/// 测试快排
/// </summary>
public static void TestQuickSort(){
//调用
int[] data = new int[] {42, 20, 20, 20, 40, 23, 12, 120, 13};
QuickSort(data, 0, data.Length - 1); //-1防止下标越界
for (int i = 0; i < data.Length; i++) {
Debug.Log(data[i]);
}
}
/// <summary>
/// 快速排序
/// </summary>
/// <param name="dataArray"></param>
/// <param name="left"></param>
/// <param name="right"></param>
public static void QuickSort(int[] dataArray, int left, int right){
if (left < right) {
int x = dataArray[left];
int i = left;
int j = right;
while (i < j) {
//后标记前移
while (i < j && dataArray[j] >= x) {
j--;
}
dataArray[i] = dataArray[j];
//前标记后移
while (i < j && dataArray[i] <= x) {
i++;
}
dataArray[j] = dataArray[i];
}
//找到中心
dataArray[i] = x;
//分段递归
QuickSort(dataArray, left, i - 1);
QuickSort(dataArray, i + 1, right);
}
}
/// <summary>
/// 查找一个乱序数组中 第K大的值
/// </summary>
/// <param name="array">数组</param>
/// <param name="k">第几大的数字</param>
/// <returns></returns>
public int FindKthLargest(int[] array, int k)
{
return FindK(array, k, 0, array.Length - 1);
}
private int FindK(int[] array, int k, int start, int end)
{
int low = start;
int high = end;
int temp = array[low];
while (low < high)
{
while (low < high && array[high] <= temp)
{
high--;
}
array[low] = array[high];
while (low < high && array[low] >= temp)
{
low++;
}
array[high] = array[low];
}
array[high] = temp;
if (high == k - 1)
{
return temp;
}
else if (high > k - 1)
{
return FindK(array, k, start, high - 1);
}
else
{
return FindK(array, k, high + 1, end);
}
}
/// <summary>
/// 字符串相乘
/// </summary>
/// <param name="string ">第一个值</param>
/// <param name="string ">第二个值</param>
/// <returns></returns>
public string multiply(string num1, string num2) {
if (num1 == "0" || num2 == "0") return "0";
char[] num1CharArr = num1.ToCharArray();
char[] num2CharArr = num2.ToCharArray();
Array.Reverse(num1CharArr) ;
Array.Reverse(num2CharArr) ;
//声明一个存储数据的空list 并且初始化
List<int> sumTemp = new List<int>();
for (int i = 0; i < (num1.Length + num2.Length); i++)
{
sumTemp.Add(0);
}
for (int i = 0; i < num1CharArr.Length; i++)
{
for (int j = 0; j < num2CharArr.Length; j++)
{
sumTemp[i + j] += int.Parse((num1CharArr[i]).ToString()) * int.Parse((num2CharArr[j]).ToString());
}
}
int temp = 0;
string sumStr = "";
List<int> resTemp = new List<int>();
for (int i = 0; i < (num1.Length + num2.Length); i++)
{
resTemp.Add(0);
}
for (int i = 0; i < sumTemp.Count; i++)
{
sumTemp[i] += temp;
temp = sumTemp[i] / 10;
sumTemp[i] = sumTemp[i] % 10;
resTemp[i] = sumTemp[i];
sumStr += sumTemp[i].ToString();
}
char[] resArr = sumStr.ToCharArray();
Array.Reverse(resArr);
string resStr = new string(resArr);
int zeroInedx = 0;
while (resArr[zeroInedx] == '0') {
zeroInedx++;
}
return resStr.Substring(zeroInedx);
}
}
/// <summary>
/// 选择排序
/// </summary>
/// <param name="array">需要排序的int数组</param>
/// <param name="array">minToMax 从小到大</param>
/// <returns></returns>
public static int[] SelectSort(int[] array, bool minToMax = true) {
for (int i = 0; i < array.Length-1; i++)
{
for (int j = i+1; j < array.Length; j++)
{
if (minToMax ? array[i] > array[j] : array[i] < array[j]) {
int tempNum = array[i];
array[i] = array[j];
array[j] = tempNum;
}
}
}
return array;
}
/// <summary>
/// 冒泡排序
/// </summary>
/// <param name="array">需要排序的int数组</param>
/// <param name="array">minToMax 从小到大</param>
/// <returns></returns>
public static int[] BubbleSort (int[] array, bool minToMax = true) {
for (int i = 0; i < array.Length - 1; i++) {
for (int j = array.Length - 1; j > i; j--) {
if (minToMax ? array[j] < array[j - 1] : array[j] > array[j - 1]) {
int tempNum = array[j];
array[j] = array[j - 1];
array[j - 1] = tempNum;
}
}
}
return array;
}
/// <summary>
/// 二分查找
/// </summary>
/// <param name="array">有序的int数组</param>
/// <param name="key">要找的值</param>
/// <param name="low">开始索引 = 0</param>
/// <param name="high">结束索引= array.length </param>
/// <returns></returns>
public static int BinarySearch (int[] array, int key, int low, int high) {
if (low > high) return -1;
int tempIndex = low + (high - low) / 2;
if (array[tempIndex] == key) return tempIndex;
else if (array[tempIndex] > key) return BinarySearch (array, key, low, tempIndex - 1);
else return BinarySearch (array, key, tempIndex + 1, high);
}