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);
}
}
}
程序输出: