直接插入排序 冒泡排序
简单选择排序
线性表:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 线性表 { interface IListDS<T> { /// <summary> /// 获取线性表长度 /// </summary> /// <returns></returns> int GetLength(); /// <summary> /// 清空线性表 /// </summary> void Clear(); /// <summary> /// 判断线性表是否为空 /// </summary> /// <returns></returns> bool IsEmpty(); /// <summary> /// 添加元素 /// </summary> /// <param name="t">元素</param> void Add(T t); /// <summary> /// 插入元素 /// </summary> /// <param name="t">元素</param> /// <param name="index">要添加的位置</param> void Insert(T t,int index); /// <summary> /// 根据索引删除元素 /// </summary> /// <param name="index">索引</param> /// <returns></returns> T Delete(int index); /// <summary> /// 索引器 /// </summary> /// <param name="index"></param> /// <returns></returns> T this[int index] { get; } /// <summary> /// 通过索引获取元素 /// </summary> /// <param name="index">索引</param> /// <returns></returns> T GetElement(int index); /// <summary> /// 根据值获得索引 /// </summary> /// <param name="t">元素</param> /// <returns></returns> int Locate(T t); } }
IListDS
/* 脚本名称: 脚本作者: 建立时间: 脚本功能: 版本号: */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 线性表 { /// <summary> /// 顺序表实现 /// </summary> /// <typeparam name="T"></typeparam> class SequenceList<T>:IListDS<T> { /// <summary> /// 用来存储数据的数组 /// </summary> private T[] data; /// <summary> /// 存储数据的数量 /// </summary> private int count; /// <summary> /// 默认构造函数 /// </summary> ) { } /// <summary> /// 构造函数 /// </summary> /// <param name="size">最大容量</param> public SequenceList(int size) { data = new T[size]; } /// <summary> /// 索引器 /// </summary> /// <param name="index"></param> /// <returns></returns> public T this[int index] { get { return GetElement(index); } } /// <summary> /// 添加数据 /// </summary> /// <param name="t"></param> public void Add(T t) { //当前数组已经满了 if(count == data.Length) { Console.WriteLine("顺序表已满"); } else { data[count] = t; count++; } } /// <summary> /// 清空 /// </summary> public void Clear() { count = ; } /// <summary> /// 删除 /// </summary> /// <param name="index"></param> /// <returns></returns> public T Delete(int index) { T temp = data[index]; ;i < count;i++) { data[i - ] = data[i]; } count--; return temp; } /// <summary> /// 根据索引取得数据 /// </summary> /// <param name="index"></param> /// <returns></returns> public T GetElement(int index) { && index <= count - ) { return data[index]; } else { Console.WriteLine("索引不存在"); } return default(T); } /// <summary> /// 获取数据的个数 /// </summary> /// <returns></returns> public int GetLength() { return count; } /// <summary> /// 插入数据 /// </summary> /// <param name="t"></param> /// <param name="index"></param> public void Insert(T t,int index) { ;i >= index;index--) { data[i + ] = data[i]; } data[index] = t; count++; } /// <summary> /// 线性表是否为空 /// </summary> /// <returns></returns> public bool IsEmpty() { ; } /// <summary> /// 获得元素所在的位置 /// </summary> /// <param name="t"></param> /// <returns></returns> public int Locate(T t) { ;i < count;i++) { if(data[i].Equals(t)) { return i; } } ; } } }
SequenceList
/* 脚本名称: 脚本作者: 建立时间: 脚本功能: 版本号: */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 线性表 { class Node<T> { /// <summary> /// 存储数据 /// </summary> private T data; /// <summary> /// 指向下一个数据 /// </summary> private Node<T> next; /// <summary> /// /// </summary> public T Data { get { return data; } set { data = value; } } /// <summary> /// /// </summary> public Node<T> Next { get { return next; } set { next = value; } } /// <summary> /// /// </summary> public Node() { data = default(T); next = null; } /// <summary> /// /// </summary> public Node(T t) { data = t; next = null; } /// <summary> /// /// </summary> /// <param name="t"></param> /// <param name="next"></param> public Node(T t,Node<T> next) { data = t; this.next = next; } /// <summary> /// /// </summary> public Node(Node<T> next) { this.next = next; } } }
Node
/* 脚本名称: 脚本作者: 建立时间: 脚本功能: 版本号: */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 线性表 { class LinkList<T>:IListDS<T> { /// <summary> /// 头节点 /// </summary> private Node<T> head; public LinkList() { head = null; } /// <summary> /// 索引器 /// </summary> /// <param name="index"></param> /// <returns></returns> public T this[int index] { get { Node<T> temp = head; ;i <= index;i++) { temp = temp.Next; } return temp.Data; } } /// <summary> /// 添加 /// </summary> /// <param name="t"></param> public void Add(T t) { Node<T> newNode = new Node<T>(t); if(head == null) { head = newNode; } else { Node<T> temp = head; while(true) { if(temp.Next != null) { temp = temp.Next; } else { break; } } temp.Next = newNode; } } /// <summary> /// 清空 /// </summary> public void Clear() { head = null; } /// <summary> /// 删除 /// </summary> /// <param name="index"></param> /// <returns></returns> public T Delete(int index) { T data = default(T); ) { data = head.Data; head = head.Next; } else { Node<T> temp = head; ;i <= index - ;i++) { temp = temp.Next; } Node<T> preNode = temp; Node<T> currentNode = temp.Next; data = currentNode.Data; Node<T> nextNode = temp.Next.Next; preNode.Next = nextNode; } return data; } /// <summary> /// 根据位置获得元素 /// </summary> /// <param name="index"></param> /// <returns></returns> public T GetElement(int index) { return this[index]; } /// <summary> /// 获取长度 /// </summary> /// <returns></returns> public int GetLength() { if(head == null) { ; } Node<T> temp = head; ; while(true) { if(temp.Next != null) { count++; temp = temp.Next; } else { break; } } return count; } /// <summary> /// 插入 /// </summary> /// <param name="t"></param> /// <param name="index"></param> public void Insert(T t,int index) { Node<T> newNode = new Node<T>(t); ) { newNode.Next = head; head = newNode; } else { Node<T> temp = head; ;i <= index - ;i++) { temp = temp.Next; } Node<T> preNode = temp; Node<T> currentNode = temp.Next; preNode.Next = newNode; newNode.Next = currentNode; } } /// <summary> /// 是否为空 /// </summary> /// <returns></returns> public bool IsEmpty() { return head == null; } /// <summary> /// 根据元素获得位置 /// </summary> /// <param name="t"></param> /// <returns></returns> public int Locate(T t) { Node<T> temp = head; if(temp == null) { ; } else { ; while(true) { if(temp.Data.Equals(t)) { return index; } else { if(temp.Next != null) { temp = temp.Next; } else { break; } } } ; } } } }
LinkList
栈:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 栈 { interface IStackDS<T> { /// <summary> /// 获取长度 /// </summary> int Count { get; } /// <summary> /// 获取长度 /// </summary> /// <returns></returns> int GetLength(); /// <summary> /// 是否为空 /// </summary> /// <returns></returns> bool IsEmpty(); /// <summary> /// 清空 /// </summary> void Clear(); /// <summary> /// 入栈 /// </summary> /// <param name="t"></param> void Push(T t); /// <summary> /// 出栈 /// </summary> /// <returns></returns> T Pop(); /// <summary> /// 取栈顶 /// </summary> /// <returns></returns> T Peek(); } }
IStackDS
/* 脚本名称: 脚本作者: 建立时间: 脚本功能: 版本号: */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 栈 { /// <summary> /// 顺序栈 /// </summary> class SequenceStack<T>:IStackDS<T> { private T[] data; /// <summary> /// 栈顶索引 /// </summary> private int top; ) { } public SequenceStack(int size) { data = new T[size]; top = -; } /// <summary> /// 获取数量 /// </summary> public int Count { get { ; } } public void Clear() { top = -; } public int GetLength() { return Count; } public bool IsEmpty() { ; } public T Peek() { return data[top]; } public T Pop() { T temp = data[top]; top--; return temp; } public void Push(T t) { data[top + ] = t; top++; } } }
SequenceStack
/* 脚本名称: 脚本作者: 建立时间: 脚本功能: 版本号: */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 栈 { class Node<T> { private T data; private Node<T> next; public T Data { get { return data; } set { data = value; } } public Node<T> Next { get { return next; } set { next = value; } } public Node() { data = default(T); next = null; } public Node(T t) { data = t; next = null; } public Node(Node<T> next) { this.next = next; data = default(T); } public Node(T t,Node<T> next) { data = t; this.next = next; } } }
Node
/* 脚本名称: 脚本作者: 建立时间: 脚本功能: 版本号: */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 栈 { class LinkStack<T>:IStackDS<T> { /// <summary> /// 栈顶 /// </summary> private Node<T> top; /// <summary> /// 栈中元素个数 /// </summary> ; public int Count { get { return count; } } public void Clear() { count = ; top = null; } public int GetLength() { return count; } public bool IsEmpty() { ; } public T Peek() { return top.Data; } public T Pop() { T data = top.Data; top = top.Next; count--; return data; } public void Push(T t) { Node<T> newNode = new Node<T>(t); newNode.Next = top; top = newNode; count++; } } }
LinkStack
队列:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 队列 { interface IQueueDS<T> { /// <summary> /// 队列元素数量 /// </summary> int Count { get; } /// <summary> /// 队列长度 /// </summary> /// <returns></returns> int GetLength(); /// <summary> /// 队列是否为空 /// </summary> /// <returns></returns> bool IsEmpty(); /// <summary> /// 清空队列 /// </summary> void Clear(); /// <summary> /// 入队 /// </summary> /// <param name="t"></param> void Enqueue(T t); /// <summary> /// 出队 /// </summary> /// <returns></returns> T Dequeue(); /// <summary> /// 取队首 /// </summary> /// <returns></returns> T Peek(); } }
IQueueDS
/* 脚本名称: 脚本作者: 建立时间: 脚本功能: 版本号: */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 队列 { class SequenceQueue<T>:IQueueDS<T> { private T[] data; private int count; /// <summary> /// 队首元素索引-1 /// </summary> private int front; /// <summary> /// 队尾元素索引 /// </summary> private int rear; ) { } public SequenceQueue(int size) { data = new T[size]; count = ; front = -; rear = -; } public int Count { get { return count; } } public void Clear() { count = ; front = rear = -; } public T Dequeue() { ) { T temp = data[front + ]; front++; count--; return temp; } else { Console.WriteLine("队列为空"); return default(T); } } public void Enqueue(T t) { if(count == data.Length) { Console.WriteLine("队列已满"); } else { ) { data[] = t; rear = ; count++; } else { data[rear + ] = t; rear++; count++; } } } public int GetLength() { return count; } public bool IsEmpty() { ; } public T Peek() { T temp = data[front + ]; return temp; } } }
SequenceQueue
/* 脚本名称: 脚本作者: 建立时间: 脚本功能: 版本号: */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 队列 { class Node<T> { private T data; private Node<T> next; public Node(T t) { t = data; } public T Data { get { return data; } set { data = value; } } public Node<T> Next { get { return next; } set { next = value; } } } }
Node
/* 脚本名称: 脚本作者: 建立时间: 脚本功能: 版本号: */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 队列 { class LinkQueue<T>:IQueueDS<T> { private Node<T> front; private Node<T> rear; private int count; public LinkQueue() { front = null; rear = null; count = ; } public int Count { get { return count; } } public void Clear() { front = null; rear = null; count = ; } public T Dequeue() { ) { Console.WriteLine("队列为空"); return default(T); } ) { T temp = front.Data; front = rear = null; count = ; return temp; } else { T temp = front.Data; front = front.Next; count--; return temp; } } public void Enqueue(T t) { Node<T> newNode = new Node<T>(t); ) { front = newNode; rear = newNode; count = ; } else { rear.Next = newNode; rear = newNode; count++; } } public int GetLength() { return count; } public bool IsEmpty() { ; } public T Peek() { if(front != null) { return front.Data; } else { return default(T); } } } }
LinkQueue
串:
/* 脚本名称: 脚本作者: 建立时间: 脚本功能: 版本号: */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 串 { class StringDS { private char[] data; public StringDS(char[] array) { data = new char[array.Length]; ;i < data.Length;i++) { data[i] = array[i]; } } public StringDS(string str) { data = new char[str.Length]; ;i < data.Length;i++) { data[i] = str[i]; } } public char this[int index] { get { return data[index]; } } public int GetLength() { return data.Length; } public int Compare(StringDS s) { int length = GetLength() < s.GetLength() ? GetLength() : s.GetLength(); ; ;i < length;i++) { if(this[i] != s[i]) { index = i; break; } } ) { if(this[index] > s[index]) { ; } else { ; } } else { if(GetLength() == s.GetLength()) { ; } else { if(GetLength() > s.GetLength()) { ; } else { ; } } } } public StringDS SubString(int index,int length) { char[] newData = new char[length]; for(int i = index;i < index + length;i++) { newData[i - index] = data[index]; } return new StringDS(newData); } public static StringDS Concat(StringDS s1,StringDS s2) { char[] newData = new char[s1.GetLength() + s2.GetLength()]; ;i < s1.GetLength();i++) { newData[i] = s1[i]; } for(int i = s1.GetLength();i < newData.Length;i++) { newData[i] = s2[i - s1.GetLength()]; } return new StringDS(newData); } public int IndexOf(StringDS s) { ;i <= GetLength()-s.GetLength();i++) { bool isEqual = true; for(int j = i;j < i+s.GetLength();j++) { if(this[j] != s[j - i]) { isEqual = false; } } if(isEqual) { return i; } else { continue; } } ; } } }
StringDS
直接插入排序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 直接插入排序 { class Program { static void InsertSort(int[] dataArray) { ;i < dataArray.Length;i++) { int iValue = dataArray[i]; bool isInsert = false; ;j >= ;j--) { if(dataArray[j]> iValue) { dataArray[j + ] = dataArray[j]; } else { dataArray[j + ] = iValue; isInsert = true; break; } } if(isInsert == false) { dataArray[] = iValue; } } } static void Main(string[] args) { ,,,,,,, }; InsertSort(data); foreach(var item in data) { Console.WriteLine(item + " "); } Console.ReadLine(); } } }
InsertSort
简单选择排序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 简单选择排序 { class Program { static void SelectSort(int[] dataArray) { ;i < dataArray.Length - ;i++) { int min = dataArray[i]; int minIndex = i; ;j < dataArray.Length;j++) { if(dataArray[j] < min) { min = dataArray[j]; minIndex = j; } } if(minIndex != i) { int temp = dataArray[i]; dataArray[i] = dataArray[minIndex]; dataArray[minIndex] = temp; } } } static void Main(string[] args) { ,,,,,,, }; SelectSort(data); foreach(var item in data) { Console.WriteLine(item + " "); } Console.ReadLine(); } } }
SelectSort
快速排序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace 快速排序 { class Program { static void QuickSort(int[] dataArray,int left,int right) { if(left < right) { int x = dataArray[left]; int i = left; int j = right; while(true && i < j) { while(true && i < j) { if(dataArray[j] <= x) { dataArray[i] = dataArray[j]; break; } else { j--; } } while(true && i < j) { if(dataArray[i] > x) { dataArray[j] = dataArray[i]; break; } else { i++; } } } dataArray[i] = x; QuickSort(dataArray,left,i - ); QuickSort(dataArray,i + ,right); } } static void Main(string[] args) { ,,,,,,, }; QuickSort(data,,data.Length - ); foreach(var item in data) { Console.WriteLine(item + " "); } Console.ReadLine(); } } }
QuickSort
项目:https://pan.baidu.com/s/1cxORd8