C#算法(一)冒泡排序


一。C#算法(一)冒泡排序

using System;

namespace BubbleSorter
{
 public class BubbleSorter
 {
  public void Sort(int [] list)
  {
   int i,j,temp;
   bool done=false;
   j=1;
   while((j<list.Length)&&(!done))
   {
    done=true;
    for(i=0;i<list.Length-j;i++)
    {
     if(list[i]>list[i+1])
     {
      done=false;
      temp=list[i];
      list[i]=list[i+1];
      list[i+1]=temp;
     }
    }
    j++;
   }

  }
 }
 public class MainClass
 {
  public static void Main()
  {
   int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
   BubbleSorter sh=new BubbleSorter();
   sh.Sort(iArrary);
   for(int m=0;m<iArrary.Length;m++)
    Console.Write("{0} ",iArrary[m]);
   Console.WriteLine();
  }
 }



二。C#算法(二)选择排序
using System;

namespace SelectionSorter
{
 public class SelectionSorter
 { 
  private int min;
  public void Sort(int [] list)
  {
   for(int i=0;i<list.Length-1;i++)
   {
    min=i;
    for(int j=i+1;j<list.Length;j++)
    {
     if(list[j]<list[min])
      min=j;
    }
    int t=list[min];
    list[min]=list[i];
    list[i]=t;
   }



  }
 }
 public class MainClass
 {
  public static void Main()
  {
   int[] iArrary=new int[]{1,5,3,6,10,55,9,2,87,12,34,75,33,47};
   SelectionSorter ss=new SelectionSorter();
   ss.Sort(iArrary);
   for(int m=0;m<iArrary.Length;m++)
    Console.Write("{0}  ",iArrary[m]);
   Console.WriteLine();
  }
 }
}
 

三。C#算法(三)插入排序
using System;

namespace InsertionSorter
{
 public class InsertionSorter
 {
  public void Sort(int [] list)
  {
   for(int i=1;i<list.Length;i++)
   {
    int t=list[i];
    int j=i;
    while((j>0)&&(list[j-1]>t))
    {
     list[j]=list[j-1];
     --j;
    }
    list[j]=t;
   }



  }
 }
 public class MainClass
 {
  public static void Main()
  {
   int[] iArrary=new int[]{1,13,3,6,10,55,98,2,87,12,34,75,33,47};
   InsertionSorter ii=new InsertionSorter();
   ii.Sort(iArrary);
   for(int m=0;m<iArrary.Length;m++)
    Console.Write("{0}",iArrary[m]);
   Console.WriteLine();
  }
 }
}
 

四。C#算法(四)希尔排序
using System;

namespace ShellSorter
{
 public class ShellSorter
 {
  public void Sort(int [] list)
  {
   int inc;
   for(inc=1;inc<=list.Length/9;inc=3*inc+1);
   for(;inc>0;inc/=3)
   {
    for(int i=inc+1;i<=list.Length;i+=inc)
    {
     int t=list[i-1];
     int j=i;
     while((j>inc)&&(list[j-inc-1]>t))
     {
      list[j-1]=list[j-inc-1];
      j-=inc;
     }
     list[j-1]=t;
    }
   }
  }
 }
 public class MainClass
 {
  public static void Main()
  {
   int[] iArrary=new int[]{1,5,13,6,10,55,99,2,87,12,34,75,33,47};
   ShellSorter sh=new ShellSorter();
   sh.Sort(iArrary);
   for(int m=0;m<iArrary.Length;m++)
    Console.Write("{0} ",iArrary[m]);
   Console.WriteLine();
  }
 }
}
 
五。穷举搜索法
求解“百钱买百鸡”,公鸡每只5钱,母鸡每只3钱,小鸡每3只1钱
分析:
      x+y+z=100
      5*x+3*y+z/3=100

  1.void main()
    {
       int x,y,z;
       for (x=0;x<=100;x++)
         for(y=0;y<=100;y++)
           for(z=0;z<=100;z++)
             if(x+y+z=100 &&  5*x+3*y+z/3=100)
               printf("%d%d%d/n",x,y,z)
  }

  2.void main()
    {
       int x,y,z;
       for (x=0;x<=20;x++)
         for(y=0;y<=33;y++)
          {  z=100-x-y;
             if(z%3=0 &&  5*x+3*y+z/3=100)
               printf("%d%d%d/n",x,y,z)
          }
  }


六。递归法
汉诺塔问题
(n阶汉诺塔问题)假设有三个分别命名为A,B和C的塔座,在塔座A上插有n个直径大小各不相同,按小到大编号为1,2,。。。,n的圆盘。现要求将A轴上的n个圆盘移至C上并按同样顺序叠排,圆盘移动时必须遵循下列规则:
   1)每次只能移动一个圆盘;
   2)圆盘可以插在A,B和C中的任一塔座上;
   3)任何时刻都不能将一个较大的圆盘压在较小的圆盘之上。


#include <studio.h>
void move(int n,char a,char c)
{
  static int step=1;
  printf("step %2d: disk %d %c-------> %c/n",step,n,a,c)
  step++;
}
void Hanoi(int n,char a,char b,char c)
{
  if(n>1)
   {
      Hanoi(n-1,a,c,b);   //先将前n-1个盘子从a通过c搬到b
      move(n,a,c);        //将第n个盘子从a搬到c
      Hanoi(n-1,b,a,c);  //再将这前n-1个盘子从b通过a搬到c
   }
  else
   {
     move(n,a,c);       //将这1个盘子从a搬到c
   } 
}

