zoukankan      html  css  js  c++  java
  • 自娱自乐之选择排序

       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:  }
  • 相关阅读:
    tinyxml优化之一
    vs下C++内存泄露检测
    Cocos2d-x项目移植到WP8系列之九:使用自定义shader
    [leetcode 双周赛 11] 1228 等差数列中缺失的数字
    [leetcode 周赛 158] 1224 最大相等频率
    [leetcode 周赛 158] 1223 掷骰子模拟
    [leetcode 周赛 158] 1222 可以攻击国王的皇后
    [leetcode 周赛 158] 1221 分割平衡字符串
    [leetcode 周赛 157] 1220 统计元音字母序列的数目
    [leetcode 周赛 157] 1219 黄金矿工
  • 原文地址:https://www.cnblogs.com/djzny/p/3492763.html
Copyright © 2011-2022 走看看