算法基础贴

/// <summary>
/// 冒泡排序
/// </summary>
public void BubbleSort(int[] MyArray)
{
    for (int j = 1; j < MyArray.Length; j++)
    {
        for (int i = 0; i < MyArray.Length - 1; i++)
        {
            if (MyArray[i] > MyArray[i + 1])
            {
                int temp = MyArray[i];
                MyArray[i] = MyArray[i + 1];
                MyArray[i + 1] = temp;
            }
        }
    }
}

 


/// <summary>
/// 选择排序
/// </summary>
public void SelectedSort(int[] MyArray)
{
    int min;

    for (int i = 0; i < MyArray.Length - 1; i++)
    {
        min = i;
        for (int j = i + 1; j < MyArray.Length; j++)
        {
            if (MyArray[j] < MyArray[min])
            {
                min = j;
            }
        }
        int t = MyArray[min];
        MyArray[min] = MyArray[i];
        MyArray[i] = t;
    }
}

 


/// <summary>
/// 插入排序
/// </summary>
/// <param name="list"></param>
public void InsertionSort(int[] list)
{
    for (int i = 1; i < list.Length; i++)
    {
        int t = list[i];
        int j = i;
        while ((j > 0) && (list[j - 1] > t))
        {
            list[j] = list[j - 1];
            --j;
        }
        list[j] = t;
    }
}

 

/// <summary>
/// 删除数组重复的值
/// </summary>
public void DuplicateArray()
{
    int[] array = new int[] { 1, 2, 2, 3, 7 };
    ArrayList list = new ArrayList();

    for (int i = 0; i < array.Length; i++)
    {
        if (list.IndexOf((int)(array[i])) == -1)
        {
            list.Add((int)(array[i]));
        }
    }
}
/// <summary>
/// 不用中间变量交换两变量的值
/// </summary>
public void ExchangeData()
{
    int a = 10; int b = 15;
    a = a + b;
    b = a - b;
    a = a - b;
    Console.WriteLine(a);
    Console.WriteLine(b);
}
/// <summary>
/// 判断两个数组中是否存在相同的数字
/// </summary>
public void FindCommon()
{
    int[] arr1 = new int[] { 1, 2, 3, 4, 5, 6 };
    int[] arr2 = new int[] { 7, 8, 9, 10 };

    int i = 0, j = 0;
    while (i < arr1.Length && j < arr2.Length)
    {
        if (arr1[i] == arr2[j])
        {
            Console.WriteLine("true");
            return;
        }
        if (arr1[i] < arr2[j])
        {
            i++;
        }
        if (arr1[i] > arr2[j])
        {
            j++;
        }
    }
    Console.WriteLine("false");
}
/// <summary>
/// 判断回文
/// </summary>
public bool Palindrome(string str)
{
    for (int i = 0; i < str.Length / 2; i++)
    {
        string head = str[i].ToString();
        string tail = str[str.Length - i - 1].ToString();
        if (head != tail)
        {
            Console.WriteLine("Not palindrome.");
            return false;
        }
    }
    Console.WriteLine("yes");
    return true;
}
/// <summary>
/// 1、1、2、3、5、8、13、21、34...... 求第30位数是多少
/// </summary>
/// <param name="i"></param>
public int Foo(int i)
{
    if (i <= 0)
    {
        return 0;
    }
    else if (i > 0 && i <= 2)
    {
        return 1;
    }
    else
    {
        return Foo(i - 1) + Foo(i - 2);
    }
}
/// <summary>
/// 求最大子序列
/// </summary>
public void FindSubArray()
{
    int total = 0, toll = 0;

    int[] array = new int[] { -5, 10, 5, 1, -5, 6 };

    for (int i = 0; i < array.Length; i++)
    {
        if (toll + array[i] > total)
        {
            total = toll = toll + array[i];
        }
        else if (toll + array[i] > 0)
        {
            toll = toll + array[i];
        }
        else
        {
            toll = 0;
        }

    }

    Console.WriteLine(total);
}
/// <summary>
/// 求x的n次方
/// </summary>
/// <param name="x"></param>
/// <param name="n"></param>
/// <returns></returns>
public int foo(int x, int n)
{
    int val = 1;
    if (n > 0)
    {
        if (n % 2 == 1)
        {
            val = val * x;
        }
        val = val * foo(x * x, n / 2);
    }
    return val;
}
/// <summary>
/// 求i的阶乘
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public int Factorial(int i)
{
    if (i == 1)
        return 1;
    else
        return i * Factorial(i - 1);
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值