Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace ConsoleApplication2
- {
- class Program
- {
- static void Main(string[] args)
- {
- int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9 };
- InsertionSort(arr);
- int temp = 0;
- for (int write = 0; write < arr.Length; write++)
- {
- for (int sort = 0; sort < arr.Length - 1; sort++)
- {
- if (arr[sort] > arr[sort + 1])
- {
- temp = arr[sort + 1];
- arr[sort + 1] = arr[sort];
- arr[sort] = temp;
- }
- }
- }
- for (int i = 0; i < arr.Length; i++)
- Console.Write(arr[i] + " ");
- Console.ReadKey();
- }
- public static 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; // 0
- //{ 800, 11, 50, 771, 649, 770, 240, 9 };
- while (j >= 0 && intArray[j] > temp)
- {
- intArray[j + 1] = intArray[j]; // 11
- 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.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement