Skip to content
Snippets Groups Projects
Commit 6cd78c11 authored by Dmytro Bogatov's avatar Dmytro Bogatov :two_hearts:
Browse files

Cahnges overview:

Add InsertionSort.
parent fe87356a
Branches
No related tags found
No related merge requests found
......@@ -8,29 +8,29 @@ using NUnit.Framework;
namespace CodingInterview.Tests
{
[TestFixture]
public class Test
public class BubleSortTests
{
[Test]
public void TestCases()
{
Assert.AreEqual(
BubleSort.Sort(new int[] { 5, 2, 6, 8, 9 }),
new int[] { 2, 5, 6, 8, 9 }
new int[] { 2, 5, 6, 8, 9 },
BubleSort.Sort(new int[] { 5, 2, 6, 8, 9 })
);
Assert.AreEqual(
BubleSort.Sort(new int[] { 1 }),
new int[] { 1 }
new int[] { 1 },
BubleSort.Sort(new int[] { 1 })
);
Assert.AreEqual(
BubleSort.Sort(new int[] { }),
new int[] { }
new int[] { },
BubleSort.Sort(new int[] { })
);
Assert.AreEqual(
BubleSort.Sort(new int[] { 5, 6, 5 }),
new int[] { 5, 5, 6 }
new int[] { 5, 5, 6 },
BubleSort.Sort(new int[] { 5, 6, 5 })
);
}
}
......
......@@ -32,6 +32,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="BubleSortTests.cs" />
<Compile Include="InsertionSortTests.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
......
//
//
// This file - InsertionSortTests.cs was created by Dmytro Bogatov (dmytro@dbogatov.org)
// on 12/6/2016, 7:45 PM
using NUnit.Framework;
namespace CodingInterview.Tests
{
[TestFixture]
public class InsertionSortTests
{
[Test]
public void TestCases()
{
Assert.AreEqual(
new int[] { 2, 5, 6, 8, 9 },
InsertionSort.Sort(new int[] { 5, 2, 6, 8, 9 })
);
Assert.AreEqual(
new int[] { 1 },
InsertionSort.Sort(new int[] { 1 })
);
Assert.AreEqual(
new int[] { },
InsertionSort.Sort(new int[] { })
);
Assert.AreEqual(
new int[] { 5, 5, 6 },
InsertionSort.Sort(new int[] { 5, 6, 5 })
);
}
}
}
......@@ -32,6 +32,7 @@
<ItemGroup>
<Compile Include="BubbleSort.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="InsertionSort.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
</Project>
\ No newline at end of file
//
//
// This file - InsertionSort.cs was created by Dmytro Bogatov (dmytro@dbogatov.org)
// on 12/6/2016, 7:44 PM
namespace CodingInterview
{
public class InsertionSort
{
public static int[] Sort(int[] input)
{
if (input.Length < 2)
{
return input;
}
var output = new int[input.Length];
output[0] = input[0];
for (int i = 1; i < input.Length; i++)
{
var current = input[i];
var inserted = false;
for (int j = 0; j < i; j++)
{
if (current < output[j])
{
for (int k = i; k > j; k--)
{
output[k] = output[k - 1];
}
output[j] = current;
inserted = true;
break;
}
}
if (!inserted)
{
output[i] = current;
}
}
return output;
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment