S老师 C#编程数据结构篇 学习

直接插入排序                                                       冒泡排序

简单选择排序

线性表:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 线性表 {
 8     interface IListDS<T> {
 9         /// <summary>
10         /// 获取线性表长度
11         /// </summary>
12         /// <returns></returns>
13         int GetLength();
14         /// <summary>
15         /// 清空线性表
16         /// </summary>
17         void Clear();
18         /// <summary>
19         /// 判断线性表是否为空
20         /// </summary>
21         /// <returns></returns>
22         bool IsEmpty();
23         /// <summary>
24         /// 添加元素
25         /// </summary>
26         /// <param name="t">元素</param>
27         void Add(T t);
28         /// <summary>
29         /// 插入元素
30         /// </summary>
31         /// <param name="t">元素</param>
32         /// <param name="index">要添加的位置</param>
33         void Insert(T t,int index);
34         /// <summary>
35         /// 根据索引删除元素
36         /// </summary>
37         /// <param name="index">索引</param>
38         /// <returns></returns>
39         T Delete(int index);
40         /// <summary>
41         /// 索引器
42         /// </summary>
43         /// <param name="index"></param>
44         /// <returns></returns>
45         T this[int index] { get; }
46         /// <summary>
47         /// 通过索引获取元素
48         /// </summary>
49         /// <param name="index">索引</param>
50         /// <returns></returns>
51         T GetElement(int index);
52         /// <summary>
53         /// 根据值获得索引
54         /// </summary>
55         /// <param name="t">元素</param>
56         /// <returns></returns>
57         int Locate(T t);
58 
59     }
60 }
IListDS
  1 /*
  2 脚本名称:
  3 脚本作者:
  4 建立时间:
  5 脚本功能:
  6 版本号:
  7 */
  8 using System;
  9 using System.Collections.Generic;
 10 using System.Linq;
 11 using System.Text;
 12 using System.Threading.Tasks;
 13 
 14 namespace 线性表 {
 15 
 16     /// <summary>
 17     /// 顺序表实现
 18     /// </summary>
 19     /// <typeparam name="T"></typeparam>
 20     class SequenceList<T>:IListDS<T> {
 21 
 22         /// <summary>
 23         /// 用来存储数据的数组
 24         /// </summary>
 25         private T[] data;
 26         /// <summary>
 27         /// 存储数据的数量
 28         /// </summary>
 29         private int count;
 30 
 31         /// <summary>
 32         /// 默认构造函数
 33         /// </summary>
 34         public SequenceList() : this(10) {
 35 
 36         }
 37 
 38         /// <summary>
 39         /// 构造函数
 40         /// </summary>
 41         /// <param name="size">最大容量</param>
 42         public SequenceList(int size) {
 43             data = new T[size];
 44         }
 45 
 46 
 47         /// <summary>
 48         /// 索引器
 49         /// </summary>
 50         /// <param name="index"></param>
 51         /// <returns></returns>
 52         public T this[int index] {
 53             get {
 54                 return GetElement(index);
 55             }
 56         }
 57 
 58         /// <summary>
 59         /// 添加数据
 60         /// </summary>
 61         /// <param name="t"></param>
 62         public void Add(T t) {
 63             //当前数组已经满了
 64             if(count == data.Length) {
 65                 Console.WriteLine("顺序表已满");
 66             } else {
 67                 data[count] = t;
 68                 count++;
 69             }
 70         }
 71 
 72         /// <summary>
 73         /// 清空
 74         /// </summary>
 75         public void Clear() {
 76             count = 0;
 77         }
 78 
 79         /// <summary>
 80         /// 删除
 81         /// </summary>
 82         /// <param name="index"></param>
 83         /// <returns></returns>
 84         public T Delete(int index) {
 85             T temp = data[index];
 86             for(int i = index+1;i < count;i++) {
 87                 data[i - 1] = data[i];
 88             }
 89             count--;
 90             return temp;
 91         }
 92 
 93         /// <summary>
 94         /// 根据索引取得数据
 95         /// </summary>
 96         /// <param name="index"></param>
 97         /// <returns></returns>
 98         public T GetElement(int index) {
 99             if(index >= 0 && index <= count - 1) {
100                 return data[index];
101             } else {
102                 Console.WriteLine("索引不存在");
103             }
104             return default(T);
105         }
106 
107         /// <summary>
108         /// 获取数据的个数
109         /// </summary>
110         /// <returns></returns>
111         public int GetLength() {
112             return count;
113         }
114 
115         /// <summary>
116         /// 插入数据
117         /// </summary>
118         /// <param name="t"></param>
119         /// <param name="index"></param>
120         public void Insert(T t,int index) {
121             for(int i = count - 1;i >= index;index--) {
122                 data[i + 1] = data[i];
123             }
124             data[index] = t;
125             count++;
126         }
127 
128         /// <summary>
129         /// 线性表是否为空
130         /// </summary>
131         /// <returns></returns>
132         public bool IsEmpty() {
133             return count == 0;
134         }
135 
136         /// <summary>
137         /// 获得元素所在的位置
138         /// </summary>
139         /// <param name="t"></param>
140         /// <returns></returns>
141         public int Locate(T t) {
142             for(int i = 0;i < count;i++) {
143                 if(data[i].Equals(t)) {
144                     return i;
145                 }
146             }
147             return -1;
148         }
149     }
150 }
SequenceList
 1 /*
 2 脚本名称:
 3 脚本作者:
 4 建立时间:
 5 脚本功能:
 6 版本号:
 7 */
 8 using System;
 9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System.Threading.Tasks;
