自娱自乐之冒泡排序

   1:  using System;
   2:  using System.Collections.Generic;
   3:  using System.Linq;
   4:  using System.Text;
   5:   
   6:  namespace ConsoleApplication6
   7:  {
   8:      class Program
   9:      {
  10:          static void Main(string[] args)
  11:          {
  12:              List<int> list = new List<int>() { 565, 5, 5, 4156, 15, 6, 84, 641, 5, 4, 98 };
  13:              list = Sort(list);
  14:              foreach (int i in list)
  15:              {
  16:                  Console.Write(i + " ");
  17:              }
  18:              Console.ReadLine();
  19:          }
  20:          /// <summary>
  21:          /// 冒泡排序相当于i是确定大小的那个数,然后j是从最大开始循环,依次选一个最大或者最小的数放到索引为i的位置去
  22:          /// </summary>
  23:          /// <param name="list"></param>
  24:          /// <returns></returns>
  25:          static List<int> Sort(List<int> list)
  26:          {
  27:              int temp;
  28:              for (int i = 0; i < list.Count - 1; i++)//
  29:              {
  30:                  for (int j = list.Count - 1; j > i; j--)
  31:                  {
  32:                      if (list[j - 1] < list[j])
  33:                      {
  34:                          temp = list[j - 1];
  35:                          list[j - 1] = list[j];
  36:                          list[j] = temp;
  37:                      }
  38:                  }
  39:              }
  40:              return list;
  41:          }
  42:      }
  43:  }
原文地址:https://www.cnblogs.com/djzny/p/3492540.html