From 2d13544b26dba05fee37621628b50f286703aabf Mon Sep 17 00:00:00 2001 From: Divlo Date: Sat, 11 Sep 2021 18:51:27 +0200 Subject: [PATCH] feat(solutions): add `sorting-algorithms/cs/bubble-sort` --- .../solutions/cs/bubble-sort/README.md | 3 ++ .../solutions/cs/bubble-sort/Solution.cs | 40 +++++++++++++++++++ 2 files changed, 43 insertions(+) create mode 100644 challenges/sorting-algorithms/solutions/cs/bubble-sort/README.md create mode 100644 challenges/sorting-algorithms/solutions/cs/bubble-sort/Solution.cs diff --git a/challenges/sorting-algorithms/solutions/cs/bubble-sort/README.md b/challenges/sorting-algorithms/solutions/cs/bubble-sort/README.md new file mode 100644 index 0000000..42c6207 --- /dev/null +++ b/challenges/sorting-algorithms/solutions/cs/bubble-sort/README.md @@ -0,0 +1,3 @@ +# sorting-algorithms/cs/bubble-sort + +Created by [@Divlo](https://github.com/Divlo) on 11 September 2021. diff --git a/challenges/sorting-algorithms/solutions/cs/bubble-sort/Solution.cs b/challenges/sorting-algorithms/solutions/cs/bubble-sort/Solution.cs new file mode 100644 index 0000000..46e5970 --- /dev/null +++ b/challenges/sorting-algorithms/solutions/cs/bubble-sort/Solution.cs @@ -0,0 +1,40 @@ +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 = BubbleSort(numbers.ToArray()); + foreach (int number in result) + { + Console.WriteLine(number); + } + } + + public static int[] BubbleSort(int[] array) + { + for (int index1 = 0; index1 < array.Length; index1++) + { + for (int index2 = 0; index2 < array.Length - 1; index2++) + { + if (array[index2] > array[index2 + 1]) + { + int temporary = array[index2]; + array[index2] = array[index2 + 1]; + array[index2 + 1] = temporary; + } + } + } + return array; + } + } +}