《数据结构C实现.doc》由会员分享,可在线阅读,更多相关《数据结构C实现.doc(15页珍藏版)》请在金锄头文库上搜索。
1、数据结构C实现/选择排序class SelectionSorterprivate int min;public void Sort(int arr)for (int i = 0; i arr.Length - 1; +i)min = i;for (int j = i + 1; j arr.Length; +j)if (arrj arrmin)min = j;int t = arrmin;arrmin = arri;arri = t;static void Main(string args)int array = new int 1, 5, 3, 6, 10, 55, 9, 2, 87, 12,
2、 34, 75, 33, 47 ;SelectionSorter s = new SelectionSorter();s。Sort(array);foreach (int m in array)Console.WriteLine(”0, m);/冒泡排序class EbullitionSorterpublic void Sort(int arr)int i, j, temp;bool done = false;j = 1;while (j arri + 1)done = false;temp = arri;arri = arri + 1;/交换数据arri + 1 = temp;j+;stat
3、ic void Main(string args)int array = new int 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 ;EbullitionSorter e = new EbullitionSorter ();e。Sort(array);foreach (int m in array)Console。WriteLine(”0”, m);/快速排序class QuickSorterprivate void swap(ref int l, ref int r)int temp;temp = l;l = r;r = temp;pu
4、blic 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 (listlow listhigh)swap(ref listlow, ref listhigh);return;mid = (low + high) 1;pivot = listmid;swap(ref listlow, ref listmid);l = low + 1;r = high;dowhile (l = r & listl pivot)
5、l+;while (listr = pivot)r;if (l r)swap(ref listl, ref listr); while (l r);listlow = listr;listr = pivot;if (low + 1 r)Sort(list, low, r 1);if (r + 1 high)Sort(list, r + 1, high);static void Main(string args)int iArrary = new int 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 ;QuickSorter q = new Q
6、uickSorter();q.Sort(iArrary, 0, 13);for (int m = 0; m = 13; m+)Console。WriteLine(”0, iArrarym);/插入排序public class InsertionSorterpublic void Sort(int arr)for (int i = 1; i t))arrj = arrj 1;/交换顺序-j;arrj = t;static void Main(string args)int array = new int 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33,
7、47 ;InsertionSorter i = new InsertionSorter();i。Sort(array);foreach (int m in array)Console.WriteLine(”0”, m);/希尔排序public class ShellSorterpublic void Sort(int arr)int inc;for (inc = 1; inc = arr。Length / 9; inc = 3 inc + 1) ;for (; inc 0; inc /= 3)for (int i = inc + 1; i = arr。Length; i += inc)int
8、t = arri - 1;int j = i;while (j inc) & (arrj inc 1 t))arrj 1 = arrj inc - 1;/交换数据j = inc;arrj - 1 = t;static void Main(string args)int array = new int 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 ;ShellSorter s = new ShellSorter();s.Sort(array);foreach (int m in array)Console。WriteLine(”0, m);先我
9、们给树下一个定义:树是一个有限的、非空的结点集 首先我们给树下一个定义: 树是一个有限的、非空的结点集,T=r or T1 or T2 oror Tn它具有下列性质:1集合指定的结点r叫做树的根结点2其余的结点可以划分成n个子集,T1,T2,Tn(n=0),其中每一个子集都是一棵树。树的其它定义如度,叶子,高等就请大家查阅别的资料吧,到处都有的。树的主要性质一个就是遍历,分为深度遍历和广度遍历在这里分别实现为DepthFirstTravesal()和WidthFirstTravesal()其中深度遍历又分为前序遍历、中序遍历、和后序遍历这是是采用适配器技术实现的.using System;us
10、ing System。Collections;namespace DataStructure / summary / Tree 的摘要说明。 / when traverse, traversaltype cant be changed,or throw a exception / 支持枚举、比较、深度复制 / /summary public abstract class Tree:IEnumerable,IComparable public Tree() / / TODO: 在此处添加构造函数逻辑 / protected Queue keyqueue=new Queue();/仅仅用于枚举时存放数据,不参与Equals实现中的比较 protected TraversalType traversaltype=TraversalType.Breadth;/ choose a traver