S老师 C#编程数据结构篇 学习
直接插入排序 冒泡排序


简单选择排序

线性表:
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
S老师 C#编程数据结构篇 学习的更多相关文章
- 图解Redis之数据结构篇——简单动态字符串SDS
图解Redis之数据结构篇--简单动态字符串SDS 前言 相信用过Redis的人都知道,Redis提供了一个逻辑上的对象系统构建了一个键值对数据库以供客户端用户使用.这个对象系统包括字符串对象 ...
- (数据科学学习手札36)tensorflow实现MLP
一.简介 我们在前面的数据科学学习手札34中也介绍过,作为最典型的神经网络,多层感知机(MLP)结构简单且规则,并且在隐层设计的足够完善时,可以拟合任意连续函数,而除了利用前面介绍的sklearn.n ...
- 阶段2-新手上路\项目-移动物体监控系统\Sprint2-摄像头子系统开发\第2节-V4L2图像编程接口深度学习
参考资料: http://www.cnblogs.com/emouse/archive/2013/03/04/2943243.htmlhttp://blog.csdn.net/eastmoon5021 ...
- 菜鸟nginx源代码剖析数据结构篇(六) 哈希表 ngx_hash_t(上)
菜鸟nginx源代码剖析数据结构篇(六) 哈希表 ngx_hash_t(上) Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog. ...
- 菜鸟nginx源代码剖析数据结构篇(九) 内存池ngx_pool_t
菜鸟nginx源代码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn ...
- 菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t[转]
菜鸟nginx源码剖析数据结构篇(九) 内存池ngx_pool_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csdn. ...
- 菜鸟nginx源码剖析数据结构篇(六) 哈希表 ngx_hash_t(上)[转]
菜鸟nginx源码剖析数据结构篇(六) 哈希表 ngx_hash_t(上) Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.c ...
- 菜鸟nginx源码剖析数据结构篇(三) 单向链表 ngx_list_t[转]
菜鸟nginx源码剖析数据结构篇(三) 单向链表 ngx_list_t Author:Echo Chen(陈斌) Email:chenb19870707@gmail.com Blog:Blog.csd ...
- (数据科学学习手札75)基于geopandas的空间数据分析——坐标参考系篇
本文对应代码已上传至我的Github仓库https://github.com/CNFeffery/DataScienceStudyNotes 1 简介 在上一篇文章中我们对geopandas中的数据结 ...
随机推荐
- caffe blob
Blob,包括输入数据.输出数据.权值等: Blob是Caffe中处理和传递实际数据的数据封装包,并且在CPU与GPU之间具有同步处理能力.从数学意义上说,blob是按C风格连续存储的N维数组. ca ...
- SWAP 简介
swap 交换分区,是存放在内存当中的临时数据(断电数据丢失) SWAP作用:当内存不足时会导致系统挂了,SWAP就是起到一个临时内存的作用,当内存不足时SWAP充当临时内存,防止系统挂掉
- centos7中docker操作
docker部署nginx 1. 下载nginx [root@localhost my.Shells]# docker images REPOSITORY TAG IMAGE ID CREATED S ...
- points from ZhiQIng Hu
1,The errors in vertical direction are about 3 times horizontal errors of GPS data. But the precisio ...
- pthread库实现一个简单的任务池
pthread库实现一个简单的任务池 类关系图: 说明: 1:TaskManager类管理Task类,Task类是一个纯虚类; 2:ThreadManager类管理Th ...
- 如何HACK无线家用警报器?
30年前,报警器都是硬连线的,具有分立元件,并由钥匙开关操作.20年前,他们已经演变为使用微控制器,LCD和键盘,但仍然是硬连线.10年前,无线报警器开始变得普及,并增加了许多之前没有的功能. 而如今 ...
- Day5作业及默写
1,有如下变量(tu是个元祖),请实现要求的功能 tu = ("alex", [11, 22, {"k1": 'v1', "k2": [&q ...
- scrapy爬取动态分页内容
1.任务定义: 爬取某动态分页页面中所有子话题的内容. 所谓"动态分页":是指通过javascript(简称"js")点击实现翻页,很多时候翻页后的页面地址ur ...
- day12作业答案
2.1 # lst=['asdgg','as','drtysr'] # lst2=[i.upper() for i in lst if len(i) >3 ] # print(lst2) # 2 ...
- POJ 2001 Shortest Prefixes(字典树)
Description A prefix of a string is a substring starting at the beginning of the given string. The p ...