Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Insertion Sort
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { /* Insertion Sort This is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element which is to be 'insert'ed in this sorted sub-list, has to find its appropriate place and then it has to be inserted there. Hence the name, insertion sort. The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). This algorithm is not suitable for large data sets as its average and worst case complexity are of Ο(n2), where n is the number of items. Insertion sort is of order O(n2) */ int [] arr = getList(); Console.WriteLine("Unsorted array : "); Display(arr); InsertionSort(arr); Console.WriteLine(); Console.WriteLine("Sorted array : "); Display(arr); } static void InsertionSort(int[] arr) { int size = arr.Length; int indx, j; // Arry loop start from second element for (indx = 1; indx < size; indx++) { int element = arr[indx]; int ins = 0; // loop Sorted sub list for (j = indx - 1; j >= 0 && element < arr[j]; j-- ) { arr[j + 1] = arr[j]; arr[j] = element; } } } public static int[] getList(){ int [] nums = { 12, 19, 15, 8,4,6,3, 29,9,2, 22, 44, 20 }; return nums; } // Display the array static void Display(int []arr) { int size = arr.Length; for (int indx=0; indx < size; ++indx) Console.Write(arr[indx] + " "); Console.WriteLine(); } } }
run
|
edit
|
history
|
help
0
ProWIN2
hello
Chest Interaction Unity
Main5.1
*271*271#
MVC Learning
sxsadxasxd
Random number generator with IF
Reference Type parameter by Ref vs. Normal
2.1.1 Basic types: Lists and Linq - caveat