13 
14 namespace 线性表 {
15 
16     class Node<T> {
17         /// <summary>
18         /// 存储数据
19         /// </summary>
20         private T data;
21         /// <summary>
22         /// 指向下一个数据
23         /// </summary>
24         private Node<T> next;
25 
26         /// <summary>
27         /// 
28         /// </summary>
29         public T Data {
30             get {
31                 return data;
32             }
33             set {
34                 data = value;
35             }
36         }
37 
38         /// <summary>
39         /// 
40         /// </summary>
41         public Node<T> Next {
42             get {
43                 return next;
44             }
45             set {
46                 next = value;
47             }
48         }
49 
50         /// <summary>
51         /// 
52         /// </summary>
53         public Node() {
54             data = default(T);
55             next = null;
56         }
57 
58         /// <summary>
59         /// 
60         /// </summary>
61         public Node(T t) {
62             data = t;
63             next = null;
64         }
65 
66         /// <summary>
67         /// 
68         /// </summary>
69         /// <param name="t"></param>
70         /// <param name="next"></param>
71         public Node(T t,Node<T> next) {
72             data = t;
73             this.next = next;
74         }
75 
76         /// <summary>
77         /// 
78         /// </summary>
79         public Node(Node<T> next) {
80             this.next = next;
81         }
82     }
83 }
Node
  1 /*
  2 脚本名称:
  3 脚本作者:
  4 建立时间:
  5 脚本功能:
  6 版本号:
  7 */
  8 using System;
  9 using System.Collections.Generic;
 10 using System.Linq;
 11 using System.Text;
 12 using System.Threading.Tasks;
 13 
 14 namespace 线性表 {
 15 
 16     class LinkList<T>:IListDS<T> {
 17 
 18         /// <summary>
 19         /// 头节点
 20         /// </summary>
 21         private Node<T> head;
 22 
 23 
 24         public LinkList() {
 25             head = null;
 26         }
 27 
 28         /// <summary>
 29         /// 索引器
 30         /// </summary>
 31         /// <param name="index"></param>
 32         /// <returns></returns>
 33         public T this[int index] {
 34             get {
 35                 Node<T> temp = head;
 36                 for(int i = 1;i <= index;i++) {
 37                     temp = temp.Next;
 38                 }
 39                 return temp.Data;
 40             }
 41         }
 42 
 43         /// <summary>
 44         /// 添加
 45         /// </summary>
 46         /// <param name="t"></param>
 47         public void Add(T t) {
 48             Node<T> newNode = new Node<T>(t);
 49             if(head == null) {
 50                 head = newNode;
 51             } else {
 52                 Node<T> temp = head;
 53                 while(true) {
 54                     if(temp.Next != null) {
 55                         temp = temp.Next;
 56                     } else {
 57                         break;
 58                     }
 59                 }
 60                 temp.Next = newNode;
 61             }
 62         }
 63         
 64         /// <summary>
 65         /// 清空
 66         /// </summary>
 67         public void Clear() {
 68             head = null;
 69         }
 70 
 71         /// <summary>
 72         /// 删除
 73         /// </summary>
 74         /// <param name="index"></param>
 75         /// <returns></returns>
 76         public T Delete(int index) {
 77             T data = default(T);
 78             if(index == 0) {
 79                 data = head.Data;
 80                 head = head.Next;
 81             } else {
 82                 Node<T> temp = head;
 83 
 84                 for(int i = 1;i <= index - 1;i++) {
 85                     temp = temp.Next;
 86                 }
 87 
 88                 Node<T> preNode = temp;
 89                 Node<T> currentNode = temp.Next;
 90                 data = currentNode.Data;
 91                 Node<T> nextNode = temp.Next.Next;
 92                 preNode.Next = nextNode;
 93             }
 94             return data;
 95         }
 96 
 97         /// <summary>
 98         /// 根据位置获得元素
 99         /// </summary>
100         /// <param name="index"></param>
101         /// <returns></returns>
102         public T GetElement(int index) {
103             return this[index];
104         }
105 
106         /// <summary>
107         /// 获取长度
108         /// </summary>
109         /// <returns></returns>
110         public int GetLength() {
111             if(head == null) {
112                 return 0;
113             }
114             Node<T> temp = head;
115             int count = 1;
116             while(true) {
117                 if(temp.Next != null) {
118                     count++;
119                     temp = temp.Next;
120                 } else {
121                     break;
122                 }
123             }
124             return count;
125         }
126 
127         /// <summary>
128         /// 插入
129         /// </summary>
130         /// <param name="t"></param>
131         /// <param name="index"></param>
132         public void Insert(T t,int index) {
133             Node<T> newNode = new Node<T>(t);
134 
135             if(index == 0) {
136                 newNode.Next = head;
137                 head = newNode;
138             } else {
139                 Node<T> temp = head;
140 
141                 for(int i = 1;i <= index - 1;i++) {
142                     temp = temp.Next;
143                 }
144 
145                 Node<T> preNode = temp;
146                 Node<T> currentNode = temp.Next;
147                 preNode.Next = newNode;
148                 newNode.Next = currentNode;
149             }
150         }
151 
152         /// <summary>
153         /// 是否为空
154         /// </summary>
155         /// <returns></returns>
156         public bool IsEmpty() {
157             return head == null;
158         }
159 
160         /// <summary>
161         /// 根据元素获得位置
162         /// </summary>
163         /// <param name="t"></param>
164         /// <returns></returns>
165         public int Locate(T t) {
166             Node<T> temp = head;
167             if(temp == null) {
168                 return -1;
169             } else {
170                 int index = 0;
171                 while(true) {
172                     if(temp.Data.Equals(t)) {
173                         return index;
174                     } else {
175                         if(temp.Next != null) {
176                             temp = temp.Next;
177                         } else {
178                             break;
179                         }
180                     }
181                 }
182                 return -1;
183             }
184         }
185     }
186 }
LinkList

