I’m Coding !(一)

版权声明:学而不思则罔,思而不学则殆 https://blog.csdn.net/qq_34481670/article/details/87695914

1、冒泡排序:

需要用两个for循环才能遍历所有的数组元素。2个for循环的用处,就是第一个for循环的每一个分别去比较后一个for循环的所有元素。

 public void addsort(int[] array)
        {
            int length = array.Length;
            for (int y = 0; y < length - 1; y++)
            {

            
                for (int i = 0; i < length - 1; i++)
                {
                    if (array[i] > array[i + 1])
                    {

                        int temp = array[i];
                        array[i] = array[i + 1];
                        array[i + 1] = temp;
                    }}}}
              
        static void Main(string[] args)
        {
            int[] array = { 1, 4, 2, 5, 3 };
            abc ssh = new abc();
            ssh.addsort(array);
            foreach (int i in array)
            {
                Console.WriteLine("{0}", i);
            }
            Console.WriteLine();

        }
    }
}

2、 简单的泛型方法:

class bcd
    {
        public static int Skyy<T>(T[] item, T items)    //创建泛型方法
        {
            for (int i = 0; i < item.Length;i++ )
            {
                if (item[i].Equals(items))
                {
                    return i;
                }
            }
            return -1;
        }
    }

------ 

class Class1
    {

 static void Main(String[] args)
        {
            int i = bcd.Skyy<int>(new int[] { 1, 2, 3, 4, 5 }, 3);
            Console.WriteLine("3在数组中的位置" + i.ToString());
        }

}

猜你喜欢

转载自blog.csdn.net/qq_34481670/article/details/87695914