菜鸟学排序算法
2015-06-09 18:50
387 查看
插入排序:
希尔排序:
快速排序:
归并排序:
基数排序:
二叉树定义:
二叉排序树:
堆排序(求最大值):
写的很烂。。。
权当每天的日记吧
public List<int> InsertSort(List<int> arr) { List<int> ARRAY = new List<int>(); ARRAY.Add(arr[0]); int value; int count; for (int arrIndex = 1; arrIndex < arr.Count; arrIndex++) { value = arr[arrIndex]; count = ARRAY.Count; for (int ArrayIndex = 0; ArrayIndex < count; ArrayIndex++) { if (value <= ARRAY[ArrayIndex]) { ARRAY.Insert(ArrayIndex, value); break; } } if (count >= ARRAY.Count) ARRAY.Add(value); } return ARRAY; }
希尔排序:
private List<int> ShellSort(List<int> arr) { List<int> ARRAY = new List<int>(); int delta = arr.Count; ARRAY = arr; do { delta = (delta / 3) + 1; for (int i = 0; i < delta; i++) { List<int> list = new List<int>(); for (int j = 0; j < arr.Count / delta; j++) { list.Add(arr[j * delta + i]); } list = InsertSort(list); for (int j = 0; j < arr.Count / delta; j++) { arr[j * delta + i] = list[j]; } } } while (delta > 1); return ARRAY; }
快速排序:
private List<int> QuickSort(List<int> arr, int first, int last) { if (first >= last) return null; int First = first; int Last = last; int key = array[first]; while (first < last) { while (array[last] >= key && last > first) --last; array[first] = array[last]; while (array[first] <= key && last > first) ++first; array[last] = array[first]; } array[first] = key; QuickSort(arr, First, first - 1); QuickSort(arr, last + 1, Last); return arr; }
归并排序:
private List<int> MergeSort(List<int> arr) { List<int> ARRAY = new List<int>(); int delta = 1; int i, j; while (delta < arr.Count) { ARRAY.Clear(); while (arr.Count > 0) { i = 0; j = i + delta; while (i < delta && j < 2 * delta && j < arr.Count) { if (arr[i] < arr[j]) { ARRAY.Add(arr[i]); i++; } else { ARRAY.Add(arr[j]); j++; } } while (i < delta && i < arr.Count) { ARRAY.Add(arr[i]); i++; } while (j < 2 * delta && j < arr.Count) { ARRAY.Add(arr[j]); j++; } arr.RemoveRange(0, Math.Min(j, arr.Count)); } delta *= 2; arr = new List<int>(ARRAY); } return ARRAY; }
基数排序:
private List<int> RadixSort(List<int> arr) { List<int> ARRAY = new List<int>(); List<int>[] ARRAYS = new List<int>[10]; int count = 0;int index = 0; int Max = arr.Max().ToString().Length; while (count < Max) { ARRAY.Clear(); for (int i = 0; i < ARRAYS.Length; i++) ARRAYS[i] = new List<int>(); foreach (int Item in arr) { index = (Item / (int)Math.Pow(10, count)) % 10; { ARRAYS[index].Add(Item); } } for (int i = 0; i < 10; i++) { ARRAY.AddRange(ARRAYS[i]); } arr = new List<int>(ARRAY); count++; } return ARRAY; }
二叉树定义:
class Tree { public int value; public Tree left = null; public Tree right = null; public Tree(int key) { value = key; } }
二叉排序树:
public List<int> BinaryTreeSort(List<int> arr) { Tree tree = null; AddListToBinaryTree(ref tree, arr); arr.Cle aa24 ar(); arr = LoadListFromTree(tree, ref arr); return arr; } private void AddListToBinaryTree(ref Tree tree, List<int> list) { foreach (int key in list) { AddKeyToBinaryTree(ref tree, key); } } private void AddKeyToBinaryTree(ref Tree tree, int key) { if (tree == null) { tree = new Tree(key); } else if ( key < tree.value) { AddKeyToBinaryTree(ref tree.left, key); } else if (tree.value < key) { AddKeyToBinaryTree(ref tree.right, key); } } private List<int> LoadListFromTree(Tree tree, ref List<int> list) { if (tree == null) return null; LeftLoadListFromTree(tree.left, ref list); list.Add(tree.value); LeftLoadListFromTree(tree.right, ref list); return list; }
堆排序(求最大值):
private int HeapSort(List<int> arr) { Tree tree = null; AddListToHeapSort(ref tree, arr); return tree.value; } private void AddListToHeapSort(ref Tree tree, List<int> list) { foreach (int key in list) { AddKeyToHeapSort(ref tree, key); } } private void AddKeyToHeapSort(ref Tree tree, int key) { if (tree == null) { tree = new Tree(key); } else if (tree.value >= key) { if (tree.left == null) AddKeyToHeapSort(ref tree.left, key); else if (tree.right == null) AddKeyToHeapSort(ref tree.right, key); else { if (tree.left.value > tree.right.value) { AddKeyToHeapSort(ref tree.left, key); } else AddKeyToHeapSort(ref tree.right, key); } } else { AddKeyToHeapSort(ref tree, tree.value); tree.value = key; } }
写的很烂。。。
权当每天的日记吧
相关文章推荐
- c#调用COM组件
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程
- C#托管堆对象实例包含内容分析
- C#实现按照指定长度在数字前补0方法小结
- C#虚方法的声明和使用实例教程
- C#获取文件夹及文件的大小与占用空间的方法
- C#定义简单的反射工厂实例分析
- C#数字图象处理之肤色检测的方法
- C#实现用于操作wav声音文件的类实例
- C#选择排序法实例分析
- C#线程间不能调用剪切板的解决方法