Skip to content

Insertion sort using c#

Sinu  Joseph

Sinu Joseph

Asked May 25, 2013, 3:29 AM EDT1 answer
Share
LinkedInX
implement array sort using insertion sort

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Sunny  Sharma
    0
    Hi Sinu Joseph,

    Below is a sample of implementation of Insertion sort for sorting the given names  alphabetically. They could be more than ten also:

    List<string> Names = new List<string>() { "RAM", "SHYAM", "RAMESH", "MAHESH", "DINESH", "RAJU SHRIVASTAV" };
    for (int pass = 1; pass <= Names.Count - 1; pass++)
    {
       for (int i = 1; i <= Names.Count - 1; i++)
       {
             string temp = Names[i];
             for (int j = i - 1; j >= 0 && string.Compare(Names[j], temp, StringComparison.Ordinal) == 1; j--)
                 {
                     Names[j + 1] = Names[j];
                     Names[j] = temp;
                 }
       }
    }




    Please don't forget to accept this as answer if it helps!
    Thanks.
    0