栈:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 栈 {
 8     interface IStackDS<T> {
 9         /// <summary>
10         /// 获取长度
11         /// </summary>
12         int Count { get; }
13         /// <summary>
14         /// 获取长度
15         /// </summary>
16         /// <returns></returns>
17         int GetLength();
18         /// <summary>
19         /// 是否为空
20         /// </summary>
21         /// <returns></returns>
22         bool IsEmpty();
23         /// <summary>
24         /// 清空
25         /// </summary>
26         void Clear();
27         /// <summary>
28         /// 入栈
29         /// </summary>
30         /// <param name="t"></param>
31         void Push(T t);
32         /// <summary>
33         /// 出栈
34         /// </summary>
35         /// <returns></returns>
36         T Pop();
37         /// <summary>
38         /// 取栈顶
39         /// </summary>
40         /// <returns></returns>
41         T Peek();
42     }
43 }
IStackDS
 1 /*
 2 脚本名称:
 3 脚本作者:
 4 建立时间:
 5 脚本功能:
 6 版本号:
 7 */
 8 using System;
 9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System.Threading.Tasks;
13 
14 namespace 栈 {
15 
16     /// <summary>
17     /// 顺序栈
18     /// </summary>
19     class SequenceStack<T>:IStackDS<T> {
20 
21         private T[] data;
22         /// <summary>
23         /// 栈顶索引
24         /// </summary>
25         private int top;
26 
27         public SequenceStack() : this(10) {
28 
29         }
30 
31         public SequenceStack(int size) {
32             data = new T[size];
33             top = -1;
34         }
35 
36 
37 
38         /// <summary>
39         /// 获取数量
40         /// </summary>
41         public int Count {
42             get {
43                 return top + 1;
44             }
45         }
46 
47         public void Clear() {
48             top = -1;
49         }
50 
51         public int GetLength() {
52             return Count;
53         }
54 
55         public bool IsEmpty() {
56             return Count == 0;
57         }
58 
59         public T Peek() {
60             return data[top];
61         }
62 
63         public T Pop() {
64             T temp = data[top];
65             top--;
66             return temp;
67         }
68 
69         public void Push(T t) {
70             data[top + 1] = t;
71             top++;
72         }
73     }
74 }
SequenceStack
 1 /*
 2 脚本名称:
 3 脚本作者:
 4 建立时间:
 5 脚本功能:
 6 版本号:
 7 */
 8 using System;
 9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System.Threading.Tasks;
