# 常用数据结构及算法C#实现

1.冒泡排序、选择排序、插入排序（三种简单非递归排序）

 1             int[] waitSort = { 1,0, 12, 13, 14, 5, 6, 7, 8, 9, 10 };
2
3             //冒泡排序
4             int length = waitSort.Length;
5
6             for (int i = 0; i < length; i++)
7             {
8                 for (int j = i + 1; j < length; j++)
9                 {
10                     if (waitSort[j] > waitSort[i])
11                     {
12                         int temp = waitSort[j];
13                         waitSort[j] = waitSort[i];
14                         waitSort[i] = temp;
15                     }
16                 }
17             }
18
19             //选择排序
20             int pos1;
21             for (int i = 0; i < length; i++)
22             {
23                 pos1 = i;
24                 for (int j = i + 1; j < length; j++)
25                 {
26                     if (waitSort[pos1] < waitSort[j])
27                     {
28                         pos1 = j;
29                     }
30                 }
31                 if (pos1 != i)
32                 {
33                     int temp = waitSort[pos1];
34                     waitSort[pos1] = waitSort[i];
35                     waitSort[i] = temp;
36                 }
37             }
38
39             //插入排序
40             for (int i = 1; i < length; i++)
41             {
42                 for (int k = i; k > 0; k--)
43                 {
44                     if (waitSort[k] > waitSort[k - 1])
45                     {
46                         int temp = waitSort[k];
47                         waitSort[k] = waitSort[k - 1];
48                         waitSort[k - 1] = temp;
49                     }
50                 }
51             }
52
53             foreach (var i in waitSort)
54             {
55                 Console.WriteLine(i);
56             }
57             Console.ReadKey();

2.快速排序

C#版：

 1         static int[] a = { 2, 5, 8, 6, 3, 4, 7, 9, 1,20,12,15,7,20,2 };
2         static void Main(string[] args)
3         {
4             QuickSort(0, a.Length - 1);
5             foreach (var t in a)
6             {
7                 Console.WriteLine(t);
8             }
10         }
11
12         static void QuickSort(int low,int high)
13         {
14             if (low < high)
15             {
16                 int partition=Partition(low,high);
17                 QuickSort(low, partition-1);
18                 QuickSort(partition+1, high);
19             }
20         }
21
22         static int Partition(int low, int high)
23         {
24             int point = a[low];
25             while (low < high)
26             {
27                 while (a[high] <= point && low<high)
28                 {
29                     high--;
30                 }
31                 Swap(high, low);
32                 while (a[low] >= point && low<high)
33                 {
34                     low++;
35                 }
36                 Swap(high, low);
37             }
38             return low;
39         }
40
41         static void Swap(int x, int y)
42         {
43             int temp = a[x];
44             a[x] = a[y];
45             a[y] = temp;
46         }

Java版：

package test.JavaProject;

import org.junit.Test;

public class QuickSort {

@Test
public void test(){
int[] num = {1,5,6,9,8,7,5,3,5,4,1,2,6};
Qsort(num,0,num.length-1);

for(int n:num){
System.out.println(n);
}
}

public void Qsort(int[] num ,int left,int right){
if(left<right){
int p = partition(num,left,right);
Qsort(num,left,p-1);
Qsort(num,p+1,right);
}

}

public int partition(int[] num,int left,int right){
int pivot = num[left];
while(right>left){
while(left<right && num[right]>=pivot){
right--;
}
exchenge(num,left,right);
while(left<right && num[left]<=pivot){
left++;
}
exchenge(num,left,right);
}
return left;
}

public void exchenge(int[] num,int m,int n){
int temp = num[m];
num[m]=num[n];
num[n]=temp;
}

}

3.二叉排序树

 1 namespace BinarySortTree
2 {
3     class Node
4     {
5         public int Num { get; set; }
6         public Node LChild { get; set; }
7         public Node RChild { get; set; }
8     }
9
10     class BinarySortTree
11     {
12         public Node Root { get; set; }
13         public BinarySortTree()
14         {
15             Root = new Node();
16         }
17     }
18
19     class Program
20     {
21         static void Main(string[] args)
22         {
23             int[] sort = { 2, 5, 8, 3, 9, 6, 1, 7, 4,2,2,2 };
24             BinarySortTree bst = new BinarySortTree();
25             bst.Root.Num = 2;
26             for (int i = 1; i < sort.Length; i++)
27             {
28                 InsertBst(bst.Root, sort[i]);
29             }
30             DFS(bst.Root);
32         }
33
34         static void InsertBst(Node parent,int num)
35         {
36             if (num <= parent.Num)
37             {
38                 if (parent.LChild == null)
39                 {
40                     parent.LChild = new Node();
41                     parent.LChild.Num = num;
42                     return;
43                 }
44                 else
45                 {
46                     InsertBst(parent.LChild, num);
47                 }
48             }
49             else
50             {
51                 if (parent.RChild == null)
52                 {
53                     parent.RChild = new Node();
54                     parent.RChild.Num = num;
55                     return;
56                 }
57                 else
58                 {
59                     InsertBst(parent.RChild, num);
60                 }
61             }
62         }
63
64         static void DFS(Node parent)
65         {
66
67             if (parent.LChild != null)
68             {
69                 DFS(parent.LChild);
70
71             }
72             Console.WriteLine(parent.Num);
73
74             if (parent.RChild != null)
75             {
76                 DFS(parent.RChild);
77             }
78         }
79     }
80 }

