原文发布时间为:2008-11-26 —— 来源于本人的百度文章 [由搬家工具导入]
C#算法(一)选择排序
using System;public class SelectionSorter{ // public enum comp {COMP_LESS,COMP_EQUAL,COMP_GRTR}; private int min; // private int m=0; public void Sort(int[] list) { for (int i = 0; i < list.Length - 1; ++i) { min = i; for (int j = i + 1; j < list.Length; ++j) { if (list[j] < list[min]) min = j; } int t = list[min]; list[min] = list[i]; list[i] = t; // Console.WriteLine("{0}",list[i]); }}
}public class MainClass{ public static void Main() { int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 }; SelectionSorter ss = new SelectionSorter(); ss.Sort(iArrary); for (int m = 0; m <= 13; m++) Console.WriteLine("{0}", iArrary[m]);}
}C#算法(二)插入排序
using System;public class InsertionSorter{ public void Sort(int[] list) { for (int i = 1; i < list.Length; ++i) { int t = list[i]; int j = i; while ((j > 0) && (list[j - 1] > t)) { list[j] = list[j - 1]; --j; } list[j] = t; }}
}public class MainClass{ public static void Main() { int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 }; InsertionSorter ii = new InsertionSorter(); ii.Sort(iArrary); for (int m = 0; m <= 13; m++) Console.WriteLine("{0}", iArrary[m]); }}C#算法(三)希尔排序
public class ShellSorter{ public void Sort(int[] list) { int inc; for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ; for (; inc > 0; inc /= 3) { for (int i = inc + 1; i <= list.Length; i += inc) { int t = list[i - 1]; int j = i; while ((j > inc) && (list[j - inc - 1] > t)) { list[j - 1] = list[j - inc - 1]; j -= inc; } list[j - 1] = t; } } }}public class MainClass{ public static void Main() { int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 }; ShellSorter sh = new ShellSorter(); sh.Sort(iArrary); for (int m = 0; m <= 13; m++) Console.WriteLine("{0}", iArrary[m]); }}C#算法(四)快速排序using System;namespace QuickSorter
{ public class QuickSorter { private void Swap(ref int l, ref int r) { int s; s = l; l = r; r = s; } public void Sort(int[] list, int low, int high) { int pivot; int l, r; int mid; if (high <= low) return; else if (high == low + 1) { if (list[low] > list[high]) Swap(ref list[low], ref list[high]); return; } mid = (low + high) >> 1; pivot = list[mid]; Swap(ref list[low], ref list[mid]); l = low + 1; r = high; do { while (l <= r && list[l] < pivot) l++; while (list[r] >= pivot) r--; if (l < r) Swap(ref list[l], ref list[r]); } while (l < r); list[low] = list[r]; list[r] = pivot; if (low + 1 < r) Sort(list, low, r - 1); if (r + 1 < high) Sort(list, r + 1, high); } } public class MainClass { public static void Main() { int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 }; QuickSorter q = new QuickSorter(); q.Sort(iArrary, 0, 13); for (int m = 0; m <= 13; m++) Console.WriteLine("{0}", iArrary[m]); } }}