排序算法
插入排序,由第二位开始,一个个往前适当的位置(比左边大,比右边小)插入
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication7 { class Class5 { //插入排序,由第二位开始,一个个往前适当的位置(比左边大,比右边小)插入 static void Main(string[] args) { int[] ary = { 32, 432, 5, 32, 43, 100, 32, 12 }; for (int i = 1; i < ary.Length; i++) { int j = i; while (j > 0 && ary[j - 1] > ary[j]) { Class5.chg(ref ary[j], ref ary[j - 1]); j--; } } } public static void chg(ref int a, ref int b) { int tmp = a; a = b; b = tmp; } } }
冒泡排序,从最尾开始,和前一个比较,将小的移前,最后得出最小的放在第一位.
重复一次,将次小的放在第二位...
最终得出结果
static void Main(string[] args) { int[] ary = { 37, 432, 5, 32, 43, 100, 46, 12 }; for (int i = 0; i < ary.Length; i++) { int j = ary.Length - 1; while (j > i) { if (ary[j - 1] > ary[j]) { int tmp = ary[j]; ary[j] = ary[j - 1]; ary[j - 1] = tmp; } j--; } } }
选择排序,由左边起第一个起,找出最小的和第一个交换位置,再从第二个起,找出最小的和第二个交换位置,重复可得结果.
static void Main(string[] args) { int[] ary = { 32, 5, 62, 43, 6, 21, 4, 7 }; for (int i = 0; i < ary.Length - 1; i++) { int j = i; int minpos = i; int first = ary[i]; while (j < ary.Length - 1) { if (ary[minpos] > ary[j + 1]) { minpos = j + 1; } j++; } ary[i] = ary[minpos]; ary[minpos] = first; } }
希尔排序
static void Main(string[] args) { int[] ary = { 37, 432, 5, 32, 43, 100, 46, 12, 88, 122, 12, 7, 32, 89, 43 }; //分的份数 int d = ary.Length / 2; while (d >= 1) { //拿各份的第一个集作基准 for (int i = 0; i < d; i++) { //对各份数字进行插入排序 for (int j = d + i; j < ary.Length; j = j + d) { int k = j; while (k >= d && ary[k - d] > ary[k]) { int tmp = ary[k - 1]; ary[k - 1] = ary[k]; ary[k] = tmp; k -= d; } } } d = d / 2; } }
归并排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Class9 { static void Main9(string[] args) { int[] ary = { 32, 43, 5, 2, 634, 75, 723, 43, 51, 34, 65, 86, 12, 36, 123, 643, 62, 23 }; Class9.order(ary); } //归并排序 public static void order(int[] ary) { int[] arynew = new int[ary.Length]; int num = 1; while (num < ary.Length) { //分成几份 int b = ary.Length / num; for (int i = 0; i < b; i = i + 2) { int p = num * i; //初位置 int j = num * i; int k = num * (i + 1); //末位置后一位 int pa = j + num; int pb = Math.Min(k + num, ary.Length); while (j <= pa && k <= pb && !(j == pa && k == pb)) { if (j == pa) //到末位置后一位了 { arynew[p] = ary[k]; k++; } else if (k == pb) //到末位置后一位了 { arynew[p] = ary[j]; j++; } else if (ary[j] > ary[k]) { arynew[p] = ary[k]; k++; } else { arynew[p] = ary[j]; j++; } p++; } } arynew.CopyTo(ary, 0); num = num * 2; } } } }
快速排序
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication2 { class Class10 { static void Main(string[] args) { int[] ary = { 32, 43, 5, 2, 634, 75, 723, 43, 51, 34, 65, 86, 12,643,24,7,21,74,243,211,76 }; Class10.order(ary); } //快速排序 public static void order(int[] ary) { //0维数量则不排 if (ary.Length == 0) { return; } int p = 0; //0时key在左边,1在右边 int j = 0; int k = ary.Length - 1; while (j < k ) { if (ary[j] > ary[k]) { int tmp = ary[j]; ary[j] = ary[k]; ary[k] = tmp; p = (p + 1) % 2; } if (p == 0) { k--; } else { j++; } } int[] aa = new int[k]; int[] ab = new int[ary.Length - 1 - k]; Array.Copy(ary, 0, aa, 0, aa.Length); Array.Copy(ary, k + 1, ab, 0, ab.Length); Class10.order(aa); Class10.order(ab); Array.Copy(aa, 0, ary, 0, aa.Length); Array.Copy(ab, 0, ary, k + 1, ab.Length); } } }
堆排序
//堆排序 static void Main(string[] args) { int[] ary = { 36, 30, 18, 40, 322, 32, 45, 22, 50, 12, 89 }; int lastpos = ary.Length - 1; int flag = 0; //一敞比较,把最大放在最后,最后前面形成新的树. while (lastpos > 0) { int len = (lastpos + 1) / 2; //由最后的支到最前的支,完成一敞后可以直接从最前的支开始 while (len > 0) { int lent = 0; if (flag == 0) { lent = len; } else { lent = 1; } //比较大小,直接根节点 while (1 == 1) { int max = lent - 1; //有右叶 if (2 * lent <= lastpos) { if (ary[lent - 1] < ary[2 * lent]) { max = 2 * lent; } } //有左叶 if (2 * lent - 1 <= lastpos) { if (ary[max] < ary[2 * lent - 1]) { max = 2 * lent - 1; } } if (max != lent - 1) { int tmp = ary[lent - 1]; ary[lent - 1] = ary[max]; ary[max] = tmp; lent = max + 1; } else { break; } } len--; } int head = ary[0]; ary[0] = ary[lastpos]; ary[lastpos] = head; lastpos--; flag = 1; } }