C#实现的基本算法

博客展示了用C#语言实现的多种排序算法,包括冒泡排序、选择排序、插入排序和希尔排序。给出了每种排序算法的代码示例,并对部分代码提出了实现多态的探讨,有助于C#学习者提升编程能力和理解算法。

using System

namespace BubbleSorter
{
     public class BubbleSorter
     {
          public void Sort
int [] list
          {
               int i,j,temp

               bool done=false

               j=1

               while
((jlist.Length&&!done))
               {
                     done=true

                     for
i=0ilist.Length-ji++
                     {
                          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=0miArrary.Lengthm++
          Console.Write
"{0} ",iArrary[m]);
          Console.WriteLine
();
     }
} 

}
 

  选择排序
  本人用了C#开发出选择排序算法。希望能为C#语言的学习者带来一些益处。不要忘了,学语言要花大力气学数据结构和算法。


using System

    

namespace SelectionSorter
{
     public class SelectionSorter
          {
               private int min

               public void Sort
int [] list
               {
                    for
int i=0ilist.Length-1i++
                    {
                         min=i

                         for
int j=i+1jlist.Lengthj++
                         {
                              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=0miArrary.Lengthm++
               Console.Write
"{0} ",iArrary[m]);
               Console.WriteLine
();


          }
     }
}
 

  插入排序

  插入排序算法。对想提高C#语言编程能力的朋友,我们可以互相探讨一下。如:下面的程序,并没有实现多态,来,帮它实现一下。


using System


namespace InsertionSorter
{
     public class InsertionSorter
     {
          public void Sort
int [] list
          {
               for
int i=1ilist.Lengthi++
               {
                    int t=list[i]

                    int j=i

                    while
((j0&&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=0miArrary.Lengthm++
               Console.Write
"{0}",iArrary[m]);
               Console.WriteLine
();
          }
     }
}
 

  希尔排序

  希尔排序是将组分段,进行插入排序. 对想提高C#语言编程能力的朋友,我们可以互相探讨一下。如:下面的程序,并没有实现多态,来,帮它实现一下。


using System


namespace ShellSorter
{
     public class ShellSorter
     {
          public void Sort
int [] list
          {
               int inc

               for
inc=1inc=list.Length/9inc=3*inc+1);
               for
(;inc0inc/=3
               {
                    for
int i=inc+1i=list.Lengthi+=inc
                    {
                         int t=list[i-1]

                         int j=i

                         while
((jinc&&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=0miArrary.Lengthm++
               Console.Write
"{0} ",iArrary[m]);
               Console.WriteLine
();
          }
     }
}
 

我现在才发现有些东西是永恒不变的,比如算法.

这个用C#实现的基本算法很不错,就是没有缩进,我排版了好久都没排好,郁闷ing

现在只好暂时这样了.应该还能看得清楚.

此为用C#写的A*算法源代码 using System; using System.Collections.Generic; using System.Text; using System.Drawing; namespace EtSoft.AStarLib { public class AStar { private StarNodeCollection openList = new StarNodeCollection(); private StarNodeCollection closeList = new StarNodeCollection(); public StarNode currentNode = null; #region 构造函数 /// <summary> /// 使用指定的地图对象、起点和终点初始化A星算法 /// </summary> /// <param name="map">地图对象</param> public AStar(Map map) { this.map = map; } /// <summary> /// /// </summary> /// <param name="map">地图对象</param> /// <param name="start">起点坐标</param> /// <param name="end">终点坐标</param> public AStar(Map map, Point start, Point end) : this(map) { this.start = new StarNode(start); this.end = new StarNode(end); openList.Add(new StarNode(start)); //AddStartNodeToOpenList(this.start); } /// <summary> /// /// </summary> /// <param name="map">地图对象</param> /// <param name="startX">起点X坐标</param> /// <param name="startY">起点Y坐标</param> /// <param name="endX">终点X坐标</param> /// <param name="endY">终点Y坐标</param> public AStar(Map map, int startX, int startY, int endX, int endY) : this(map, new Point(startX, startY), new Point(endX, endY)) { } #endregion #region 属性 protected Map map; /// <summary> /// 地图数据 /// </summary> public Map Map { get { return map; } set { map = value; } } private StarNode start = null; /// <summary> /// 起点坐标,如果没有设置起点,返回null /// </summary> public StarNode Start { get { return start; } set { start = value; openList.Clear(); openList.Add(start); //AddNodeToOpenList(start); } } private StarNode end = null; /// <summary> /// 终点坐标,如果没有设置终点,返回null /// </summary> public StarNode End { get { return end; } set { end = value; } } private StarNodeCollection path; /// <summary> /// 返回路径节点集合,没有路径则返回null /// </summary> public StarNodeCollection Path { get { return path; } } private bool allDirection = false; /// <summary> /// true,允许八方向寻路 /// </summary> public bool AllDirection { get { return allDirection; } set { allDirection = value; } } #endregion /// <summary> /// 开始寻路 /// </summary> public void StartSearchPath() { if (start == null) throw new InvalidNodeException(InvalidNodeTypes.NoStart); if (end == null) throw new InvalidNodeException(InvalidNodeTypes.NoEnd); path = null; openList.Clear(); closeList.Clear(); currentNode = start; SearchPath(currentNode); } //寻路递归,受保护的虚方法,允许在子类重写寻路算法 protected virtual void SearchPath(StarNode starNode) { //currentNode = starNode; openList.Remove(starNode); closeList.Add(starNode); AddNodeToOpenList(); if (closeList.Contains(end)) { //如果终点在关闭列表中,找到路径 StarNode node=starNode.Parent; path = new StarNodeCollection(); do { path.Add(node); node = node.Parent; } while (node != null && !node.Equals(start)); path.Reverse(); return; } else if (openList.Count == 0) { //终点不在关闭列表,开放列表已空,没有可通行的路径 return; } currentNode = GetNextNode(); //迭代过程 SearchPath(currentNode); } /// <summary> /// 获得当前节点的下一个最佳节点 /// </summary> /// <returns></returns> public StarNode GetNextNode() { openList.SortByF(); return openList[0]; } /// <summary> /// 把当前点周围可通过的点加入到开放列表中 /// </summary>
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值