算法大全源码

冒泡排序 bubble sorting

        int t;

          int[] a ={21,56,64,94,97,123};

          for(int j =a.Length-1;j>0;j--)

          {  for(int i =0;i<j;i++)

             { if(a[i]>a[i+1])

              { t =a[i];

                a[i]=a[i+1];

                a[i+1]=t;

              } } }

          for(int u =0;u<a.Length;u++)

         Console.WriteLine(a[u]);    结果:21,56,64,94,97,123

同时找最大最小

        int temp;                 

          int[] a={56,66,5,1230,87,95};

          for(int i =0;i<(a.Length+1)/2;i++)

          {  if(a[i]>a[a.Length-1-i])

             {  temp =a[i];

                 a[i] =a[a.Length-1-i];

                 a[a.Length-1-i] =temp;

             }

          }

          int max =a[a.Length-1],min =a[0];

          for(int i=0;i<(a.Length+1)/2;i++)

          {    if(min>a[i])

              min =a[i];

}

          for(int i =(a.Length+1)/2;i<a.Length;i++)

          {    if(max<a[i])

              max =a[i];

}

          Console.WriteLine("{0},{1}",min,max);   结果:5,1230

基数排序

        int[] a={1,5,9,7};

          int[] b=new int[10];

          for(int i=0;i<a.Length;i++)

             b[a[i]]=1;

          for(int j=0;j<b.Length;j++)

             if(b[j]==1)

                 Console.WriteLine(j);     结果:1,5,7,9

插入排序

        int[] r={12,2,6,65,42};

           for(int i=1;i<r.Length;i++)

          {  int t;

             t=r[i];

             int j;

             for(j=i-1;j>=0 && r[j]>t;j--)

             {}

             for(int k=i;k>j+1;k--)

                 r[k]=r[k-1];

             r[j+1]=t;        

          }

          for(int f=0;f<r.Length;f++)

             Console.WriteLine(r[f]);    结果:2,6,12,42,65

QuickSort   快速排序

        static void QuickSort(int[] a,int start,int end)

          {  int i=start,j=end;

             int pivot = a[i];

             while(i<j)

             {  while(i<j && pivot<=a[j])

                    j--;

                 a[i] = a[j];

                 while(i<j && a[i]<=pivot)

                    i++;

                 a[j]=a[i];

             }

             a[i] = pivot;

             if(i>start)

                 QuickSort(a,start,i);

             if(i<end)

                 QuickSort(a,i+1,end);

          }

      static void Main(string[] args)

      {  int[] x={87,56,5,13,5,12,};

          QuickSort(x,0,x.Length-1);

          for(int i=0;i<x.Length;i++)

             Console.WriteLine(x[i]);

       }    结果:5,5,12,13,56,87

MergeSort  归并排序

        static void MergeSort(int[] a,int s,int e)

          {  if(s>=e) return;

             MergeSort(a,s,(s+e)/2);

             MergeSort(a,(s+e)/2+1,e);

             Merge(a,s,(s+e)/2,e);

          }

      static void Merge(int[] a,int s,int mid, int e)

      {  int[] b=new int[a.Length];

          for(int w=0;w<a.Length;w++)

             b[w]=a[w];

          int i=s;

          int j=mid+1;

          int k=s;

          while(i<=mid && j<=e)

          {  if(b[i]<b[j])

                 a[k++] = b[i++];

             else

                 a[k++] = b[j++];

          }

          while(i<=mid)

             a[k++] = b[i++];

          while(j<=e)

             a[k++] = b[j++];

      }

      static void Main(string[] args)

      {  int[] a={34,2,5,66,87,99};

          MergeSort(a,0,a.Length-1);

          for(int i=0;i<a.Length;i++)

             Console.WriteLine(a[i]);

      }    结果:2,5,34,66,87,99

二叉查找树

