Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class InsertionSort
- {
- static void Main()
- {
- InsertionSort();
- }
- static void InsertionSort()
- {
- Console.Write("\nEnter the total numbers: ");
- int max = int.Parse(Console.ReadLine());
- int[] array = new int[max];
- //Input numbers
- for (int i = 0; i < max; i++)
- {
- Console.Write("Enter [" + (i + 1).ToString() + "] element: ");
- array[i] = Convert.ToInt32(Console.ReadLine());
- }
- for (int i = 0; i < max; i++)
- {
- int j = i;
- while (j > 0)
- {
- if (array[j - 1] > array[j])
- {
- int temp = array[j - 1];
- array[j - 1] = array[j];
- array[j] = temp;
- }
- else
- {
- break;
- }
- }
- }
- PrintArray(array);
- }
- static void PrintArray(int[] array)
- {
- for (int i = 0; i < array.Length; i++)
- {
- Console.Write("\nSorted [" + (i + 1).ToString() + "] element: ");
- Console.WriteLine(array[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment