namespace insertionsort
{
class Program
{
public void arrayAccept(int[] array, int n)
{
Console.WriteLine("\nEnter the numbers:");
for (int a = 0; a < n; a++)
array[a] = Convert.ToInt32(Console.ReadLine());
}
public void insertionSort(int[] array, int n)
{
for (int pass = 1; pass <= n - 1; pass++)
{
for (int i = 1; i <= n - 1; i++)
{
int temp = array[i];
for (int j = i - 1; j >= 0 && array[j] > temp; j--)
{
array[j + 1] = array[j];
array[j] = temp;
}
}
}
}
public void displayArray(int[] array, int n)
{
Console.WriteLine("\nThe sorted array elements are:");
for (int a = 0; a < n; a++)
Console.WriteLine(array[a]);
}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine("Enter the number of elements in the array:");
int num = Convert.ToInt32(Console.ReadLine());
int[] array = new int[num];
p.arrayAccept(array, num);
p.insertionSort(array, num);
p.displayArray(array, num);
Console.ReadLine();
}
}
}
I would like to know if I have implemented the logic for insertion sort correctly ?
This post has been edited by crafty: 05 June 2008 - 12:43 AM

New Topic/Question
Reply



MultiQuote




|