常用数据结构及算法C#/Java实现
2015-04-17 23:52
891 查看
常用数据结构及算法C#实现
1.冒泡排序、选择排序、插入排序(三种简单非递归排序)int[] waitSort = { 1,0, 12, 13, 14, 5, 6, 7, 8, 9, 10 }; //冒泡排序 int length = waitSort.Length; for (int i = 0; i < length; i++) { for (int j = i + 1; j < length; j++) { if (waitSort[j] > waitSort[i]) { int temp = waitSort[j]; waitSort[j] = waitSort[i]; waitSort[i] = temp; } } } //选择排序 int pos1; for (int i = 0; i < length; i++) { pos1 = i; for (int j = i + 1; j < length; j++) { if (waitSort[pos1] < waitSort[j]) { pos1 = j; } } if (pos1 != i) { int temp = waitSort[pos1]; waitSort[pos1] = waitSort[i]; waitSort[i] = temp; } } //插入排序 for (int i = 1; i < length; i++) { for (int k = i; k > 0; k--) { if (waitSort[k] > waitSort[k - 1]) { int temp = waitSort[k]; waitSort[k] = waitSort[k - 1]; waitSort[k - 1] = temp; } } } foreach (var i in waitSort) { Console.WriteLine(i); } Console.ReadKey();
2.快速排序
C#版:
static int[] a = { 2, 5, 8, 6, 3, 4, 7, 9, 1,20,12,15,7,20,2 }; static void Main(string[] args) { QuickSort(0, a.Length - 1); foreach (var t in a) { Console.WriteLine(t); } Console.ReadKey(); } static void QuickSort(int low,int high) { if (low < high) { int partition=Partition(low,high); QuickSort(low, partition-1); QuickSort(partition+1, high); } } static int Partition(int low, int high) { int point = a[low]; while (low < high) { while (a[high] <= point && low<high) { high--; } Swap(high, low); while (a[low] >= point && low<high) { low++; } Swap(high, low); } return low; } static void Swap(int x, int y) { int temp = a[x]; a[x] = a[y]; a[y] = temp; }
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 ; num =temp; } }
3.二叉排序树
namespace BinarySortTree { class Node { public int Num { get; set; } public Node LChild { get; set; } public Node RChild { get; set; } } class BinarySortTree { public Node Root { get; set; } public BinarySortTree() { Root = new Node(); } } class Program { static void Main(string[] args) { int[] sort = { 2, 5, 8, 3, 9, 6, 1, 7, 4,2,2,2 }; BinarySortTree bst = new BinarySortTree(); bst.Root.Num = 2; for (int i = 1; i < sort.Length; i++) { InsertBst(bst.Root, sort[i]); } DFS(bst.Root); Console.ReadKey(); } static void InsertBst(Node parent,int num) { if (num <= parent.Num) { if (parent.LChild == null) { parent.LChild = new Node(); parent.LChild.Num = num; return; } else { InsertBst(parent.LChild, num); } } else { if (parent.RChild == null) { parent.RChild = new Node(); parent.RChild.Num = num; return; } else { InsertBst(parent.RChild, num); } } } static void DFS(Node parent) { if (parent.LChild != null) { DFS(parent.LChild); } Console.WriteLine(parent.Num); if (parent.RChild != null) { DFS(parent.RChild); } } } }
4.堆排
namespace HeapSort { class Program { static int[] a = new int[] { -1,1, 5, 9, 3, 7, 6, 4, 2, 8 }; static void Main(string[] args) { HeapSort(a.Length-1); } static void HeapSort(int len) { //第一次调整得到最小堆,即k>2k+1 && k>2k for (int i = len/2; i >= 1; i--) { Adjust(i, len); } //第二次先交换第一个节点和最后一个节点,使堆顶元素最小,然后调整 for (int i = len; i >= 2; i--) { Swap(1, i); Adjust(1, i-1); } } static void Swap(int m,int n) { int temp = a[m]; a[m] = a ; a = temp; } static void Adjust(int parent,int len) { int l = 2 * parent; int r = 2 * parent + 1; int largest = parent; //选出最大的节点,用于与父节点交换位置 if (l <=len && a[l] > a[largest]) { largest = l; } if (r<=len && a[r]>a[largest]) { largest = r; } //如果需要调整父节点,先交换然后调整交换节点与其孩子节点 if (largest != parent) { Swap(parent, largest); Adjust(largest, len); } } } }
5.栈的实现
namespace 栈 { public class MyStack { private int index = -1; private int[] a = new int[100]; public void Push(int num) { a[++index] = num; } public int? Pop() { if (index == -1) { return null; } return a[index--]; } } } namespace 栈 { class Program { static void Main(string[] args) { MyStack stack = new MyStack(); stack.Push(1); stack.Push(2); stack.Push(3); stack.Push(4); int? temp; while ((temp = stack.Pop()) != null) { Console.WriteLine(temp); } stack.Push(4); stack.Push(3); stack.Push(2); stack.Push(1); while ((temp = stack.Pop()) != null) { Console.WriteLine(temp); } Console.ReadKey(); } } }
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() { Head = new Node(); Head.Next = null; Length = 1; } public void Add(int num) { Node n = new Node(); n.Num = num; Node node = Head; while (node.Next != null) { node = node.Next; } node.Next = n; Length++; } public void Delete(int index) { Node n = Head; if (index == 0) { Head = n.Next; } 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 { Node n = Head; for (int i = 0; i < index; i++) { n = n.Next; } return n.Num; } set { Node n = Head; 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(); list.Head.Num = 1; list.Add(2); list.Add(3); list.Add(4); list.Add(5); list.Add(6); Console.WriteLine("链表长度:"+list.Length); Node n = list.Head; Console.WriteLine(n.Num); while (n.Next != null) { n = n.Next; Console.WriteLine(n.Num); } Console.ReadKey(); } } }
7.DFS(深搜)/BFS(宽搜)
private static void DFS(XmlNode parent) { if (!parent.HasChildNodes) { return; } else if (parent.HasChildNodes) { for (int j = 0; j < parent.ChildNodes.Count; j++) { if (parent.ChildNodes[j].Name == "span") { list.Add(parent.ChildNodes[j]); } DFS(parent.ChildNodes[j]); } } } public static void BFS(XmlNode root) { queue.Enqueue(root); while (queue.Count != 0) { XmlNode n = queue.Dequeue(); if (n.Name == "span") { list.Add(n); } for (int i = 0; i < n.ChildNodes.Count; i++) { queue.Enqueue(n.ChildNodes[i]); } } }
8.优先队列(堆实现)
namespace PriorityQueue { class Program { static List<int> list = new List<int>(); static void Main(string[] args) { list.Add(-1); EnQueue(1); EnQueue(3); EnQueue(2); EnQueue(4); EnQueue(5); EnQueue(7); Console.WriteLine(DeQueue()); EnQueue(10); EnQueue(8); EnQueue(15); int num = -1; while ((num = DeQueue()) != -1) { Console.WriteLine(num); } Console.ReadKey(); } static void EnQueue(int num) { list.Add(num); Adjust((list.Count-1)/2, list.Count-1); } static int DeQueue() { int len = list.Count-1; if (len == 0) { return -1; } Swap(1, len); int num = list[len]; list.RemoveAt(len); int len1 = list.Count - 1; Adjust(1, len1); return num; } static void Adjust(int parent, int len) { if (parent == 0) { return; } int l = 2 * parent; int r = 2 * parent + 1; int min = parent; //选出最大的节点,用于与父节点交换位置 if (l <= len && list[l] > list[min]) { min = l; } if (r <= len && list[r] > list[min]) { min = r; } //如果需要调整父节点,先交换然后调整父节点的父节点 if (min != parent) { Swap(parent, min); Adjust(parent/2, len); } } static void Swap(int m, int n) { int temp = list[m]; list[m] = list ; list = temp; } } }
9.归并排序
C#版:
namespace MergeSort { class Program { static int[] a = new int[] { 1,2,2,2, 3, 5, 7, 9, 2, 4, 8, 6 ,12,33,44,11,22,33}; static void Main(string[] args) { MergeSort(0, a.Length-1); } static void MergeSort(int first, int last) { if (first < last) { int mid = (first + last) / 2; MergeSort(first, mid); MergeSort(mid + 1, last); Merge(first, mid, last); } } static void Merge(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 (a[l] > a[r]) { temp[index++] = a[l++]; } else { temp[index++] = a[r++]; } } while (l <= mid) { temp[index++] = a[l++]; } while (r <= last) { temp[index++] = a[r++]; } index = 0; while (first <= last) { a[first++] = temp[index++]; } } } }
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.二分查找
namespace BinarySearch { class Program { static int[] search = {1,2,3,4,5,6,7,8,9,10}; static void Main(string[] args) { int target = BinarySearch(7); Console.WriteLine("第"+target+"个数"); Console.WriteLine(search[target]); Console.ReadKey(); } static int BinarySearch(int num) { int low = 0; int high = search.Length-1; while (high >= low) { int middle = (low + high) / 2; if (search[middle] == num) { return middle; } else if (search[middle] > num) { high = middle-1; } else { low = middle + 1; } } return -1; } } }
相关文章推荐
- 数据结构与算法—常用数据结构及其Java实现
- Java数据结构与算法---栈的实现
- Java数据结构与算法---链表的实现
- 数据结构与算法Java版——单链表的实现
- 【数据结构与算法】c# Dictionary内部实现
- 常用查找数据结构及算法(Python实现)
- Java实现数据统计的常用算法
- 数据结构与算法(4)---Java语言实现:队列的单链表定义
- 数据结构与算法(8)---Java语言实现:插入排序
- 常用查找数据结构及算法(Python实现)
- 【数据结构与算法】数组应用1:矩阵乘法(Java实现)
- 【数据结构与算法】数组应用2:矩阵转置(Java实现)
- 【数据结构与算法】数组应用3:稀疏矩阵压缩(Java实现)
- 【数据结构与算法】链表1:单向链表(Java实现)
- Java数据结构与算法—及实现 线性表 顺序表、链表、栈、队列详解
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- 数据结构之应用 "栈(Stack)" 实现: 解析算术表达式及计算求值 (C#/Java)
- 数据结构与算法(C#实现)系列---二叉树
- 数据结构与算法(C#实现)系列---AVLTree(二)(外摘)
- 数据结构与算法(C#实现)系列---演示篇(一)(外摘)