13 
14 namespace 栈 {
15 
16 
17     class Node<T> {
18 
19         private T data;
20 
21         private Node<T> next;
22 
23         public T Data {
24             get {
25                 return data;
26             }
27             set {
28                 data = value;
29             }
30         }
31 
32         public Node<T> Next {
33             get {
34                 return next;
35             }
36             set {
37                 next = value;
38             }
39         }
40 
41         public Node() {
42             data = default(T);
43             next = null;
44         }
45 
46         public Node(T t) {
47             data = t;
48             next = null;
49         }
50 
51         public Node(Node<T> next) {
52             this.next = next;
53             data = default(T);
54         }
55 
56         public Node(T t,Node<T> next) {
57             data = t;
58             this.next = next;
59         }
60 
61 
62     }
63 }
Node
 1 /*
 2 脚本名称:
 3 脚本作者:
 4 建立时间:
 5 脚本功能:
 6 版本号:
 7 */
 8 using System;
 9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System.Threading.Tasks;
13 
14 namespace 栈 {
15     class LinkStack<T>:IStackDS<T> {
16 
17         /// <summary>
18         /// 栈顶
19         /// </summary>
20         private Node<T> top;
21         /// <summary>
22         /// 栈中元素个数
23         /// </summary>
24         private int count = 0;
25 
26         public int Count {
27             get {
28                 return count;
29             }
30         }
31 
32         public void Clear() {
33             count = 0;
34             top = null;
35         }
36 
37         public int GetLength() {
38             return count;
39         }
40 
41         public bool IsEmpty() {
42             return count == 0;
43         }
44 
45         public T Peek() {
46             return top.Data;
47         }
48 
49         public T Pop() {
50             T data = top.Data;
51             top = top.Next;
52             count--;
53             return data;
54         }
55 
56         public void Push(T t) {
57             Node<T> newNode = new Node<T>(t);
58             newNode.Next = top;
59             top = newNode;
60             count++;
61         }
62     }
63 }
LinkStack