4.堆排

 1 namespace HeapSort
2 {
3     class Program
4     {
5         static int[] a = new int[] { -1,1, 5, 9, 3, 7, 6, 4, 2, 8 };
6         static void Main(string[] args)
7         {
8             HeapSort(a.Length-1);
9         }
10
11         static void HeapSort(int len)
12         {
13             //第一次调整得到最小堆，即k>2k+1 && k>2k
14             for (int i = len/2; i >= 1; i--)
15             {
17             }
18
19             //第二次先交换第一个节点和最后一个节点，使堆顶元素最小，然后调整
20             for (int i = len; i >= 2; i--)
21             {
22                 Swap(1, i);
24             }
25         }
26
27         static void Swap(int m,int n)
28         {
29             int temp = a[m];
30             a[m] = a[n];
31             a[n] = temp;
32         }
33
34         static void Adjust(int parent,int len)
35         {
36             int l = 2 * parent;
37             int r = 2 * parent + 1;
38             int largest = parent;
39             //选出最大的节点，用于与父节点交换位置
40             if (l <=len && a[l] > a[largest])
41             {
42                 largest = l;
43             }
44             if (r<=len && a[r]>a[largest])
45             {
46                 largest = r;
47             }
48             //如果需要调整父节点，先交换然后调整交换节点与其孩子节点
49             if (largest != parent)
50             {
51                 Swap(parent, largest);
53             }
54         }
55     }
56 }

5.栈的实现

 1 namespace 栈
2 {
3     public class MyStack
4     {
5         private int index = -1;
6         private int[] a = new int[100];
7
8         public void Push(int num)
9         {
10             a[++index] = num;
11         }
12
13         public int? Pop()
14         {
15             if (index == -1)
16             {
17                 return null;
18             }
19             return a[index--];
20         }
21     }
22 }
23
24 namespace 栈
25 {
26     class Program
27     {
28         static void Main(string[] args)
29         {
30             MyStack stack = new MyStack();
31             stack.Push(1);
32             stack.Push(2);
33             stack.Push(3);
34             stack.Push(4);
35             int? temp;
36             while ((temp = stack.Pop()) != null)
37             {
38                 Console.WriteLine(temp);
39             }
40
41             stack.Push(4);
42             stack.Push(3);
43             stack.Push(2);
44             stack.Push(1);
45             while ((temp = stack.Pop()) != null)
46             {
47                 Console.WriteLine(temp);
48             }
50         }
51     }
52 }

6.List实现

namespace MyList
{
public class Node
{
public int Num{get;set;}
public Node Next{get;set;}
}

public class MyList
{
public Node Head { get; set; }
public int Length { get; set; }

public MyList()
{
Length = 1;
}

{
Node n = new Node();
n.Num = num;
while (node.Next != null)
{
node = node.Next;
}
node.Next = n;
Length++;
}

public void Delete(int index)
{
if (index == 0)
{
}
else if(Length-1==index)
{
for (int i = 0; i < index - 1; i++)
{
n = n.Next;
}
n.Next = null;
}
else
{
for (int i = 0; i < index - 1; i++)
{
n = n.Next;
}
n.Next = n.Next.Next;
}
Length--;
}

public int this[int index]
{
get {
for (int i = 0; i < index; i++)
{
n = n.Next;
}
return n.Num;
}
set  {
for (int i = 0; i < index; i++)
{
n = n.Next;
}
n.Num = value;
}
}
}
}

namespace MyList
{
class Program
{
static void Main(string[] args)
{
MyList list = new MyList();

Console.WriteLine("链表长度："+list.Length);
Console.WriteLine(n.Num);
while (n.Next != null)
{
n = n.Next;
Console.WriteLine(n.Num);
}

}
}
}

7.DFS(深搜)/BFS(宽搜)

 1         private static void DFS(XmlNode parent)
2         {
3             if (!parent.HasChildNodes)
4             {
5                 return;
6             }
7             else if (parent.HasChildNodes)
8             {
9                 for (int j = 0; j < parent.ChildNodes.Count; j++)
10                 {
11                     if (parent.ChildNodes[j].Name == "span")
12                     {
14                     }
15                     DFS(parent.ChildNodes[j]);
16                 }
17             }
18         }
19
20
21         public static void BFS(XmlNode root)
22         {
23             queue.Enqueue(root);
24             while (queue.Count != 0)
25             {
26                 XmlNode n = queue.Dequeue();
27                 if (n.Name == "span")
28                 {
30                 }
31                 for (int i = 0; i < n.ChildNodes.Count; i++)
32                 {
33                     queue.Enqueue(n.ChildNodes[i]);
34                 }
35             }
36         }

8.优先队列（堆实现）

 1 namespace PriorityQueue
