1
1
mirror of https://github.com/theoludwig/programming-challenges.git synced 2024-12-08 00:45:29 +01:00

feat(solutions): add sorting-algorithms/cs/insertion-sort

This commit is contained in:
Divlo 2021-09-11 18:58:16 +02:00
parent 0d1dc719e1
commit 6638ed59ae
No known key found for this signature in database
GPG Key ID: 6F24DA54DA3967CF
2 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,3 @@
# sorting-algorithms/cs/insertion-sort
Created by [@Divlo](https://github.com/Divlo) on 11 September 2021.

View File

@ -0,0 +1,39 @@
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 = 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;
}
}
}