队列:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 队列 {
 8 
 9     interface IQueueDS<T> {
10         
11         /// <summary>
12         /// 队列元素数量
13         /// </summary>
14         int Count { get; }
15 
16         /// <summary>
17         /// 队列长度
18         /// </summary>
19         /// <returns></returns>
20         int GetLength();
21 
22         /// <summary>
23         /// 队列是否为空
24         /// </summary>
25         /// <returns></returns>
26         bool IsEmpty();
27 
28         /// <summary>
29         /// 清空队列
30         /// </summary>
31         void Clear();
32 
33         /// <summary>
34         /// 入队
35         /// </summary>
36         /// <param name="t"></param>
37         void Enqueue(T t);
38 
39         /// <summary>
40         /// 出队
41         /// </summary>
42         /// <returns></returns>
43         T Dequeue();
44 
45         /// <summary>
46         /// 取队首
47         /// </summary>
48         /// <returns></returns>
49         T Peek();
50     }
51 }
IQueueDS
 1 /*
 2 脚本名称:
 3 脚本作者:
 4 建立时间:
 5 脚本功能:
 6 版本号:
 7 */
 8 using System;
 9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System.Threading.Tasks;
13 
14 namespace 队列 {
15     class SequenceQueue<T>:IQueueDS<T> {
16 
17         private T[] data;
18 
19         private int count;
20 
21         /// <summary>
22         /// 队首元素索引-1
23         /// </summary>
24         private int front;
25 
26         /// <summary>
27         /// 队尾元素索引
28         /// </summary>
29         private int rear;
30 
31         public SequenceQueue() : this(10) {
32 
33         }
34 
35         public SequenceQueue(int size) {
36             data = new T[size];
37             count = 0;
38             front = -1;
39             rear = -1;
40         }
41 
42         public int Count {
43             get {
44                 return count;
45             }
46         }
47 
48         public void Clear() {
49             count = 0;
50             front = rear = -1;
51         }
52 
53         public T Dequeue() {
54             if(count > 0) {
55                 T temp = data[front + 1];
56                 front++;
57                 count--;
58                 return temp;
59             } else {
60                 Console.WriteLine("队列为空");
61                 return default(T);
62             }
63         }
64 
65         public void Enqueue(T t) {
66             if(count == data.Length) {
67                 Console.WriteLine("队列已满");
68             } else {
69                 if(rear == data.Length - 1) {
70                     data[0] = t;
71                     rear = 0;
72                     count++;
73                 } else {
74                     data[rear + 1] = t;
75                     rear++;
76                     count++;
77                 }
78             }
79         }
80 
81         public int GetLength() {
82             return count;
83         }
84 
85         public bool IsEmpty() {
86             return count == 0;
87         }
88 
89         public T Peek() {
90             T temp = data[front + 1];
91             return temp;
92         }
93     }
94 }
SequenceQueue
 1 /*
 2 脚本名称:
 3 脚本作者:
 4 建立时间:
 5 脚本功能:
 6 版本号:
 7 */
 8 using System;
 9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System.Threading.Tasks;
13 
14 namespace 队列 {
15     class Node<T> {
16         private T data;
17 
18         private Node<T> next;
19 
20         public Node(T t) {
21             t = data;
22         }
23 
24         public T Data {
25             get {
26                 return data;
27             }
28             set {
29                 data = value;
30             }
31         }
32 
33         public Node<T> Next {
34             get {
35                 return next;
36             }
37             set {
38                 next = value;
39             }
40         }
41 
42 
43     }
44 }
Node
 1 /*
 2 脚本名称:
 3 脚本作者:
 4 建立时间:
 5 脚本功能:
 6 版本号:
 7 */
 8 using System;
 9 using System.Collections.Generic;