2 {
3     class Program
4     {
5         static List<int> list = new List<int>();
6         static void Main(string[] args)
7         {
9
10             EnQueue(1);
11             EnQueue(3);
12             EnQueue(2);
13             EnQueue(4);
14             EnQueue(5);
15             EnQueue(7);
16
17
18             Console.WriteLine(DeQueue());
19
20             EnQueue(10);
21             EnQueue(8);
22             EnQueue(15);
23
24             int num = -1;
25             while ((num = DeQueue()) != -1)
26             {
27                 Console.WriteLine(num);
28             }
30         }
31
32         static void EnQueue(int num)
33         {
36         }
37
38         static int DeQueue()
39         {
40             int len = list.Count-1;
41             if (len == 0)
42             {
43                 return -1;
44             }
45
46             Swap(1, len);
47             int num = list[len];
48             list.RemoveAt(len);
49
50             int len1 = list.Count - 1;
52             return num;
53         }
54
55         static void Adjust(int parent, int len)
56         {
57             if (parent == 0)
58             {
59                 return;
60             }
61             int l = 2 * parent;
62             int r = 2 * parent + 1;
63             int min = parent;
64             //选出最大的节点，用于与父节点交换位置
65             if (l <= len && list[l] > list[min])
66             {
67                 min = l;
68             }
69             if (r <= len && list[r] > list[min])
70             {
71                 min = r;
72             }
73             //如果需要调整父节点，先交换然后调整父节点的父节点
74             if (min != parent)
75             {
76                 Swap(parent, min);
78             }
79         }
80
81         static void Swap(int m, int n)
82         {
83             int temp = list[m];
84             list[m] = list[n];
85             list[n] = temp;
86         }
87
88
89     }
90 }

9.归并排序

C#版：

 1 namespace MergeSort
2 {
3     class Program
4     {
5         static int[] a = new int[] { 1,2,2,2, 3, 5, 7, 9, 2, 4, 8, 6 ,12,33,44,11,22,33};
6         static void Main(string[] args)
7         {
8             MergeSort(0, a.Length-1);
9         }
10
11         static void MergeSort(int first, int last)
12         {
13             if (first < last)
14             {
15                 int mid = (first + last) / 2;
16                 MergeSort(first, mid);
17                 MergeSort(mid + 1, last);
18                 Merge(first, mid, last);
19             }
20         }
21
22         static void Merge(int first, int mid, int last)
23         {
24             int[] temp = new int[last - first + 1];
25             int l = first;
26             int r = mid + 1;
27             int index = 0;
28
29             while (l <= mid && r <= last)
30             {
31                 if (a[l] > a[r])
32                 {
33                     temp[index++] = a[l++];
34                 }
35                 else
36                 {
37                     temp[index++] = a[r++];
38                 }
39             }
40
41             while (l <= mid)
42             {
43                 temp[index++] = a[l++];
44             }
45             while (r <= last)
46             {
47                 temp[index++] = a[r++];
48             }
49
50             index = 0;
51             while (first <= last)
52             {
53                 a[first++] = temp[index++];
54             }
55         }
56     }
57 }

Java版：

package test.JavaProject;

import org.junit.Test;

public class MergeSort {
@Test
public void test(){
int[] num = {1,5,6,9,8,7,5,3,5,4,1,2,6};
mergeSort(num,0,num.length-1);

for(int n:num){
System.out.println(n);
}
}

public void mergeSort(int[] num,int first,int last){
if(first<last){
int middle = (last+first)/2;
mergeSort(num,first,middle);
mergeSort(num,middle+1,last);
merge(num,first,middle,last);
}
}

public void merge(int[] num,int first,int mid,int last){
int[] temp = new int[last-first+1];
int l = first;
int r = mid+1;
int index=0;

while(l<=mid && r<=last){
if(num[l]<num[r]){
temp[index++]=num[l++];
}
else{
temp[index++]=num[r++];
}
}
while(l<=mid){
temp[index++]=num[l++];
}
while(r<=last){
temp[index++]=num[r++];
}
index=0;
while(first<=last){
num[first++]=temp[index++];
}
}
}

10.二分查找

 1 namespace BinarySearch
2 {
3     class Program
4     {
5         static int[] search = {1,2,3,4,5,6,7,8,9,10};
6         static void Main(string[] args)
7         {
8             int target = BinarySearch(7);
9             Console.WriteLine("第"+target+"个数");
10             Console.WriteLine(search[target]);
12         }
13
14         static int BinarySearch(int num)
15         {
16             int low = 0;
17             int high = search.Length-1;
18             while (high >= low)
19             {
20                 int middle = (low + high) / 2;
21                 if (search[middle] == num)
22                 {
23                     return middle;
24                 }
25                 else if (search[middle] > num)
26                 {
27                     high = middle-1;
28                 }
29                 else
30                 {
31                     low = middle + 1;
32                 }
33             }
34             return -1;
35         }
36     }
37 }

posted @ 2015-04-17 23:52  Sunnier  阅读(3590)  评论(0编辑  收藏  举报