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; + } + } +}