惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
AI
AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
H
Help Net Security
Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
L
LINUX DO - 最新话题
A
Arctic Wolf
博客园_首页
S
Securelist
S
Secure Thoughts
Google DeepMind News
Google DeepMind News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Cyberwarzone
Cyberwarzone
小众软件
小众软件
T
Threatpost
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Blog — PlanetScale
Blog — PlanetScale
N
News and Events Feed by Topic
NISL@THU
NISL@THU
Forbes - Security
Forbes - Security
博客园 - 聂微东
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
H
Heimdal Security Blog
罗磊的独立博客
S
Security @ Cisco Blogs
B
Blog
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Hacker News
The Hacker News
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
I
Intezer
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
S
Schneier on Security
月光博客
月光博客
L
LINUX DO - 热门话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

博客园 - 小白天地

从今日起重新开张!!! D3D坐标系统和几何-DirectX Griaphic学习〔转载〕 重新开始我的blog内容! 关于Java栈与堆的思考[zz] MVC模式〔zz〕 google电话面试经历 面试英语_zt 指针和数组(C++)(zz) 某公司的一道机考题的解答[zz] 设计模式的点滴[zz] 30分钟学会STL [zz] STL实践指南[zz] PHP的HTML文字编辑器 [zz] C#数据库类(zz) asp.net遍历控件的实现[zz] - 小白天地 - 博客园 C/C++的泪水![zz] 程序员数据结构笔记[zz] - 小白天地 - 博客园 百度之星程序设计大赛[zz] Asp.net与SQL一起打包部署安装[zz]
C#相关算法(zz)
小白天地 · 2005-10-19 · via 博客园 - 小白天地

冒泡法:

Using directives

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[] 151361055992871234753347 };
            BubbleSorter sh 
= new BubbleSorter();
            sh.Sort(iArrary);
            
for (int m = 0; m < iArrary.Length; m++)
                Console.Write(
"{0}", iArrary[m]);
            Console.WriteLine();
        }

    }

}

选择排序法

Using directives

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[] 1536105592871234753347 };
            SelectionSorter ss 
= new SelectionSorter();
            ss.Sort(iArrary);
            
for (int m = 0; m < iArrary.Length; m++)
                Console.Write(
"{0}", iArrary[m]);
            Console.WriteLine();
        }

    }

}

插入排序法

Using directives

namespace InsertionSorter
{
    
public class InsetionSorter
    
{
        
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[] 113361055982871234753347 };
            InsertionSorter ii 
= new InsertionSorter();
            ii.Sort(iArrary);
            
for (int m = 0; m < iArrary.Length; m++)
                Console.Write(
"{0}", iArrary[m]);
            Console.WriteLine();
        }

    }

}

希尔排序法

Using directives

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[] 151361055992871234753347 };
            ShellSorter sh 
= new ShellSorter();
            sh.Sort(iArrary);
            
for (int m = 0; m < iArrary.Length; m++)
                Console.Write(
"{0}", iArrary[m]);
            Console.WriteLine();
        }

    }

}

以前空闲的时候用C#实现的路径规划算法,今日贴它出来,看大家有没有更好的实现方案。关于路径规划(最短路径)算法的背景知识,大家可以参考《C++算法--图算法》一书。
    该图算法描述的是这样的场景:图由节点和带有方向的边构成,每条边都有相应的权值,路径规划(最短路径)算法就是要找出从节点A到节点B的累积权值最小的路径。
    首先,我们可以将“有向边”抽象为Edge类:

    public class Edge
    {
        
public string StartNodeID ;
        
public string EndNodeID   ;
        
public double Weight      ; //权值,代价        
    }

    节点则抽象成Node类,一个节点上挂着以此节点作为起点的“出边”表。

       public class Node
    
{
        
private string iD ;
        
private ArrayList edgeList ;//Edge的集合--出边表

        
public Node(string id )
     
{
            
this.iD = id ;
            
this.edgeList = new ArrayList() ;
        }


        
#region property
        
public string ID
     
{
            
get
          
{
                
return this.iD ;
            }

        }


        
public ArrayList EdgeList
      
{
            
get
          
{
                
return this.edgeList ;
            }

        }

        
#endregion

    }

    

    在计算的过程中,我们需要记录到达每一个节点权值最小的路径,这个抽象可以用PassedPath类来表示:

    /// <summary>
    
/// PassedPath 用于缓存计算过程中的到达某个节点的权值最小的路径
    
/// </summary>
    public class PassedPath
    {
        
private string     curNodeID ;
        
private bool     beProcessed ;   //是否已被处理
        private double     weight ;        //累积的权值
        private ArrayList passedIDList ; //路径

        
public PassedPath(string ID)
        {
            
this.curNodeID = ID ;
            
this.weight    = double.MaxValue ;
            
this.passedIDList = new ArrayList() ;
            
this.beProcessed = false ;
        }
#region property
        
public bool BeProcessed
        {
            
get
            {
                
return this.beProcessed ;
            }
            
set
            {
                
this.beProcessed = value ;
            }
        }
public string CurNodeID
        {
            
get
            {
                
return this.curNodeID ;
            }
        }
public double Weight 
        {
            
get
            {
                
return this.weight ;
            }
            
set
            {
                
this.weight = value ;
            }
        }
public ArrayList PassedIDList
        {
            
get
            {
                
return this.passedIDList ;
            }
        }
        
#endregion
    }

    另外,还需要一个表PlanCourse来记录规划的中间结果,即它管理了每一个节点的PassedPath。


    
/// <summary>
    