10 using System.Linq;
11 using System.Text;
12 using System.Threading.Tasks;
13 
14 namespace 队列 {
15     class LinkQueue<T>:IQueueDS<T> {
16 
17         private Node<T> front;
18 
19         private Node<T> rear;
20 
21         private int count;
22 
23         public LinkQueue() {
24             front = null;
25             rear = null;
26             count = 0;
27         }
28 
29         public int Count {
30             get {
31                 return count;
32             }
33         }
34 
35         public void Clear() {
36             front = null;
37             rear = null;
38             count = 0;
39         }
40 
41         public T Dequeue() {
42             if(count == 0) {
43                 Console.WriteLine("队列为空");
44                 return default(T);
45             } else if(count == 1) {
46                 T temp = front.Data;
47                 front = rear = null;
48                 count = 0;
49                 return temp;
50             } else {
51                 T temp = front.Data;
52                 front = front.Next;
53                 count--;
54                 return temp;
55             }
56         }
57 
58         public void Enqueue(T t) {
59             Node<T> newNode = new Node<T>(t);
60             if(count == 0) {
61                 front = newNode;
62                 rear = newNode;
63                 count = 1;
64             } else {
65                 rear.Next = newNode;
66                 rear = newNode;
67                 count++;
68             }
69         }
70 
71         public int GetLength() {
72             return count;
73         }
74 
75         public bool IsEmpty() {
76             return count == 0;
77         }
78 
79         public T Peek() {
80             if(front != null) {
81                 return front.Data;
82             } else {
83                 return default(T);
84             }
85         }
86     }
87 }
LinkQueue

串:

  1 /*
  2 脚本名称:
  3 脚本作者:
  4 建立时间:
  5 脚本功能:
  6 版本号:
  7 */
  8 using System;
  9 using System.Collections.Generic;
 10 using System.Linq;
 11 using System.Text;
 12 using System.Threading.Tasks;
 13 
 14 namespace 串 {
 15 
 16     class StringDS {
 17 
 18         private char[] data;
 19 
 20         public StringDS(char[] array) {
 21             data = new char[array.Length];
 22             for(int i = 0;i < data.Length;i++) {
 23                 data[i] = array[i];
 24             }
 25         }
 26 
 27         public StringDS(string str) {
 28             data = new char[str.Length];
 29             for(int i = 0;i < data.Length;i++) {
 30                 data[i] = str[i];
 31             }
 32         }
 33 
 34         public char this[int index] {
 35             get {
 36                 return data[index];
 37             }
 38         }
 39 
 40         public int GetLength() {
 41             return data.Length;
 42         }
 43 
 44         public int Compare(StringDS s) {
 45 
 46             int length = GetLength() < s.GetLength() ? GetLength() : s.GetLength();
 47             int index = -1;
 48 
 49             for(int i = 0;i < length;i++) {
 50                 if(this[i] != s[i]) {
 51                     index = i;
 52                     break;
 53                 }
 54             }
 55 
 56             if(index != -1) {
 57                 if(this[index] > s[index]) {
 58                     return 1;
 59                 } else {
 60                     return -1;
 61                 }
 62             } else {
 63                 if(GetLength() == s.GetLength()) {
 64                     return 0;
 65                 } else {
 66                     if(GetLength() > s.GetLength()) {
 67                         return 1;
 68                     } else {
 69                         return -1;
 70                     }
 71                 }
 72             }
 73         }
 74 
 75         public StringDS SubString(int index,int length) {
 76             char[] newData = new char[length];
 77 
 78             for(int i = index;i < index + length;i++) {
 79                 newData[i - index] = data[index];
 80             }
 81             return new StringDS(newData);
 82         }
 83 
 84         public static StringDS Concat(StringDS s1,StringDS s2) {
 85             char[] newData = new char[s1.GetLength() + s2.GetLength()];
 86             for(int i = 0;i < s1.GetLength();i++) {
 87                 newData[i] = s1[i];
 88             }
 89 
 90             for(int i = s1.GetLength();i < newData.Length;i++) {
 91                 newData[i] = s2[i - s1.GetLength()];
 92             }
 93 
 94             return new StringDS(newData);
 95         }
 96 
 97         public int IndexOf(StringDS s) {
 98             for(int i = 0;i <= GetLength()-s.GetLength();i++) {
 99                 bool isEqual = true;
100                 for(int j = i;j < i+s.GetLength();j++) {
101                     if(this[j] != s[j - i]) {
102                         isEqual = false;
103                     }
104                 }
105                 if(isEqual) {
106                     return i;
107                 } else {
108                     continue;
109                 }
110             }
111             return -1;
112         }
113     }
114 }
StringDS

