zoukankan      html  css  js  c++  java
  • C#简单选择排序

           简单选择排序算法:

          设所排序序列的记录个数为n。i取1,2,…,n-1,从所有n-i+1个记录(R,R[i+1],…,R[n]中找出排序码最小的记录,与第i个记录交换。执行n-1趟 后就完成了记录序列的排序。

          代码如下:

    简单选择排序代码
     1 using System.Text;
     2 protected void Page_Load(object sender, EventArgs e)
     3     {
     4        SimpleSelectSort();
     5     }
     6 
     7     private void SimpleSelectSort()
     8     {
     9         int[] inputIntArray = new int[8] { 84752361 };
    10         for (int i = 1; i < inputIntArray.Length;i++)
    11         {//遍历7次 (N-1)
    12             int t = i - 1;//设置数组下标
    13             for (int j = i;j<inputIntArray.Length;j++)
    14             {//从第i个开始进行循环  起始为数组第2个
    15                 if(inputIntArray[t]>inputIntArray[j])
    16                 {//获取最小值的INDEX
    17                     t = j;
    18                 }
    19             }
    20             //进行置换
    21             int temp=inputIntArray[t];
    22             inputIntArray[t]=inputIntArray[i-1];
    23             inputIntArray[i - 1= temp;
    24             //打印置换后的排序结果
    25             PrintSortedResult(inputIntArray ,i);
    26         }
    27     }
    28 
    29  private void PrintSortedResult(int[] inputIntArray,int num)
    30     {
    31         StringBuilder sb = new StringBuilder();
    32         for (int i = 0; i < inputIntArray.Length; i++)
    33         {
    34             if (i == 0)
    35             {
    36                 sb.Append(inputIntArray[i].ToString());
    37             }
    38             else
    39             {
    40                 sb.Append("," + inputIntArray[i].ToString());
    41             }
    42         }
    43         Response.Write(""+num+"次排序的结果:  "+sb.ToString()+"<br/>");
    44     }

    结果显示如下图:

  • 相关阅读:
    [Unity3D]蓝港面试题
    BZOJ 2186 SDOI2008 沙拉公主的困惑 数论
    JSONObject与JSONArray的使用
    一个int类型究竟占多少个字节
    软件开发的金字塔
    poj 1064 Cable master ,二分 精度!!!
    php实现工厂模式
    数据库索引的作用和长处缺点
    C++中使用class和structkeyword的不同
    提交时提示错误This Bundle is invalid.New apps and app updates submitted to the App Store must be built wit
  • 原文地址:https://www.cnblogs.com/jasenkin/p/1667477.html
Copyright © 2011-2022 走看看