zoukankan      html  css  js  c++  java
  • 几种排序方法的总结

    冒泡排序

     1 static void Main(string[] args)
     2         {
     3 
     4             int[] a = new int[10];
     5 
     6             for (int i = 0; i < a.Length; i++)
     7             {
     8                 a[i] = Convert.ToInt32(Console.ReadLine());
     9             }
    10 
    11             for (int m = 0; m < a.Length; m++)
    12             {
    13                 for (int n = 0; n < a.Length - 1; n++)
    14                 {
    15                     if (a[n] > a[n + 1])       //从小到大用大于号,反之小于号
    16                     {
    17                         int temp;
    18                         temp = a[n]; 
    19                         a[n] = a[n + 1]; 
    20                         a[n + 1] = temp;
    21                     }
    22                 }
    23             }
    24 
    25             Console.WriteLine("===================");
    26             for (int l = 0; l < a.Length; l++)
    27             {
    28                 Console.WriteLine(a[l]);
    29                 
    30             }
    31 
    32             Console.ReadKey();
    33 
    34 
    35         }
    冒泡排序

    选择排序

     1 static void Main(string[] args)
     2         {
     3 
     4             int[] a = new int[10];
     5 
     6             for (int i = 0; i < a.Length; i++)
     7             {
     8                 a[i] = Convert.ToInt32(Console.ReadLine());
     9             }
    10 
    11 
    12             int min_index;
    13 
    14 
    15             for (int n = 0; n < a.Length - 1; n++)
    16             {
    17                 min_index = n;           //设置第一个数为最小值
    18 
    19                 //从第二个数到最后一个数进行循环
    20                 for (int j = n + 1; j < a.Length; j++)     
    21                 {   
    22                    //把从第二个数到最后的每个数和第一个数比较,如果比他小,就把索引赋值
    23                     if (a[j] < a[min_index])          
    24                     {
    25                         min_index = j;
    26                     }
    27 
    28                    //找到最小项交换,即将这一项移到列表中的正确位置
    29                     if (min_index != n)     
    30 
    31                     {
    32                         int x;
    33                         x = a[n];
    34                         a[n] = a[min_index];
    35                         a[min_index] = x;
    36                     } 
    37                 }
    38 
    39             }
    40             
    41 
    42             Console.WriteLine("===================");
    43             for (int l = 0; l < a.Length; l++)
    44             {
    45                 Console.WriteLine(a[l]);
    46                 
    47             }
    48 
    49             Console.ReadKey();
    50 
    51 
    52         }
    选择排序

    插入排序

     1 static void Main(string[] args)
     2         {
     3 
     4             int[] a = new int[10];
     5 
     6             for (int i = 0; i < a.Length; i++)
     7             {
     8                 a[i] = Convert.ToInt32(Console.ReadLine());
     9             }
    10 
    11             //循环从第二个数组元素开始,因为a[0]作为最初已排序部分
    12             for (int j = 1; j < a.Length; j++)
    13             {
    14                 //temp标记为未排序第一个元素
    15                 int temp = a[j];         
    16                 int x = j - 1;
    17 
    18                 /*将temp与已排序元素从小到大比较,寻找temp应插入的位置*/
    19                 while (x >= 0 && a[x] > temp)  
    20                 {
    21                     a[x + 1] = a[x];
    22                     x--;
    23                 }
    24 
    25                 a[x + 1] = temp;
    26 
    27             }
    28 
    29 
    30             Console.WriteLine("===================");
    31             for (int l = 0; l < a.Length; l++)
    32             {
    33                 Console.WriteLine(a[l]);
    34                 
    35             }
    36 
    37             Console.ReadKey();
    38 
    39 
    40         }
    插入排序
     
  • 相关阅读:
    vue使用腾讯地图选点组件问题总结
    腾讯位置服务实现点击建筑显示围栏及建筑信息效果
    unity使用UMP播放RTSP流,打包exe后显示空白
    uniapp获取context
    Android studio安装debug apk提示“调用者不被允许测试的测试程序”
    unity使用VuplexWebView内嵌浏览器遮挡前方按钮的问题
    unity透明材质上放3dtext不同角度,文字变灰的问题
    Python线程指南
    mysql 简单表和索引
    dubbo 提示 403 unknown user
  • 原文地址:https://www.cnblogs.com/Johnfx-home/p/12385963.html
Copyright © 2011-2022 走看看