C#插入排序算法

SortUtil类:

using System;

namespace insertsort.Sort {
    public class SortUtil<T> where T:struct,IComparable<T> {
        public static void printArray(T[] arr) {
            Console.WriteLine("数组:");
            for(int i=0;i<arr.Length;i++){
                Console.Write(arr[i]+" ");
            }
            Console.WriteLine();
        }

         public static void insertSort(T[] table) {
            /**
                6 5 1 4 2 3 7 8
             */

            int n = table.Length;
            int i,j;
            T temp;
            for(i=1;i<n;i++) {
                temp = table[i];
                for(j=i-1;j>=0 && temp.CompareTo(table[j])<0;j--) {
                    table[j+1] = table[j];
                }

                table[j+1] = temp;
                printArray(table);
            }
        }
    }
}

主程序类:

using System;
using insertsort.Sort;

namespace insertsort
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = {10,1,2,3,5,4,6,8,7,9};
            SortUtil<int>.insertSort(arr);    
        }
    }
}

程序输出:


1.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,391评论 19 139
  • 1. Java基础部分 基础部分的顺序:基本语法,类相关的语法,内部类的语法,继承相关的语法,异常的语法,线程的语...
    子非鱼_t_阅读 31,930评论 18 399
  • 前言 《四柱特训班讲义》一书,是笔者根据2003年春举办的四柱特训班讲课记录的基础上整理出来的。它是以《四柱详真》...
    小狐狸娃娃阅读 14,330评论 1 29
  • 地震, 像一道闪电从天而降, 不过它不是来自天空, 而是来自地狱, 夺走了美丽的家园的同时, 带走了我们的至亲, ...
    卖萌的攻城狮阿西阅读 1,443评论 0 1
  • 知财道学堂(微信号:ZCDxuetang)每日市场行情一览,通过以宏观视角把握市场行情,遵循信息透明的原则,做到充...
    小Z说道阅读 2,695评论 0 1