直接插入排序:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 直接插入排序 {
 8     class Program {
 9 
10         static void InsertSort(int[] dataArray) {
11 
12             for(int i = 1;i < dataArray.Length;i++) {
13                 int iValue = dataArray[i];
14                 bool isInsert = false;
15 
16                 for(int j = i - 1;j >= 0;j--) {
17                     if(dataArray[j]> iValue) {
18                         dataArray[j + 1] = dataArray[j];
19                     } else {
20                         dataArray[j + 1] = iValue;
21                         isInsert = true;
22                         break;
23                     }
24                 }
25                 if(isInsert == false) {
26                     dataArray[0] = iValue;
27                 }
28             }
29         }
30 
31         static void Main(string[] args) {
32             int[] data = new int[] { 42,20,17,27,13,8,17,48 };
33             InsertSort(data);
34 
35             foreach(var item in data) {
36                 Console.WriteLine(item + " ");
37             }
38             Console.ReadLine();
39         }
40     }
41 }
InsertSort

简单选择排序:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 简单选择排序 {
 8     class Program {
 9 
10         static void SelectSort(int[] dataArray) {
11             for(int i = 0;i < dataArray.Length - 1;i++) {
12                 int min = dataArray[i];
13                 int minIndex = i;
14 
15                 for(int j = i + 1;j < dataArray.Length;j++) {
16                     if(dataArray[j] < min) {
17                         min = dataArray[j];
18                         minIndex = j;
19                     }
20                 }
21                 if(minIndex != i) {
22                     int temp = dataArray[i];
23                     dataArray[i] = dataArray[minIndex];
24                     dataArray[minIndex] = temp;
25                 }
26             }
27             
28         }
29 
30         static void Main(string[] args) {
31 
32             int[] data = new int[] { 42,20,17,27,13,8,17,48 };
33             SelectSort(data);
34 
35             foreach(var item in data) {
36                 Console.WriteLine(item + " ");
37             }
38             Console.ReadLine();
39         }
40     }
41 }
SelectSort

快速排序:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Threading.Tasks;
 6 
 7 namespace 快速排序 {
 8     class Program {
 9 
10 
11         static void QuickSort(int[] dataArray,int left,int right) {
12             if(left < right) {
13                 int x = dataArray[left];
14                 int i = left;
15                 int j = right;
16 
17                 while(true && i < j) {
18 
19                     while(true && i < j) {
20                         if(dataArray[j] <= x) {
21                             dataArray[i] = dataArray[j];
22                             break;
23                         } else {
24                             j--;
25                         }
26                     }
27 
28                     while(true && i < j) {
29                         if(dataArray[i] > x) {
30                             dataArray[j] = dataArray[i];
31                             break;
32                         } else {
33                             i++;
34                         }
35                     }
36                 }
37 
38                 dataArray[i] = x;
39 
40                 QuickSort(dataArray,left,i - 1);
41                 QuickSort(dataArray,i + 1,right);
42             }
43         }
44 
45         static void Main(string[] args) {
46             int[] data = new int[] { 42,20,17,27,13,8,17,48 };
47             QuickSort(data,0,data.Length - 1);
48 
49             foreach(var item in data) {
50                 Console.WriteLine(item + " ");
51             }
52             Console.ReadLine();
53         }
54     }
55 }
QuickSort

 

项目:https://pan.baidu.com/s/1cxORd8

转载于:https://www.cnblogs.com/revoid/p/6399687.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值