Skip to content

Commit 6122f55

Browse files
authored
Merge pull request #2 from MinyazevR/FirstHomework.Sort
Homework 1. Bubble sort
2 parents 3f3d4c2 + 8b18f6f commit 6122f55

5 files changed

Lines changed: 148 additions & 0 deletions

File tree

Homework1/Sort/Sort.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.31410.357
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Sort", "Sort\Sort.csproj", "{B938A3FF-D987-49A6-9820-762C4B3ECA0B}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SortTest", "SortTest\SortTest.csproj", "{2DAFE3CA-CA70-4E73-97BE-2A022208F24B}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{B938A3FF-D987-49A6-9820-762C4B3ECA0B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{B938A3FF-D987-49A6-9820-762C4B3ECA0B}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{B938A3FF-D987-49A6-9820-762C4B3ECA0B}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{B938A3FF-D987-49A6-9820-762C4B3ECA0B}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{2DAFE3CA-CA70-4E73-97BE-2A022208F24B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{2DAFE3CA-CA70-4E73-97BE-2A022208F24B}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{2DAFE3CA-CA70-4E73-97BE-2A022208F24B}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{2DAFE3CA-CA70-4E73-97BE-2A022208F24B}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {9AB1C4C4-EC8F-4BED-AB6C-2BA29F7399C9}
30+
EndGlobalSection
31+
EndGlobal

Homework1/Sort/Sort/BubbleSort.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using System;
2+
3+
namespace Sort;
4+
public class BubbleSort
5+
{
6+
public static void BubbleSortArray(int[] arrayOfNumbers)
7+
{
8+
for (int i = 0; i < arrayOfNumbers.Length - 1; i++)
9+
{
10+
for (int j = arrayOfNumbers.Length - 1; j > i; j--)
11+
{
12+
if (arrayOfNumbers[j - 1] > arrayOfNumbers[j])
13+
{
14+
(arrayOfNumbers[j - 1], arrayOfNumbers[j]) = (arrayOfNumbers[j], arrayOfNumbers[j - 1]);
15+
}
16+
}
17+
}
18+
}
19+
public static void Main(string[] args)
20+
{
21+
Console.WriteLine("Введите количество элементов в массиве");
22+
var inputString = Console.ReadLine();
23+
int numberOfElementsInArray = int.Parse(inputString);
24+
var random = new Random();
25+
var arrayOfNumbers = new int[numberOfElementsInArray];
26+
for (int i = 0; i < arrayOfNumbers.Length; i++)
27+
{
28+
arrayOfNumbers[i] = random.Next(15);
29+
Console.Write($"{arrayOfNumbers[i]} ");
30+
}
31+
BubbleSortArray(arrayOfNumbers);
32+
Console.WriteLine();
33+
for (int i = 0; i < arrayOfNumbers.Length; i++)
34+
{
35+
Console.Write($"{arrayOfNumbers[i]} ");
36+
}
37+
}
38+
}

Homework1/Sort/Sort/Sort.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
</PropertyGroup>
7+
8+
</Project>
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using Sort;
3+
using System;
4+
namespace SortTest;
5+
6+
[TestClass]
7+
public class BubbleSortTest
8+
{
9+
private static void CheckSort(int[] arrayOfNumbers)
10+
{
11+
BubbleSort.BubbleSortArray(arrayOfNumbers);
12+
for (int i = 1; i < arrayOfNumbers.Length; i++)
13+
{
14+
if (arrayOfNumbers[i] < arrayOfNumbers[i - 1])
15+
{
16+
Assert.Fail();
17+
}
18+
}
19+
}
20+
21+
[TestMethod]
22+
public void CheckSortArrayOfEqualNumbers()
23+
{
24+
int[] arrayOfNumbers = new int[100];
25+
for (int i = 1; i < arrayOfNumbers.Length; i++)
26+
{
27+
arrayOfNumbers[i] = 12;
28+
}
29+
BubbleSort.BubbleSortArray(arrayOfNumbers);
30+
CheckSort(arrayOfNumbers);
31+
}
32+
33+
[TestMethod]
34+
public void CheckSortOfSortedArray()
35+
{
36+
int[] arrayOfNumbers = new int[100];
37+
for (int i = 1; i < arrayOfNumbers.Length; i++)
38+
{
39+
arrayOfNumbers[i] = i;
40+
}
41+
CheckSort(arrayOfNumbers);
42+
}
43+
44+
[TestMethod]
45+
public void CheckSortOfEmptyArray()
46+
{
47+
int[] arrayOfNumbers = Array.Empty<int>();
48+
CheckSort(arrayOfNumbers);
49+
}
50+
}
51+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
6+
<IsPackable>false</IsPackable>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.9.4" />
11+
<PackageReference Include="MSTest.TestAdapter" Version="2.2.3" />
12+
<PackageReference Include="MSTest.TestFramework" Version="2.2.3" />
13+
<PackageReference Include="coverlet.collector" Version="3.0.2" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\Sort\Sort.csproj" />
18+
</ItemGroup>
19+
20+
</Project>

0 commit comments

Comments
 (0)