// 二叉查找树节点 Binary search tree node

   public class BinarySearchTreeNode

   { public int key;// 二叉查找树节点的值

      public BinarySearchTreeNode left;// 二叉查找树节点的左子节点

      public BinarySearchTreeNode right;// 二叉查找树节点的右子节点

      /// 二叉查找树节点构造函数

      public BinarySearchTreeNode(int nodeValue)

      {  key = nodeValue;//nodeValue 节点的值

          left = null; right = null;

      }

      /// 插入节点

      public void InsertNode(BinarySearchTreeNode node)

      {  if(node.key > this.key)

          {  if(this.right == null)

             {  this.right = node;//node插入的节点

                 return;

             }

             else

                 this.right.InsertNode(node);

          }

          else

          {  if(this.left == null)

             {  this.left = node; return; }

             else

                 this.left.InsertNode(node);

          }

      }

      /// 二叉查找树查询

      public bool SearchKey(int searchValue)

      { if(this.key == searchValue)//searchValue需要查询的值

             return true;// 是否找到查询的值

          if(searchValue > this.key)

          {  if(this.right == null) return false;

             else

                 return this.right.SearchKey(searchValue);

          }

          else

          {  if(this.left == null)  return false;

             else

                 return this.left.SearchKey(searchValue);

          }

      }

      // 中序遍历

      public void MiddleDisplay()

      { if(this.left != null)

             this.left.MiddleDisplay();

          Console.WriteLine(this.key);

          if(this.right != null)

             this.right.MiddleDisplay();

      }

      // 前序遍历

      public void FrontDisplay()

      { Console.WriteLine(this.key);

          if(this.left != null)

             this.left.FrontDisplay();

          if(this.right != null)

             this.right.FrontDisplay();

      }

      // 后序遍历

      public void BehindDisplay()

      {  if(this.left != null)

             this.left.BehindDisplay();

          if(this.right != null)

             this.right.BehindDisplay();

          Console.WriteLine(this.key);

      }

   }

   /// 二叉查找树

   public class BinarySearchTree

   {  private BinarySearchTreeNode root;

      /// 生成一个二叉查找树

       public BinarySearchTree()

      {  root = nul; }

      /// 生成一个二叉查找树

      /// <param name="nodeValue">二叉查找树根节点的值</param>

      public BinarySearchTree(int nodeValue)

      { root = new BinarySearchTreeNode(nodeValue); }

      /// 在二叉查找树上插入一个节点

      /// <param name="nodeValue">插入节点的值</param>

      public void InsertBinarySearchTreeNode(int nodeValue)

      {   BinarySearchTreeNode insertNode = new BinarySearchTreeNode(nodeValue);

                    if(root == null)

          { root = insertNode;

             return;

          }

          else

             root.InsertNode(insertNode);

          return;

      }

      /// 在二叉查找树上查询一个数

      /// <param name="searchValue">需要查询的值</param>

      /// <returns>是否找到查询的值</returns>

      public bool SearchKey(int searchValue)

      { if(root.key == searchValue)  return true;

          else

             return root.SearchKey(searchValue);

      }

      /// 二叉查找树中序遍历

      public void MiddleDisplay()

      { root.MiddeleDisplay(); return; }

      /// 二叉查找树前序遍历

      public void FrontDisplay()

      { root.FrontDisplay(); return; }

      /// 二叉查找树后序遍历

      public void BehindDisplay()

      {  root.BehindDisplay(); return; }

      /// 二叉查找树排序

      /// <param name="a">需要排序的数组</param>

      public static void BinarySearchTreeSort(int [] a)

      { BinarySearchTree t = new BinarySearchTree();

          for(int i = 0; i < a.Length; i ++)

             t.InsertBinarySearchTreeNode(a[i]);

          t.MiddleDisplay();return;

      }/// 二叉查找树查找

      /// <param name="a">进行查找的数组</param>

      /// <param name="searchKey">需要查找的树</param>

      public static bool BinarySearchTreeSearch(int [] a, int searchKey)

      {  BinarySearchTree t = new BinarySearchTree();

          for(int i = 0; i < a.Length; i ++)

             t.InsertBinarySearchTreeNode(a[i]);

          return t.SearchKey(searchKey);

      }

   }

namespace 二叉树

{  class Node

   {  int n;

       public Node(int x)

       {  n=x; }

       public Node Left;

       public Node Right;

       public void Insert(Node node)

       {  if(node.n > this.n)

           {  if(this.Right == null)

                   this.Right = node;

               else

                   this.Right.Insert(node);  }

           else

           {  if(this.Left == null)

               { this.Left = node; }

               else

               { this.Left.Insert(node); }  }  }   //递归

       public void Show()

       {  Console.WriteLine(n); } }

