1.Implement arrary sorting Using Insertion Sort
2. Accept the Names of ten candidates and sort them alphabetically
Implement arrary sorting Using Insertion Sort
5 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Sunny SharmaRank #223 · 11.1K pointsPosted May 23, 2013, 10:39 PM EDT
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.
Sinu Joseph87 pointsPosted May 25, 2013, 4:11 AM EDT
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]);
}
}
}
Sunny SharmaRank #223 · 11.1K pointsPosted May 25, 2013, 4:14 AM EDT
Sunny SharmaRank #223 · 11.1K pointsPosted May 25, 2013, 4:30 AM EDT
Sunny SharmaRank #223 · 11.1K pointsPosted May 26, 2013, 10:00 AM EDT