mirror of
https://github.com/theoludwig/programming-challenges.git
synced 2024-12-08 00:45:29 +01:00
feat(solutions): add sorting-algorithms/cs/bubble-sort
This commit is contained in:
parent
02bfe7454f
commit
2d13544b26
@ -0,0 +1,3 @@
|
||||
# sorting-algorithms/cs/bubble-sort
|
||||
|
||||
Created by [@Divlo](https://github.com/Divlo) on 11 September 2021.
|
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Solution
|
||||
{
|
||||
class Program
|
||||
{
|
||||
static void Main()
|
||||
{
|
||||
string line = Console.ReadLine();
|
||||
List<int> numbers = new List<int>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user