void main()
{Hanoi(3,'A','B','C');}

七。回溯法
求解迷宫问题。迷宫用一个二维数组表示,其元素值有两个:0和1,0表示通路,1表示阻塞,数组左上房[0][0]元素为迷宫入口,右下房[m-1][n-1]元素为迷宫的出口,现在寻找一条从入口到出口的通路(并不一定是最短的)。

#include <studio.h>
#define M 12
#define N 15
void maze(int p[][],int m,int n)
{
  int is[8]={0,1,1,1,0,-1,-1,-1};   //行前进方向:右,右下,下,左下,左,左上,上,右上
  int js[8]={1,1,0,-1,-1,-1,0,-1};  //列前进方向:右,右下,下,左下,左,左上,上,右上
  int stack[3*M*N],*top;
  int i,j,v,g,h,jt;
  top=stack;
  i=j=v=0;
  if(p[0][0]!=0)         //入口无路可走,提示无路,返回;
   {
    p[0][0]=4;printf("no path!/n";return;)
   }
   while(top!=stack || v!=7)
    {
       g=i+is[v];
       h=j+js[v];jt=0;
       if(g>-1 && g<m && h>-1 && h<n)
         { if(g= =m-1 && h= =n-1 && p[m-1][n-1]= =0)
             {
                p[i][j]=8;p[g][h]=8;
                return;                //找到路径,停止返回;
              }
             if (p[g][h]= =0)
             {
                p[g][h]=4;
                p[i][j]=8;             //前一步位置上置8
                top+=3;
                top[0]=i;top[1]=j;top[2]=v;  //入栈,记录下前一步
                i=g;j=h;v=0;jt=1;
             }
        }
      if(jt= =0)
      {
         if(v<7) v++;       //换一个方向
         else
           {
              while(top!stack && top[2]= =7)
              {
                 p[top[0]][top[1]]=4;  //无路,栈中元素置为4,并弹栈
                 top-=3;
               }
             if(top!=stack)
              {
                 i=top[0];j=top[1];v=top[2];   //回溯,弹栈
                 p[i][j]=4;top-=3;
              }
            }

      }
    }
printf("no path!/n");    //无路可走,提示无路,返回
return;
}

void main()
{
  int i,j;
  int p[M][N]={
                        {0,1,0,0,0,1,1,0,0,0,1,1,1,1,1} 
                        {1,0,0,0,1,1,0,1,1,1,0,0,1,1,1}                     
                        {0,1,1,0,0,0,0,1,1,1,1,0,0,1,1}  
                        {1,1,0,1,1,1,1,0,1,1,0,1,1,0,0}
                        {1,1,0,1,1,1,1,0,1,1,0,1,1,0,0}
                        {1,1,0,1,0,0,1,0,1,1,1,1,1,1,1}
                        {0,0,1,1,0,1,1,1,0,1,0,0,1,1,1} 
                        {0,1,1,1,1,0,0,1,1,1,1,1,1,1,1}                     
                        {0,0,1,1,0,1,1,0,1,1,1,1,1,0,1}  
                        {1,1,0,0,0,1,1,0,1,1,0,0,0,0,0}
                        {0,0,1,1,1,1,1,0,0,0,1,1,1,1,0}
                        {0,1,0,0,1,1,1,1,1,0,1,1,1,1,0}       
                    }
maze(p,12,15)

}

结果:

                        {8,1,0,0,0,1,1,4,4,4,1,1,1,1,1} 
                        {1,8,8,8,1,1,4,1,1,1,4,4,1,1,1}                     
                        {0,1,1,8,8,4,4,1,1,1,1,4,4,1,1}  
                        {1,1,8,1,1,1,1,4,1,1,4,1,1,4,4}
                        {1,1,8,1,1,1,1,4,1,1,4,1,1,4,4}
                        {1,1,8,1,0,0,1,4,1,1,1,1,1,1,1}
                        {0,8,1,1,1,1,1,1,4,1,0,0,1,1,1} 
                        {8,1,1,1,1,8,8,1,1,1,1,1,1,1,1}                     
                        {0,8,1,1,8,1,1,8,1,1,1,1,1,0,1}  
                        {1,1,8,8,8,1,1,8,1,1,8,8,8,8,8}
                        {0,0,1,1,1,1,1,0,8,8,1,1,1,1,8}
                        {0,1,0,0,1,1,1,1,1,4,1,1,1,1,8}    




二阶Fibonacci数列的递归算法 

 int f(int n)  
  {  
     if(n<0)  
        {return 0};  
     else
     {  
       if(n==1 || n==2)  
         {return 1; } 
       else  
        {return(f(n-2)+f(n-1));}
    }   
  }  
   
  void  main()  
  {  
  int i;  
  do  
  {  
     cout<<"enter   a   number:"<<endl;  
     cin>>i;  
     cout<<"Fibonacci数列:f("<<i<<")="<<f(i)<<endl;  
  }while(i>0);  
   
  }  



二阶Fibonacci数列的迭代算法

 int f(int n)
{
  if (n<0)
    return 0;
  else
    if(n==1 || n==2)
      return 1;
    else
      int a=1;
      int b=0;
      int s=0;
      for (int i=3;i<n;i++)
      {
        s=a+b;
        b=s;
        a=b;

      }
      return s
}  

   
  void  main()  
  {  
  int i;  
  do  
  {  
     cout<<"enter   a   number:"<<endl;  
     cin>>i;  
     cout<<"Fibonacci数列:f("<<i<<")="<<f(i)<<endl;  
  }while(i>0);  
   
  } 

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值