Skip to content

Implement arrary sorting Using Insertion Sort

Sinu  Joseph

Sinu Joseph

Asked May 23, 2013, 6:03 PM EDT5 answers
Share
LinkedInX
1.Implement arrary sorting Using Insertion Sort

2. Accept the Names of ten candidates and sort them alphabetically

5 Answers

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



    Hope this helps!
    Don't forget to accept this as answer. Thanks.
    0
  • Sinu  Joseph
    0
    using System;

    class Program
    {
    static void Main(string[] args)
    {
    int [] array = new int [10] {100, 50, 20, 40, 10, 60, 80, 70, 90, 30};
    int array_size = 10;

    Console.WriteLine("The array before Insertion Sort is: ");
    for (int i = 0; i < array_size; i++){
    Console.WriteLine("array[" +i +"] = " +array[i]);
    }

    // Now we will use Insertion sort
    int temp, k;
    for (int i = 1; i < array_size; i++) {
    temp = array[i];
    k = i - 1;
    while (k >= 0 && array[k] > temp) {
    array[k + 1] = array[k];
    k--;
    }
    array[k + 1] = temp;
    }

    Console.WriteLine();
    Console.WriteLine("The array after Insertion Sort is: ");
    for (int i = 0; i < array_size; i++) {
    Console.WriteLine("array[" + i + "] = " + array[i]);
    }
    }
    }
    0
  • Sunny  Sharma
    0
    is that a problem, or a solution? Didn't get your point.
    0
  • Sunny  Sharma
    0
    I assume you've got the answer... if yes, then please accept this as answer.
    0
  • Sunny  Sharma
    0
    Hey sinu joseph... hope you're doing fine. Earlier I was informed that you've accepted the answer, later you unaccepted this. Do you find any problem with the solution? Please share. Thanks.
    0