.NET > Algorithm >> Algorithm in CSharp Views : 14451
Rate This Article :

Insertion Sort

      public void InsertionSort(int[] intArray)

        {

            Console.WriteLine("==========Integer Array Input===============");

            for (int i = 0; i < intArray.Length; i++)

            {

                Console.WriteLine(intArray[i]);

            }

 

            int temp, j;

            for (int i = 1; i < intArray.Length; i++)

            {

                temp = intArray[i];

                j = i - 1;

 

                while (j >= 0 && intArray[j] > temp)

                {

                    intArray[j + 1] = intArray[j];

                    j--;

                }

 

                intArray[j + 1] = temp;

            }

 

            Console.WriteLine("==========Integer Array OutPut===============");         

            for (int i = 0; i < intArray.Length; i++)

            {

                Console.WriteLine(intArray[i]);

            }

        }

Output: Pass Un-sorted Integer Array to the above Method and get the array Sorted.

Insertion_Sort_Output.png

About Author
Raj Kumar
Total Posts 24
Developer in .Net!
Comment this article
Name*
Email Address* (Will not be shown on this website.)
Comments*
Enter Image Text*
   
View All Comments
Jody
Way to use the intneert to help people solve problems!
  Privacy   Terms Of Use   Advertise With Us   Contact Us
© 2016 Developerin.Net. All rights reserved.
Trademarks and Article Images mentioned in this site may belongs to Microsoft and other respective trademark owners.
Articles, Tutorials and all other content offered here is for educational purpose only and its author copyrights.