冒泡排序法

  • using System;
    using System.Collections.Generic;
    using System.Text;
    namespace 程序算法
    {
        /// <summary>
        /// 就是将一组数中最大的挑出来,像气泡一样向上升
        /// 
        /// 过程:
        /// 首先将一组数组中的第1个数与第2个数进行比较,若为逆序,则将连个记录交换,然后第2个与第3个数,以此类推,直至第n-1个数与第n个数比较完,将
        /// </summary>
        publicclass 冒泡算法
        {
            publicvoid Sort(int[] arr)
            {
                int temp;           
                for (int i = 0; i < arr.Length; i++) //
                {
                    for (int j = 0; j < arr.Length - i-1; j++)
                    {
                        //if (arr[j] > arr[j + 1]) //和自己后面的一个元素进行比较
                        //{
                        //    temp = arr[j];
                        //    arr[j] = arr[j + 1];
                        //    arr[j + 1] = temp;
                        //}
                        /*倒序排列*/
                        if (arr[j] < arr[j + 1])
                        {
                            temp = arr[j + 1];
                            arr[j + 1] = arr[j];
                            arr[j] = temp;
                        }
                    }
                }
            }
            publicstaticvoid Main(string[] args)
            {
                冒泡算法 maoPao = new 冒泡算法();
                int[] array = newint[] { 1, 3, 2, 10, 13, 16, 42, 36, 77, 37, 99, 100 };
                maoPao.Sort(array);
                foreach (int k in array)
                {
                    Console.Write(k);
                    Console.Write(" ");              
                }
                Console.WriteLine();
            }       
        }
    }

你可能感兴趣的:(算法)