Roledenez
Roledenez

Reputation: 761

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). 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; 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. I am very new to C# programming, please give me some ideas for this.

Upvotes: 0

Views: 320

Answers (2)

Roledenez
Roledenez

Reputation: 761

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

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);
            }


    }

Upvotes: 0

Devarsh Desai
Devarsh Desai

Reputation: 6112

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. These ID's don't behave exactly like strings or integers, rather they have their own certain definition. And for this reason, it would be advantageous to you to create your own class, maybe call it bookIdentity.

What you'll have to do with your custom class, bookIdentity, is to the overload the < operator and the .equals() method. With this you can precisely define what it means to have order with the labels B123, B234,C456,E456. The underlying C# primitives or String/Integer classes won't define what it means to have order in your situation. 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. 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!

Upvotes: 2

Related Questions