简体   繁体   English

如何使用插入排序算法对字符串集合进行排序

[英]How to sort String Collection using insertion sort algorithm

Suppose that, I have 10000(exactly cant say) books that each one have unique identity number range 0 - 45000(exactly cant say). 假设,我有10000本书(完全不能说),每本书具有唯一的标识号范围0-45000(完全不能说)。 With supplying identity number of a book,I want to find where to put that book to sort, eg: if I already sorted the following books B12, B65, B842, B4695 (suppose these Ids are Strings) and the my next book id is B1021 and I want to print the book in between book ids of B842 and B4695; 提供一本书的标识号后,我想找到将该书放在何处进行排序,例如:如果我已经对以下书籍B12,B65,B842,B4695(假设这些ID为字符串)进行了排序,而我的下一本书ID为B1021和我想在B842和B4695的书号之间打印书; so I hope to use insertion sort algorithm to determine where to put the next book in sorted book sequence, but I don't know the exact book storage, so I hope to use string collection or list of C#, but I can not get good idea for it. 所以我希望使用插入排序算法来确定将下一本书放在排序后的书中的位置,但是我不知道确切的书存储空间,因此我希望使用字符串集合或C#列表,但是我做不到的想法。 I am very new to C# programming, please give me some ideas for this. 我是C#编程的新手,请给我一些想法。

So you're on the right track with your thinking, but you need to think about the type of object you have: B123 , B234 , C456 , E456 . 因此,您的思路是正确的,但您需要考虑一下您拥有的对象的类型: B123B234C456E456 These ID's don't behave exactly like strings or integers, rather they have their own certain definition. 这些ID的行为并不完全像字符串整数,而是具有自己的特定定义。 And for this reason, it would be advantageous to you to create your own class, maybe call it bookIdentity. 因此,创建自己的类(也许将其称为bookIdentity)对您有利。

What you'll have to do with your custom class, bookIdentity, is to the overload the < operator and the .equals() method . 您需要对自定义类bookIdentity进行处理,以重载<运算符.equals()方法 With this you can precisely define what it means to have order with the labels B123 , B234 , C456 , E456 . 这样,您可以精确地定义带有标签B123B234C456E456 The underlying C# primitives or String/Integer classes won't define what it means to have order in your situation. 基本的C#原语或String / Integer类不会定义在您的情况下具有顺序的含义。 However once you define this behavior with your custom class, you can then define what it means to have objects organized as B123 < B234 < C456 < E456 . 但是,一旦使用自定义类定义了此行为,就可以定义将对象组织为B123 < B234 < C456 < E456 Then, you can then easily use any data structure and algorithm you would like to sort and store your objects in a fashion you prefer. 然后,您可以轻松地使用想要的任何数据结构和算法,以自己喜欢的方式对对象进行排序和存储。

Please let me know if you have any questions! 请让我知道,如果你有任何问题!

Finally I wrote a code and I like share with you. 最后,我编写了代码,并喜欢与您分享。 and please give some comment about this code @cartridgemeadow thank you for you ideas, it helps me a lot 并请对此代码发表一些评论@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);
            }


    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM