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

    class Program
        {
            static void Main(string[] args)
            {
                int[] arr = new int[] { 6, 9, 13, 2, 4, 64 };
                int[] newArr = SelectionSort(arr);
                Console.Read();
            }
            public static int[] SelectionSort(int[] a)
            {
                int item; //中间临时变量,用于交换元素位置
                for (int i = 0; i < a.Length; i++)
                {
                    int minIndex = i; //记录最小元素的下标
                    for (int j = i+1; j < a.Length; j++)
                    {
                        if (a[minIndex]>a[j])
                        {
                            minIndex = j;
                        }
                    }
                    if (a[minIndex]!=a[i])
                    {
                        item = a[i];
                        a[i] = a[minIndex];
                        a[minIndex] = item;
                    }
                    StringBuilder sb = new StringBuilder();
                    sb.Append($"第{i+1}次排序后:");
                    foreach (var num in a)
                    {
                        sb.Append($"{num},");
                    }
                    Console.WriteLine(sb.ToString());
                }
                return a;
            }
        }

     备注:选择排序的复杂度,用大O表示法为:O(n*n)

     效率上:   快速排序 > 选择排序 > 冒泡排序

  • 相关阅读:
    Python2.7-math, cmath
    Python2.7-pprint
    Python2.7-copy
    Python2.7-weakref
    Python2.7-Queue
    Python2.7-sched
    Python2.7-array
    Python2.7-bisect
    搜索专题:Balloons
    【洛谷P4460】解锁屏幕【状压dp】
  • 原文地址:https://www.cnblogs.com/25miao/p/10662471.html
Copyright © 2011-2022 走看看