zoukankan      html  css  js  c++  java
  • C#排序算法之选择排序

    排序规则:

      首先在未排序的序列中找到最小(大)元素,放到排序序列的起始位置。

      在从剩余未排序的元素中继续寻找最小(大)元素,然后放到排序序列的末尾

      重复第二步,直到所有元素均排序完毕

    时间复杂度:

    平均时间复杂度:O(n^2),最好情况:O(n^2) ,最坏情况:O(n^2)

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    namespace 排序算法
    {
        class SelectSort
        {
            //泛型排序
            public static void Sort<E>(E[] arr) where E : IComparable<E>
            {
                int n = arr.Length;
                
                for(int i = 0; i < n; i++)
                {
                    int min = i;
                    for (int j =i + 1; j < n; j++)
                    {
                        if (arr[j].CompareTo(arr[min]) < 0)
                        {
                            min = j;
                        }
                    }
                    Swap(arr, i, min);
                }
            }
            //整形排序
            public static void Sort( int[] arr)
            {
                int n = arr.Length;
                for(int i = 0; i < n; i++)
                {
                    int min = i;
                    for(int j =  i + 1; j < n; j++)
                    {
                        if(arr[j] < arr[min])
                        {
                            min = j;
                        }
                    }
                    Swap(arr, i, min);
                }
            }
            private static void Swap<E>(E[] arr, int i, int j)
            {
                E t = arr[i];
                arr[j] = arr[i];
                arr[i] = t;
            }
        }
    }
  • 相关阅读:
    迭代器
    LinkedList存储一副扑克牌,实现洗牌功能。
    线程
    堆栈、队列
    路由-第7集
    javascript中split字符串分割函数
    this的用法
    什么是AOP面向切面编程
    Servlet与JSP的区别
    堆(heap)、栈(stack)、方法区(method)
  • 原文地址:https://www.cnblogs.com/sy-liu/p/13262681.html
Copyright © 2011-2022 走看看