排序

来源:互联网 发布:威纶触摸屏数据导出 编辑:程序博客网 时间:2024/06/06 00:19
冒泡: 
public static void MaoPao()        {            int[] arrList = { 1, 2, 6, 8, 3, 4, 5,12,9,14,29,16 };            for (int i = 0; i < arrList.Length ; i++)            {                int temp = arrList[i];                for (int j = i + 1; j < arrList.Length; j++)                {                    if (temp > arrList[j])                    {                        arrList[i] = arrList[j];                        arrList[j] = temp;                        temp = arrList[i];                    }                                   }            }            foreach (var item in arrList)            {                Console.WriteLine("冒泡结果{0}", item);            }            int[] arr = { 1, 2, 6, 8, 3, 4, 5, 12, 9, 14, 29, 16 };            for (int i = 0; i <= arr.Length - 1; i++)            {                for (int j = arr.Length - 1; j > i; j--)                {                    if ((int)arr[j] < (int)arr[j - 1])                    {                        int temp = (int)arr[j];                        arr[j] = arr[j - 1];                        arr[j - 1] = temp;                    }                }            }            foreach (var item in arr)            {                Console.WriteLine("冒泡结果{0}", item);            }        }

原创粉丝点击