/// PlanCourse 缓存从源节点到其它任一节点的最小权值路径=》路径表
    
/// </summary>
    public class PlanCourse
    {
        
private Hashtable htPassedPath ;    #region ctor
        
public PlanCourse(ArrayList nodeList ,string originID)
        {
            
this.htPassedPath = new Hashtable() ;

            Node originNode 

= null ;
            
foreach(Node node in nodeList)
            {
                
if(node.ID == originID)
                {
                    originNode 
= node ;
                }
                
else
                {
                    PassedPath pPath 
= new PassedPath(node.ID) ;
                    
this.htPassedPath.Add(node.ID ,pPath) ;
                }
            }
if(originNode == null
            {
                
throw new Exception("The origin node is not exist !") ;
            }        
    
            
this.InitializeWeight(originNode) ;
        }
private void InitializeWeight(Node originNode)
        {
            
if((originNode.EdgeList == null||(originNode.EdgeList.Count == 0))
            {
                
return ;
            }
foreach(Edge edge in originNode.EdgeList)
            {
                PassedPath pPath 
= this[edge.EndNodeID] ;
                
if(pPath == null)
                {
                    
continue ;
                }

                pPath.PassedIDList.Add(originNode.ID) ;
                pPath.Weight 

= edge.Weight ;
            }
        }
        
#endregionpublic PassedPath this[string nodeID]
        {
            
get
            {
                
return (PassedPath)this.htPassedPath[nodeID] ;
            }
        }
    }

    在所有的基础构建好后,路径规划算法就很容易实施了,该算法主要步骤如下:
(1)用一张表(PlanCourse)记录源点到任何其它一节点的最小权值,初始化这张表时,如果源点能直通某节点,则权值设为对应的边的权,否则设为double.MaxValue。
(2)选取没有被处理并且当前累积权值最小的节点TargetNode,用其边的可达性来更新到达其它节点的路径和权值(如果其它节点   经此节点后权值变小则更新,否则不更新),然后标记TargetNode为已处理。
(3)重复(2),直至所有的可达节点都被处理一遍。
(4)从PlanCourse表中获取目的点的PassedPath,即为结果。
    
    下面就来看上述步骤的实现,该实现被封装在RoutePlanner类中:

    /// <summary>
    
/// RoutePlanner 提供图算法中常用的路径规划功能。
    
/// 2005.09.06
    
/// </summary>
    public class RoutePlanner
    {
        
public RoutePlanner()
        {            
        }
#region Paln
        
//获取权值最小的路径
        public RoutePlanResult Paln(ArrayList nodeList ,string originID ,string destID)
        {
            PlanCourse planCourse 
= new PlanCourse(nodeList ,originID) ;

            Node curNode 

= this.GetMinWeightRudeNode(planCourse ,nodeList ,originID) ;#region 计算过程
            
while(curNode != null)
            {
                PassedPath curPath 
= planCourse[curNode.ID] ;
                
foreach(Edge edge in curNode.EdgeList)
                {
                    PassedPath targetPath 
= planCourse[edge.EndNodeID] ;
                    
double tempWeight = curPath.Weight + edge.Weight ;if(tempWeight < targetPath.Weight)
                    {
                        targetPath.Weight 
= tempWeight ;
                        targetPath.PassedIDList.Clear() ;
for(int i=0 ;i<curPath.PassedIDList.Count ;i++)
                        {
                            targetPath.PassedIDList.Add(curPath.PassedIDList[i].ToString()) ;
                        }

                        targetPath.PassedIDList.Add(curNode.ID) ;
                    }
                }

//标志为已处理
                planCourse[curNode.ID].BeProcessed = true ;
                
//获取下一个未处理节点
                curNode = this.GetMinWeightRudeNode(planCourse ,nodeList ,originID) ;
            }
            
#endregion
            
            
//表示规划结束
            return this.GetResult(planCourse ,destID) ;                
        }
        
#endregion#region private method
        
#region GetResult
        
//从PlanCourse表中取出目标节点的PassedPath,这个PassedPath即是规划结果
        private RoutePlanResult GetResult(PlanCourse planCourse ,string destID)
        {
            PassedPath pPath 
= planCourse[destID]  ;            if(pPath.Weight == int.MaxValue)
            {
                RoutePlanResult result1 
= new RoutePlanResult(null ,int.MaxValue) ;
                
return result1 ;
            }
            
            
string[] passedNodeIDs = new string[pPath.PassedIDList.Count] ;
            
for(int i=0 ;i<passedNodeIDs.Length ;i++)
            {
                passedNodeIDs[i] 
= pPath.PassedIDList[i].ToString() ;
            }
            RoutePlanResult result 
= new RoutePlanResult(passedNodeIDs ,pPath.Weight) ;return result ;            
        }
        
#endregion#region GetMinWeightRudeNode
        
//从PlanCourse取出一个当前累积权值最小,并且没有被处理过的节点
        private Node GetMinWeightRudeNode(PlanCourse planCourse ,ArrayList nodeList ,string originID)
        {
            
double weight = double.MaxValue ;
            Node destNode 
= null ;foreach(Node node in nodeList)
            {
                
if(node.ID == originID)
                {
                    
continue ;
                }

                PassedPath pPath 

= planCourse[node.ID] ;
                
if(pPath.BeProcessed)
                {
                    
continue ;
                }
if(pPath.Weight < weight)
                {
                    weight 
= pPath.Weight ;
                    destNode 
= node ;
                }
            }
return destNode ;
        }
        
#endregion
        
#endregion
    }