   class BinaryTree

   {  Node root; 

       public void GenerateTree(Node node) //高内聚,低耦合

       {  if(root == null)

           {  root = node; return; }//如果树是空,第一次加节点

           root.Insert(node); 

   }

       public void ShowInOrder(Node node) //中序遍历(in order):左中右。先(前)序遍历(pre order):中左右。后序遍历(post order):左右中。

       {  if(node == null) return;//递归必须有个终止条件,递归方法中一定要接受参数

           ShowInOrder(node.Left);

           node.Show();

           ShowInOrder(node.Right);

       }

       public void Show()

       {  ShowInOrder(root); }

 }

   class A

   {  static void Main()

       {  BinaryTree b = new BinaryTree();

           Node node = new Node(5);

           b.GenerateTree(node);

           node = new Node(13);

           b.GenerateTree(node);

           node = new Node(6);

           b.GenerateTree(node);

           node = new Node(26);

           b.GenerateTree(node);

           node = new Node(7);

           b.GenerateTree(node);

           b.Show();  }  }  }   结果:5,6,7,13,26

单链表

   class Node

   { int a;

      public Node(int a) 

      {  this.a=a; }

      public int A      

      {get{return a;}  set{a=value;} }

      public Node next;

   }

   class LinkedList

   { Node header;

      public void Generate(int x)

      {  if(header==null)

             header=new Node(x);

          else

          {  Node n = new Node(x);

             if(n.A < header.A)

             {  n.next = header;

                 header=n;

                 return;

             }

             Node tmp=header;

             Node t=header;

             while(tmp.A < n.A)

             {  t=tmp; //为了下一次循环

                 tmp=tmp.next;

                 if(tmp==null)

                    break;

             }

             t.next=n;

             n.next=tmp;

          }

      }

      public void Out()

      {  Node tmp=header;

          while(tmp!=null)

          {  Console.WriteLine(tmp.A);

             tmp = tmp.next;

          } } }

   class Test

   {  static void Main()

      {  LinkedList ll = new LinkedList();

          ll.Generate(6);

          ll.Generate(36);

         ll.Generate(26);

          ll.Generate(16);

          ll.Out();

      }  }  }

反向链表

   class Link           //this class reverse the LinkedList

   { public int a;

      public Link next;

   }

   class createLink    //the class create the LinkedList

   {

      Link header=null;

      Link p = null;

      Link temp = null;

      Link l=null;      //Link k=null;

      Link g=null;

      public void create()

      { string str;

          int i;

          Console.WriteLine("Please enter number:");

          str=Console.ReadLine();

          while(str!="y")

          { i=Convert.ToInt32(str);

             temp=new Link();

             temp.a=i;

             temp.next=null;

               if(g==null)

                 g=temp;

             if(header==null)

                 header=temp;

             if(p==null)

                 p=temp;

             else

             { p.next=temp;

                 p=p.next;

             }

             Console.WriteLine("please enter number:");

             str=Console.ReadLine();

          }

      }

      public void display()

      {  while(header!=null)

          {  Console.WriteLine(header.a);

             header=header.next;

          }

      }

      public void reversed() // the mothod reversed the LinkedList

      { Link k=null;

          Link tmp=null;

          Link com =null;

          if(tmp==null)

             tmp=header.next;

          while(tmp!=null)

          {  //           if(com==null)

//            com=header;

             l=tmp;

             if(k==null)

             {  header.next=null;

                 k=header;

             }

             com=header;

             header=l;

             tmp=l.next;

             l.next=com;

          }

      }

      public void show()

      {  while(l!=null)

          {  Console.WriteLine(l.a);

             l=l.next;

          } } }

   class Tester

   {  static void Main()

      {  createLink cl=new createLink();

          cl.create();

          //cl.display();

          cl.reversed();

          cl.show();

      } } }

