zoukankan      html  css  js  c++  java
  • C#冒泡排序,最完美,最简洁的两种方式

     1  var array = new int[] { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
     2             int temp;
     3             //排序方法1
     4             for (int i = 0; i < array.Length - 1; i++)
     5             {
     6                 for (int j = 0; j < array.Length - i - 1; j++)
     7                 {
     8                     if (array[j] > array[j + 1])
     9                     {
    10                         temp = array[j];
    11                         array[j] = array[j + 1];
    12                         array[j + 1] = temp;
    13                     }
    14                 }
    15             }
    16             foreach (var item in array)
    17             {
    18                 Console.WriteLine(item);
    19             }
    20             Console.ReadKey();
    21             //排序2:
    22             array = new int[] { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
    23             for (int i = 0; i < array.Length - 1; i++)
    24             {
    25                 for (int j = i + 1; j < array.Length; j++)
    26                 {
    27                     if (array[j] < array[i])
    28                     {
    29                         temp = array[i];
    30                         array[i] = array[j];
    31                         array[j] = temp;
    32                     }
    33                 }
    34             }
    35             foreach (var item in array)
    36             {
    37                 Console.WriteLine(item);
    38             }
    39             Console.ReadKey();
  • 相关阅读:
    redis修改密码
    redis配置
    django中日志配置
    django中缓存配置
    navicat批量导入数据
    django添加REST_FRAMEWORK 接口浏览
    django验证码配置与使用
    LUA_OBJECT
    LUA comment
    lua-redis
  • 原文地址:https://www.cnblogs.com/momj/p/15697849.html
Copyright © 2011-2022 走看看