自娱自乐之选择排序

   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:   
  21:          /// <summary>
  22:          /// 选择排序的原理就是依次循环数据,然后再通过一个循环找出当前最小的数或者最大的数,然后赋值给第一次循环的索引
  23:          /// </summary>
  24:          /// <param name="list"></param>
  25:          /// <returns></returns>
  26:          static List<int> Sort(List<int> list)
  27:          {
  28:              int temp = 0;
  29:              int baseNum = 0;
  30:              for (int j = 0; j < list.Count - 1; j++)
  31:              {
  32:                  temp = j;
  33:                  for (int i = j + 1; i < list.Count; i++)
  34:                  {
  35:                      if (list[temp] < list[i])
  36:                          temp = i;
  37:                  }
  38:                  baseNum = list[temp];
  39:                  list[temp] = list[j];
  40:                  list[j] = baseNum;
  41:              }
  42:              return list;
  43:          }
  44:      }
  45:  }
posted @ 2013-12-26 17:07  Djzny  阅读(178)  评论(0编辑  收藏  举报