繁体   English   中英

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

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

假设,我有10000本书(完全不能说),每本书具有唯一的标识号范围0-45000(完全不能说)。 提供一本书的标识号后,我想找到将该书放在何处进行排序,例如:如果我已经对以下书籍B12,B65,B842,B4695(假设这些ID为字符串)进行了排序,而我的下一本书ID为B1021和我想在B842和B4695的书号之间打印书; 所以我希望使用插入排序算法来确定将下一本书放在排序后的书中的位置,但是我不知道确切的书存储空间,因此我希望使用字符串集合或C#列表,但是我做不到的想法。 我是C#编程的新手,请给我一些想法。

因此,您的思路是正确的,但您需要考虑一下您拥有的对象的类型: B123B234C456E456 这些ID的行为并不完全像字符串整数,而是具有自己的特定定义。 因此,创建自己的类(也许将其称为bookIdentity)对您有利。

您需要对自定义类bookIdentity进行处理,以重载<运算符.equals()方法 这样,您可以精确地定义带有标签B123B234C456E456 基本的C#原语或String / Integer类不会定义在您的情况下具有顺序的含义。 但是,一旦使用自定义类定义了此行为,就可以定义将对象组织为B123 < B234 < C456 < E456 然后,您可以轻松地使用想要的任何数据结构和算法,以自己喜欢的方式对对象进行排序和存储。

请让我知道,如果你有任何问题!

最后,我编写了代码,并喜欢与您分享。 并请对此代码发表一些评论@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