| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1710 人关注过本帖
标题:C#基数排序
只看楼主 加入收藏
hnzzc
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2006-4-26
收藏
 问题点数:0 回复次数:8 
C#基数排序
using System;
using System.Text;
using System.Collections;
using
using System.Data;

namespace a
{
        class sort
        {
        
            static void Main(string []args)
            {
                StreamReader sr = new StreamReader("d:\\abc.txt");//读文本文件
                string line = sr.ReadLine();  
                string info = "";
                int len=0;  //记录文件中记录关键字个数
           
                string []s1=line.Split(new char[]{','});
                if(line!=null)   //判断记录中关键字个数
                    foreach(string c in s1)
                        len++;
                List p=new List();
                while(line != null)
                {
                    info += line;
                    line = sr.ReadLine();//读入一条记录
                    string[] s = info.Split(new char[]{','});//文件中记录用","分开
                    p.Append(s,len);   //添加节点
                    info="";
                }
                p.print();    //这个地方调试时出错        }
        }


        public class ListNode     //节点类
        {
            public string[]keys;  //关键字数组
            public ListNode link;
            public static int Length;  //关键字个数

            public ListNode(string[]key,int len)   //构造函数,生成一个具有len个关键字的结点
            {
                int i=0;
                Length=len;
                keys=new string[len];
                foreach(string c in key)
                {
                    keys[i]=c;
                    i++;
                }
                link=null;
            }

            public void Print()
            {
                for(int i=0;i<Length;i++)
                    Console.Write(keys[i]);
            }

        }


        public class List    //链表
        {
            private ListNode Head;  //头结点
            private ListNode Tail;  //尾结点
            private ListNode Current; //当前结点
            private int index;     //结点个数

            public List()   
            {
                Head=null;
                Tail=null;
                index=0;
            }

            /// <summary>
            /// 尾部添加数据
            /// </summary>

            public void Append(string [] key,int n )  //传递一个长度为n的字符数组
            {
                ListNode NewNode=new ListNode(key,n );
                if (index==0)  //如果头指针为空
                {
                    Head=NewNode;
                    Tail=NewNode;
                }
                else
                {
                    Tail.link =NewNode;
                    Tail=NewNode;
                }
                Current=NewNode;
                index+=1;//链表数据个数加一
            }
        
            public void print()   //打印链表
            {
                ListNode p=Head;
                while(p!=null)
                {
                    int j=0;
                    Console.Write("第{}条记录:",j++);
                    p.Print();   //调用结点打印函数
                    Console.WriteLine();
                    p=p.link;
                }
            }


        }

    }



未处理的异常: System.FormatException: 输入字符串的格式不正确。
   at System.Text.StringBuilder.FormatError()
   at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String fo
rmat, Object[] args)
   at System.String.Format(IFormatProvider provider, String format, Object[] arg
s)
   at (String format, Object arg0)
   at (String format, Object arg0)
   at System.Console.Write(String format, Object arg0)
   at a.List.print()
   at a.sort.Main(String[] args)


[[it] 本帖最后由 hnzzc 于 2008-3-26 13:44 编辑 [/it]]
搜索更多相关主题的帖子: 基数排序 
2008-03-25 16:29
visolleon
Rank: 3Rank: 3
等 级:论坛游侠
威 望:1
帖 子:71
专家分:182
注 册:2007-1-16
收藏
得分:0 
这里好几处错误!
一个是文件行对象string line = sr.ReadLine(); 是否为空?
如果对象为空,Split()就会报错!
后面也是..
打印链表函数你怎么把变量初始化语句int j=0;写在循环里面?
Console.Write("第{}条记录:",j++);应该是Console.Write("第{0}条记录:",(j++).ToString());

2008-03-25 17:06
师妃暄
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:805
专家分:107
注 册:2006-3-1
收藏
得分:0 
Console.Write("第{}条记录:",j++);应该是Console.Write("第{0}条记录:",(j++).ToString());

楼上道破天机