Stack 栈

   class Node

   { int a;

      public Node(int a)

      {  this.a=a; }

      public int A

      { get{return a;} set{a=value;} }

      public Node next;

   }

   class LinkedList

   { protected Node header;

      public void Generate(int x)

      {  if(header==null)

             header=new Node(x);

          else

          {  Node n = new Node(x);

             n.next=header;

             header=n;

          }

      }

      public void Out()

      {  Node tmp=header;

          while(tmp!=null)

          {  Console.WriteLine(tmp.A);

             tmp = tmp.next;

          } } }

   class Stack : LinkedList

   {  public void Push(int x)

      {  this.Generate(x); }

      public int Pop()

      { if(this.header == null)

             return -1; // empty stack

          int n = header.A;

          header = header.next;

          return n;

      }

   }

   class Test

   {  static void Main()

      {  Stack ss = new Stack();

          ss.Push(7);

          ss.Push(78);

          ss.Push(9);

          ss.Push(2);

          int i = ss.Pop();

          while(i != -1)

          {  Console.WriteLine(i);

             i = ss.Pop();

          } } } }

 

C#基础

ArrayList

        ArrayList al;

          al =new ArrayList();

          string s = Console.ReadLine();

          while(s !="q")

          { al.Add(s);

             s = Console.ReadLine();

          }

          for(int i=0;i<al.Count;i++)

          Console.WriteLine(al[i].ToString());

从键盘输入一个数组,并求最大

           int[] a;

             Console.WriteLine("Pls input len:");

             int len = Convert.ToInt32(Console.ReadLine());

             a = new int[len];

             Console.WriteLine("Pls input array");

             for(int i=0;i<len;i++)

             {  a[i]=Convert.ToInt32(Console.ReadLine()); }

             for(int j=0;j<a.Length;j++)

                 Console.WriteLine(a[j]);

             for(int k=0;k<a.Length;k++)

             {  if(a[0]<a[k])

                a[0]=a[k];

               }  Console.WriteLine(a[0]);

递归

          int fac (int n) 

          {  if(n==0) return 1;

             return n*fac(n-1);

          }

斐波那契数列第n项

          int fib (int n) 

          {  if(n==1) return 1;

             if(n==2) return 1;

             return fib(n-1)+fib(n-2);

          }

递归求1加到100

          public int Fac(int n)

          {  if(n==1) return 1;

             return n+Fac(n-1);

          }  

      static void Main()

      {  math f=new math();

          int s=f.Fac(100);

          Console.WriteLine(s);

      }

求前24项斐波那契序列数

          static int fib(int n)

          {  if(n==1) return 1;

             if(n==2) return 1;

             return fib(n-1)+fib(n-2);

          }

         static void Main(string[] args)

         {  int t=0;

             for(int i=1;i<25;i++)

          {  t=fib(i);

             Console.WriteLine(t);

          } }

汉诺塔

          static void Hanoi(string X, string Y,string Z,int n)

          {  if(n==1)

             {  Console.WriteLine(X + "-->" +Z);

                  return;

             }

             Hanoi (X,Z,Y,n-1);

             Console.WriteLine(X+"-->"+Z);三

             Hanoi (Y,X,Z,n-1);

          }

      static void Main(string[] args)

      { Hanoi("A","B","C",5); }

3位的水仙花数

          for(int i=100;i<1000;i++)

          {  int a=i/100; int c=i; int b=((i-c)/10);

             if(i==a*a*a+b*b*b+c*c*c)

                 Console.WriteLine(i);

          }

StringBuilder

             class A

             {  static void Main()

                 {  StringBuilder a=new StringBuilder();

                    a.Append ("asa");

                    a.Append ("www");

                    string s=a.ToString();

                    Console.WriteLine(s);

                }

             }      结果:asawww

singleton设计模式

             class A

             {  private int v;

                 A(int x)

                 { v=x; }

                 public int V

                 { get{return v;}  }

                 static A c=null;

                 public static A fun(int n)

                 {  if(c==null)

                      c=new A(n);

                    return c;

                 }

             }

             class B

             {  static void Main(string[] args)

                 {  A c=A.fun(2);

                    Console.WriteLine(c.V);

                    c=A.fun(8);

                    Console.WriteLine(c.V);

                 }

             }      结果:2,2

 

参考文章:http://blog.sina.com.cn/s/blog_534b270f01014pfp.html

posted @ 2012-08-15 12:06  ecollab  阅读(475)  评论(0编辑  收藏  举报