如何使用插入排序算法对String Collection进行排序

时间:2014-08-16 19:12:25

标签: list c#-4.0 collections arraylist insertion-sort

假设我有10000个(完全不能说)书籍,每个书都有唯一的身份编号范围0 - 45000(完全不能说)。通过提供书籍的身份编号,我想找到将该书放在哪里排序, 例如:如果我已经分类了以下书籍B12,B65,B842,B4695(假设这些Ids是字符串),我的下一本书ID是B1021,我想在B842和B4695的书籍ID之间打印这本书。 所以我希望使用插入排序算法来确定下一本书在排序书序列中的位置,但我不知道确切的书籍存储,所以我希望使用字符串集合或C#列表,但我不能好好理解它。我是C#编程的新手,请给我一些想法。

2 个答案:

答案 0 :(得分:2)

因此,您的思维方式正确,但您需要考虑您拥有的对象类型:B123B234C456,{{ 1}}。这些ID不像整数那样完全,而是具有自己的特定定义。因此,创建自己的类是有利的,也许称之为bookIdentity。

您与自定义类bookIdentity的关系是overload the < operator .equals() method。有了这个,您可以使用标签E456B123B234C456精确定义订单的含义。底层的C#原语或String / Integer类不能定义在您的情况下有顺序的含义。但是,一旦使用自定义类定义此行为,就可以定义将对象组织为E456&lt; B123&lt; B234&lt; C456。然后,您可以轻松地使用任何数据结构和算法,您希望以您喜欢的方式对对象进行排序和存储。

如果您有任何疑问,请与我们联系!

答案 1 :(得分:0)

最后我写了一个代码,我喜欢和你分享。请对此代码发表一些评论 @cartridgemeadow谢谢你的想法,这对我有很大帮助

class Program
{

    class Test {

        String s1;

        public String S1
        {
            get { return s1; }
            set { s1 = value; }
        }


        public Test(String s1) {
            this.s1 = s1;
            //this.s2 = s2;
        }
        public Test() { }

        public static bool operator <(Test t1, Test t2)
        {
            int a = int.Parse(t1.s1.Remove(0, 1));
            int b = int.Parse(t2.s1.Remove(0, 1));


            return a < b;
        }

        public static bool operator > (Test t1, Test t2)
        {
            int a = int.Parse(t1.s1.Remove(0, 1));
            int b = int.Parse(t2.s1.Remove(0, 1));


            return a > b;
        }
    }

    static void Main(string[] args)
    {

        Console.WriteLine("\nbefore sorted");
        List<Test> test = new List<Test>() { 
         new Test("B005"),
         new Test("B002"),
         new Test("B007"),
         new Test("B001"),
         new Test("B003"),
         new Test("B004"),
         new Test("B006"),
         new Test("B010"),
         new Test("B008")
        };



        foreach (var t in test) {
            Console.WriteLine(t.S1);
        }
        Console.WriteLine("\nsorting messages");
        insertionSort(test);
        Console.WriteLine("\nafter sorted");
        foreach (var t in test)
        {
            Console.WriteLine(t.S1);
        }
        Console.Read();
    }

    static void insertionSort(List<Test> test)
    {
        String key;
        int i=0;
        int j;
        String before;
        String after;

            for (j = 1; j < test.Count; j++)
            {
                key = test.ElementAt(j).S1;
                i = j - 1;
                while (i >= 0 && int.Parse(test.ElementAt(i).S1.Remove(0, 1)) > int.Parse(key.Remove(0, 1)))
                {
                    test.ElementAt(i + 1).S1 = test.ElementAt(i).S1;

                    i = i - 1;

                }
                test.ElementAt(i + 1).S1 = key;
                if (i == -1)
                {
                    Console.WriteLine(test.ElementAt(i + 1).S1 + " is the starting book, keep this in before " + test.ElementAt(i + 2).S1);
                }
                else if (i == j - 1)
                {
                    Console.WriteLine(test.ElementAt(i + 1).S1 + " is the last book, keep this in after  " + test.ElementAt(i).S1);
                }
                else {
                    Console.WriteLine(test.ElementAt(i + 1).S1 + " is between " + test.ElementAt(i).S1 + " and " + test.ElementAt(i+2).S1);
                }
            }
            if (test.Count == 1) {
                Console.WriteLine("This is the first book");
            } else if (j == i)
            {
                Console.WriteLine("This is the last book, keep this after " + test.ElementAt(i).S1);
            }


    }