有实力才会有魅力 实力来自坚持不懈的努力
2008-03-25 17:07
hnzzc
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2006-4-26
收藏
得分:0 
谢谢了,问题解决,麻烦帮忙看下下面的代码.为什么调试时总是死循环.
下面是基数排序
public void RadixSort(int d,int radix)   //基数排序
        {
            ListNode[]rear,front;
            rear=new ListNode[radix];//radix个队列的头指针
            front=new ListNode[radix];//radix个列列的尾指针
            ListNode current=Head;

            for(int i=d-1;i>=0;i--)   
            {
                while(current!=null)    //将n个对象分配到radix个队列中去
                {
                    int k=Convert.ToInt32(current.keys[i]);  //取当前检测对象的第i个关键码
                    [color=Red]if(front[k].link==null)      //第k个队列空,该对象成为队头
                        front[k]=current;   
                    else                    //不空,队尾结点链域指向它
                        rear[k].link=current;
                    rear[k]=current;        //该对象成为新的队尾
                    current=current.link ;
                }[/color]
                int j=0;        //依次从各个队列中把对象取出并拉链

                while(front[j].link==null) j++;   //找到第一个非空队列                Head=current=front[j];
                ListNode last=rear[j];
                for(int k=j+1;k<radix;k++)     //连接其余的队列
                 if(front[k]!=null)
                 {
                    last.link=front[k];
                    last=rear[k];
                 }
            }
        }



调试时错误: if(front[k].link==null)      //第k个队列空,该对象成为队头

错误代码: 输入字符串的格式不正确。


[[it] 本帖最后由 hnzzc 于 2008-3-25 21:47 编辑 [/it]]
2008-03-25 20:15
hnzzc
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2006-4-26
收藏
得分:0 
using System;
using System.Text;
using System.Collections;
using
using System.Data;

namespace a
{
        
    public class ListNode
    {
        public string[]keys;
        public ListNode link;
        public static int Length;
        public ListNode()
        {
            link=null;
            Length=0;
        }

        public ListNode(string[]key,int len)
        {
            int i=0;
            Length=len;
            keys=new string[len];
            foreach(string c in key)
            {
                keys[i]=c;
                i++;
            }
            link=null;

        }

    }


    public class List
    {
        private ListNode Head;
        private ListNode Tail;
        private ListNode Current;
        private int index;

        public List()
        {
            Head=null;
            Tail=null;
            index=0;
        }

        /// <summary>
        /// 尾部添加数据
        /// </summary>

        public void Append(string [] key,int n )
        {
            ListNode NewNode=new ListNode(key,n );
            if (index==0)  //如果头指针为空
            {
                Head=NewNode;
                Tail=NewNode;
            }
            else
            {
                Tail.link =NewNode;
                Tail=NewNode;
            }
            Current=NewNode;
            index+=1;//链表数据个数加一
        }
        
        public void print(int Len)
        {
            ListNode p=Head;
            int j=0;
            while(p!=null)
            {
                Console.Write("第{0}条记录  :",(j++).ToString());//Console.Write("第{}条记录:",j++);
                for(int i=0;i<Len;i++)
                    Console.Write("key[{0}]={1}  ",i.ToString(),p.keys[i]);
                Console.WriteLine();
                p=p.link;
            }
        }

        public void RadixSort(int d,int radix)   //基数排序
        {
            ListNode[]rear,front;
            rear=new ListNode[radix];
            front=new ListNode[radix];
            ListNode current=Head;

            for(int i=d-1;i>=0;i--)   
            {
                while(current!=null)    //将n个对象分配到radix个队列中去
                {
                    int k=Convert.ToInt32(current.keys[i]);  //取当前检测对象的第i个关键码
                    [color=Red]if(front[k].link==null)      //第k个队列空,该对象成为队头
                        front[k]=current;   
                    else                    //不空,队尾结点链域指向它
                        rear[k].link=current;
                    rear[k]=current;        //该对象成为新的队尾
                    current=current.link ;
                }[/color]
                int j=0;        //依次从各个队列中把对象取出并拉链

                while(front[j].link==null) j++;   //找到第一个非空队列
                Head=current=front[j];
                ListNode last=rear[j];
                for(int k=j+1;k<radix;k++)     //连接其余的队列
                 if(front[k].link!=null)
                 {
                    last.link=front[k];
                    last=rear[k];
                 }
            }
        }
    }


