冒泡排序

冒泡排序算法的运作如下:

  1. 比较相邻的元素。如果第一个比第二个大,就交换他们两个。
  2. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。在这一点,最后的元素应该会是最大的数。
  3. 针对所有的元素重复以上的步骤,除了最后一个。
  4. 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。
protected void Page_Load(object sender, EventArgs e)
        {
            int[] score ={10,15,14,25,66,88,99};
            SortArray(score);

            for (int i = 0; i < score.Length; i++)
            {
                Response.Write(score[i] + "<br>");
            }
        }

        protected void SortArray(int[] array)
        {
            if (array.Length>0)
            {
                for (int i = 0; i < array.Length-1; i++)        //冒泡排序的次数  
                {
                    for (int j = 0; j < array.Length - 1 - i; j++)  //对2个值,进行比较  除了最后一个
                    {
                        
                        if (array[j]>array[j+1])
                        {
                            int temp;
                            temp = array[j];
                            array[j] = array[j + 1];
                            array[j + 1] = temp;
                        }
                    }
                }
            }
        }
原文地址:https://www.cnblogs.com/iceicebaby/p/2546323.html