1.Implement arrary sorting Using Insertion Sort
2. Accept the Names of ten candidates and sort them alphabetically
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sunny SharmaPosted May 26, 2013, 2:00 PM
Sunny SharmaPosted May 25, 2013, 8:30 AM
Sunny SharmaPosted May 25, 2013, 8:14 AM
Sinu JosephPosted May 25, 2013, 8:11 AM
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 SharmaPosted May 24, 2013, 2:39 AM
Below is a sample of implementation of Insertion sort for sorting the given names alphabetically. They could be more than ten also:
List
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.