2 Replies - 11681 Views - Last Post: 10 June 2008 - 06:30 AM Rate Topic: -----

#1 crafty  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 10-May 08

insertion sort

Posted 05 June 2008 - 12:42 AM

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


Is This A Good Question/Topic? 1

Replies To: insertion sort

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: insertion sort

Posted 08 June 2008 - 07:51 AM

Instead of telling you if you did it correctly, Ill let you look through a completed insertion sort program
Was This Post Helpful? 0
  • +
  • -

#3 crafty  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 10-May 08

Re: insertion sort

Posted 10 June 2008 - 06:30 AM

Thank You.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1