第七次学习

57 阅读1分钟

using System; using System.Transactions;

namespace ConsoleApp1 {

internal class Program
{
    public static void Main(string[] args)
    {
        /*
         
        int[] array = { 1, 23, 45, 78, 90, 2, 4 };
        int temp = 0;
        */

        //冒泡排序
        /*
        for (int i = 0; i < array.Length - 1; i++)
        {

            for (int j = 0; j < array.Length - 1 - i; j++)
            {

                if (array[j] > array[j + 1])
                {
                    temp = array[j + 1];
                    array[j + 1] = array[j];
                    array[j] = temp;
                }

            }
        }
        */
        /*
         //选择排序
         for (int i = 0; i < array.Length - 1 ; i++)
         {
            int  k = i;

             for (int j = i + 1; j < array.Length; j++)
             {

                 if (array[k] > array[j])
                 {
                     temp = array[j];
                     array[j] = array[k];
                     array[k] = temp;
                 }

             }
         }
         */

        /*
        Array.Sort(array); 
        Array.Reverse(array);
        foreach (int i in array)
        {
            Console.WriteLine(i);
        }
        */
        //有n(n <=100)个整数,已经按照从小到大的顺序排列好,现在另外给一个整数x,请将该数插入到序列中,并使新的序列仍然井然有序
        //投机倒把的方法
        int n = Convert.ToInt32(Console.ReadLine());
        int[] array1 = { 1, 3, 5, 7};
        int[] array2 = new int[array1.Length + 1];
        for (int i = 0; i < array1.Length; i++) {      
            array2[i] = array1[i];           
        }
        //Array.Copy(array1, array2,array1.Length);
        array2[array1.Length] = n;
        Array.Sort(array2);

        Console.WriteLine(String.Join(" ", array2));
        //真正的方式-》判断记录需要插入的值的下标,+1 插入该值的后方,使用线性表的插入移位思想插入
    }
}

}