    class sort
    {
        
        static void Main(string []args)
        {
            StreamReader sr = new StreamReader("d:\\abc.txt");
            string line = sr.ReadLine();
            string info = "";
            int len=0;
                
            if(line!=null)
            {
                string []s1=line.Split(new char[]{','});
                foreach(string c in s1)
                    len++;
            }
            else
            {
                Console.WriteLine("文件为空");
                return;
            }

            List p=new List();
            while(line != null)
            {
                info += line;
                line = sr.ReadLine();
                string[] s = info.Split(new char[]{','});
                p.Append(s,len);
                info="";
            }
            Console.WriteLine("初始化链表:");
            p.print(len);
            p.RadixSort(len,10);
            Console.WriteLine("排序后的链表:");
            p.print(len);
        }
    }

}

我这个程序是基数排序,从文件中读入数据,每条记录有n个关键字.
红色部分应该有问题,麻烦各位看看

调试时错误: if(front[k].link==null)      //第k个队列空,该对象成为队头

错误代码: 输入字符串的格式不正确。


[[it] 本帖最后由 hnzzc 于 2008-3-25 21:46 编辑 [/it]]
2008-03-25 20:18
hnzzc
Rank: 1
等 级:新手上路
帖 子:86
专家分:0
注 册:2006-4-26
收藏
得分:0 
基数排序:
using System;
    using System.Text;
    using System.Collections;
    using
    using System.Data;

    namespace d
    {
    
        public class ListNode    //结点类
        {
            public string[]keys;   //关键字数组
            public ListNode link;  //指针
            public  int Length;    //关键字个数
            public ListNode()      //构造函数
            {
                link=null;
                keys=new string[10];
                Length=10;
                for(int i=0;i<10;i++)
                    keys[i]="";
            }

            public ListNode(string[]key,int len)  //带参数构造函数,构造一个带len个关键字的记录
            {
                int i=0;
                Length=len;
                keys=new string[len];
                foreach(string c in key)  
                {
                    keys[i]=c;
                    i++;
                }
                link=null;
            }

        }


        public class List   //链表类
        {
            private ListNode Head;   //头指针
            private ListNode Tail;   //尾指针
            private ListNode Current; //当前指针
            private int index;      
 
            public List()         //构造函数
            {
                Head=null;
                Tail=null;
                index=0;
            }

            /// <summary>
            /// 尾部添加数据
            /// </summary>

            public void Append(string [] key,int n )    //向链表插入一个新的结点
            {
                ListNode NewNode=new ListNode(key,n );
                if (index==0)  //如果头指针为空
                {
                    Head=NewNode;
                    Tail=NewNode;
                }
                else
                {
                    Tail.link =NewNode;
                    Tail=NewNode;
                }
                Current=NewNode;
                index+=1;//链表数据个数加一
            }
        
            public void print(int Len)         //打印函数
            {
                ListNode p=Head;
                int j=0;
                while(p!=null)
                {
                    Console.Write("第{0}条记录  :",(j++).ToString());
                    for(int i=0;i<Len;i++)
                        Console.Write("key[{0}]={1}  ",i.ToString(),p.keys[i]);
                    Console.WriteLine();
                    p=p.link;
                }
            }

            public  void RadixSort(int d,int radix)   //基数排序
            {
                ListNode[] front= new ListNode[radix]; //radix个队列的头指针
                ListNode []rear=new ListNode[radix];   //radix个队列的尾指针
                for(int  j=0;j<radix;j++)              //初始化
                {
                    front[j]=Tail.link  ;
                    rear[j]=Tail.link ;
                }

                ListNode current=Head;
         
                for(int i=d-1;i>=0;i--)   //将关键码keys[i]排序,i=d-1,d-2,..,0
                {
                    for(int  j2=0;j2<radix;j2++)      //将各箱初始化为空队列
                        front[j2]=null;
                    Console.WriteLine("第{0}趟排序:",i.ToString());   
                    while(current!=null)    //将n个对象分配到radix个队列中去
                    {
                        int k=Convert.ToInt32(current.keys[i]);  //取当前检测对象的第i个关键码
                        if(front[k]==null)      //第k个队列空,该对象成为队头
                            front[k]  =current;
                        else                    //不空,队尾结点链域指向它
                            rear[k].link=current;
                        rear[k] =current;        //该对象成为新的队尾
                        current=current.link ;
                    }

                    
                    int j=0;        //依次从各个队列中把对象取出并拉链

                    while(front[j] ==null) j++;   //找到第一个非空队列

                    Head=current=front[j];
                    ListNode last=rear[j];
                    for(int k=j+1;k<radix;k++)     //连接其余的队列
                        if(front[k] !=null)
                        {
                            last.link=front[k] ;
                            last=rear[k];
                            ListNode p1=front[k];
                        }
                    last.link=null;   //新链表表尾

                    for(int k=0;k<radix;k++)     //输出第d个关键排序后的链表
                        if(front[k] !=null)
                        {
                            ListNode p1=front[k];
                            Console.WriteLine("k={0}",k);
                            while(p1!=rear[k])
                                {
                                    for(int k1=0;k1<d;k1++)
                                        Console.Write("keys[{0}]={1};",k1,p1.keys[k1]);
                                    Console.WriteLine();
                                    p1=p1.link;
                                }
                            for(int k1=0;k1<d;k1++)
                                Console.Write("keys[{0}]={1};",k1,p1.keys[k1]);
                            Console.WriteLine();

                        }
                }
            }

            public void Write(int len)   //将含有len个关键码的记录写入文本文件中
            {
                ListNode p=Head;
                StreamWriter sW=new StreamWriter("d:\\ab.txt");
                int j=len-1;
                while(p!=null)
                {
                    string s="";
                    for(int i=0;i<j;i++)
                    {
                        s+=p.keys[i];
                        s+=",";
                    }
                    s+=p.keys[j];
                    sW.WriteLine(s);
                    p=p.link;
                }
                sW.Close();
            }
        }

        class sort
        {
        
            static void Main(string []args)
            {
                StreamReader sr = new StreamReader("d:\\abc.txt");
                string line = sr.ReadLine();
                string info = "";
                int len=0;
                
                if(line!=null)
                {
                    string []s1=line.Split(new char[]{','});
                    foreach(string c in s1)
                        len++;
                }
                else
                {
                    Console.WriteLine("文件为空");
                    return;
                }

                List p=new List();
                while(line != null)
                {
                    info += line;
                    line = sr.ReadLine();
                    string[] s = info.Split(new char[]{','});
                    p.Append(s,len);
                    info="";
                }
                Console.WriteLine("初始化链表:");
                p.print(len);
                p.RadixSort(len,10);
                Console.WriteLine("排序后的链表:");
                p.print(len);
                p.Write(len);
            }
        }

    }
2008-03-26 13:42
sdnd2000
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-4-10
收藏
得分:0 
这段程序能完成字母的排序吗
这段程序能完成字母的排序吗,比如有几个字母组成的字符串,需要排序,如:
ADFDSABNKDJDKHIGF
AKJDFIDSJIFS
KJDISFSA
BDJ
GBDFJ
排序后应该是
ADFDSABNKDJDKHIGF
AKJDFIDSJIFS
BDJ
GBDFJ
KJDISFSA
我试了下,好像不行,那这段程序只能排序数字吗?
2008-04-10 06:24
sdnd2000
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-4-10
收藏
得分:0 
顺便再问下,尾部添加数据是什么意思啊
2008-04-10 06:32
sdnd2000
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2008-4-10
收藏
得分:0 
请大家看看啊
2008-04-10 11:22
快速回复:C#基数排序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.041721 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved