From 6638ed59aef4be8c9deb847815538bc599a7d31b Mon Sep 17 00:00:00 2001 From: Divlo Date: Sat, 11 Sep 2021 18:58:16 +0200 Subject: [PATCH] feat(solutions): add `sorting-algorithms/cs/insertion-sort` --- .../solutions/cs/insertion-sort/README.md | 3 ++ .../solutions/cs/insertion-sort/Solution.cs | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 challenges/sorting-algorithms/solutions/cs/insertion-sort/README.md create mode 100644 challenges/sorting-algorithms/solutions/cs/insertion-sort/Solution.cs diff --git a/challenges/sorting-algorithms/solutions/cs/insertion-sort/README.md b/challenges/sorting-algorithms/solutions/cs/insertion-sort/README.md new file mode 100644 index 0000000..7a9e67d --- /dev/null +++ b/challenges/sorting-algorithms/solutions/cs/insertion-sort/README.md @@ -0,0 +1,3 @@ +# sorting-algorithms/cs/insertion-sort + +Created by [@Divlo](https://github.com/Divlo) on 11 September 2021. diff --git a/challenges/sorting-algorithms/solutions/cs/insertion-sort/Solution.cs b/challenges/sorting-algorithms/solutions/cs/insertion-sort/Solution.cs new file mode 100644 index 0000000..6a195c5 --- /dev/null +++ b/challenges/sorting-algorithms/solutions/cs/insertion-sort/Solution.cs @@ -0,0 +1,39 @@ +using System; +using System.Collections.Generic; + +namespace Solution +{ + class Program + { + static void Main() + { + string line = Console.ReadLine(); + List numbers = new List(); + while ((line = Console.ReadLine()) != null) + { + numbers.Add(int.Parse(line)); + } + int[] result = InsertionSort(numbers.ToArray()); + foreach (int number in result) + { + Console.WriteLine(number); + } + } + + public static int[] InsertionSort(int[] array) + { + for (int index1 = 1; index1 < array.Length; index1++) + { + int index2 = index1; + while (index2 > 0 && array[index2 - 1] > array[index2]) + { + int temporary = array[index2]; + array[index2] = array[index2 - 1]; + array[index2 - 1] = temporary; + index2--; + } + } + return array; + } + } +}