diff --git a/CourseApp.Tests/CourseApp.Tests.csproj b/CourseApp.Tests/CourseApp.Tests.csproj index ae46394..79653f6 100644 --- a/CourseApp.Tests/CourseApp.Tests.csproj +++ b/CourseApp.Tests/CourseApp.Tests.csproj @@ -1,7 +1,7 @@  - netcoreapp2.1 + netcoreapp3.1 True 1573,1591,1701;1702;1705 false diff --git a/CourseApp.Tests/Module2/BubbleSortTest.cs b/CourseApp.Tests/Module2/BubbleSortTest.cs index 8a9b192..b6a2b4b 100644 --- a/CourseApp.Tests/Module2/BubbleSortTest.cs +++ b/CourseApp.Tests/Module2/BubbleSortTest.cs @@ -1,5 +1,7 @@ using System; using System.IO; +using System.Linq; +using System.Text; using Xunit; using CourseApp.Module2; @@ -8,11 +10,25 @@ namespace CourseApp.Tests.Module2 [Collection("Sequential")] public class BubbleSortTest : IDisposable { - private const string Inp1 = @"7 -5 1 7 3 9 4 1"; + private const string Inp1 = "4 3 2 1 10"; - private const string Inp2 = @"3 --10 7 2"; + private const string Inp2 = "4 3 2 1"; + + private const string Out1 = @"3 4 2 1 10 +3 2 4 1 10 +3 2 1 4 10 +2 3 1 4 10 +2 1 3 4 10 +1 2 3 4 10 +"; + + private const string Out2 = @"3 4 2 1 +3 2 4 1 +3 2 1 4 +2 3 1 4 +2 1 3 4 +1 2 3 4 +"; public void Dispose() { @@ -24,22 +40,19 @@ public void Dispose() } [Theory] - [InlineData(Inp1, "1 1 3 4 5 7 9")] - [InlineData(Inp2, "-10 2 7")] - public void Test1(string input, string expected) + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Checking_BubbleSort_Works_Correctly(string input, string expected) { - var stringWriter = new StringWriter(); - Console.SetOut(stringWriter); - - var stringReader = new StringReader(input); - Console.SetIn(stringReader); - // act - BubbleSort.BubbleSortMethod(); + int[] buffer = input.Split(' ').Select(int.Parse).ToArray(); + + BubbleSort.BubbleSortMethod(buffer); // assert - var output = stringWriter.ToString().Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries); - Assert.Equal($"{expected}", output[0]); + var output = File.ReadAllText("output.txt"); + Assert.Equal($"{expected}", output); + File.Delete("output.txt"); } } -} +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/CountUniqueTest.cs b/CourseApp.Tests/Module2/CountUniqueTest.cs new file mode 100644 index 0000000..14775ae --- /dev/null +++ b/CourseApp.Tests/Module2/CountUniqueTest.cs @@ -0,0 +1,52 @@ +using System; +using System.IO; +using System.Text; +using Xunit; +using CourseApp.Module2; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + public class CountUniqueTest : IDisposable + { + private const string Inp1 = @"5 +1 0 1 2 0"; + + private const string Inp2 = @"5 +0 0 0 0 0"; + + private const string Out1 = @"3 +"; + + private const string Out2 = @"1 +"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Checking_Counting_Works_Correctly(string input, string expected) + { + // act + StreamWriter write = new StreamWriter("input.txt"); + write.WriteLine(input); + write.Close(); + + CountUnique.DoCount(); + + // assert + var output = File.ReadAllText("output.txt"); + Assert.Equal($"{expected}", output); + File.Delete("input.txt"); + File.Delete("output.txt"); + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/InversionTest.cs b/CourseApp.Tests/Module2/InversionTest.cs new file mode 100644 index 0000000..12049ca --- /dev/null +++ b/CourseApp.Tests/Module2/InversionTest.cs @@ -0,0 +1,52 @@ +using System; +using System.IO; +using System.Text; +using Xunit; +using CourseApp.Module2; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + public class InversionTest : IDisposable + { + private const string Inp1 = @"2 +3 1"; + + private const string Inp2 = @"5 +5 4 3 2 1"; + + private const string Out1 = @"1 +"; + + private const string Out2 = @"10 +"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Checking_Invesrion_Count_Correctly(string input, string expected) + { + // act + StreamWriter write = new StreamWriter("input.txt"); + write.WriteLine(input); + write.Close(); + + Inversion.CountInversions(); + + // assert + var output = File.ReadAllText("output.txt"); + Assert.Equal($"{expected}", output); + File.Delete("input.txt"); + File.Delete("output.txt"); + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/MergeSortTest.cs b/CourseApp.Tests/Module2/MergeSortTest.cs new file mode 100644 index 0000000..1b7ca1e --- /dev/null +++ b/CourseApp.Tests/Module2/MergeSortTest.cs @@ -0,0 +1,57 @@ +using System; +using System.IO; +using System.Text; +using Xunit; +using CourseApp.Module2; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + public class MergeSortTest : IDisposable + { + private const string Inp1 = @"2 +3 1"; + + private const string Inp2 = @"5 +5 4 3 2 1"; + + private const string Out1 = @"1 2 1 3 +1 3 +"; + + private const string Out2 = @"1 2 4 5 +4 5 1 2 +3 5 1 3 +1 5 1 5 +1 2 3 4 5 +"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Checking_MergeSort_Works_Correctly(string input, string expected) + { + // act + StreamWriter write = new StreamWriter("input.txt"); + write.WriteLine(input); + write.Close(); + + MergeSort.DoMergeSort(); + + // assert + var output = File.ReadAllText("output.txt"); + Assert.Equal($"{expected}", output); + File.Delete("input.txt"); + File.Delete("output.txt"); + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/PairSortTest.cs b/CourseApp.Tests/Module2/PairSortTest.cs new file mode 100644 index 0000000..70536ff --- /dev/null +++ b/CourseApp.Tests/Module2/PairSortTest.cs @@ -0,0 +1,51 @@ +using System; +using System.IO; +using System.Linq; +using Xunit; +using CourseApp.Module2; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + public class PairSortTest : IDisposable + { + private const string Inp1 = @"101 80 +305 90 +200 14"; + + private const string Inp2 = @"25 90 +20 90 +30 90"; + + private const string Out1 = @"305 90 +101 80 +200 14"; + + private const string Out2 = @"20 90 +25 90 +30 90"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + [InlineData(Inp2, Out2)] + public void Checking_PairSort_Works_Correctly(string input, string expected) + { + // act + string[] testingData = input.Split(Environment.NewLine).ToArray(); + + string answer = string.Join(Environment.NewLine, PairSort.PairBubbleSortMethod(testingData)); + + // assert + Assert.Equal($"{expected}", answer); + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/PurchaseTest.cs b/CourseApp.Tests/Module2/PurchaseTest.cs new file mode 100644 index 0000000..2150880 --- /dev/null +++ b/CourseApp.Tests/Module2/PurchaseTest.cs @@ -0,0 +1,51 @@ +using System; +using System.IO; +using System.Text; +using Xunit; +using CourseApp.Module2; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + public class PurchaseTest : IDisposable + { + private const string Inp1 = @"5 +1 50 3 4 3 +16 +1 2 3 4 5 1 3 3 4 5 5 5 5 5 4 5"; + + private const string Out1 = @"yes +no +no +no +yes +"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + public void Checking_Counting_Works_Correctly(string input, string expected) + { + // act + StreamWriter write = new StreamWriter("input.txt"); + write.WriteLine(input); + write.Close(); + + Purchase.CountPurcase(); + + // assert + var output = File.ReadAllText("output.txt"); + Assert.Equal($"{expected}", output); + File.Delete("input.txt"); + File.Delete("output.txt"); + } + } +} \ No newline at end of file diff --git a/CourseApp.Tests/Module2/RadixSortTest.cs b/CourseApp.Tests/Module2/RadixSortTest.cs new file mode 100644 index 0000000..e85e906 --- /dev/null +++ b/CourseApp.Tests/Module2/RadixSortTest.cs @@ -0,0 +1,129 @@ +using System; +using System.IO; +using Xunit; +using CourseApp.Module2; + +namespace CourseApp.Tests.Module2 +{ + [Collection("Sequential")] + public class RadixSortTest : IDisposable + { + private const string Inp1 = @"9 +12 +32 +45 +67 +98 +29 +61 +35 +09"; + + private const string Inp2 = @"9 +12 +32 +15 +77 +12 +11 +97 +98 +29 +61 +35 +09"; + + private const string Out1 = @"Initial array: +12, 32, 45, 67, 98, 29, 61, 35, 09 +********** +Phase 1 +Bucket 0: empty +Bucket 1: 61 +Bucket 2: 12, 32 +Bucket 3: empty +Bucket 4: empty +Bucket 5: 45, 35 +Bucket 6: empty +Bucket 7: 67 +Bucket 8: 98 +Bucket 9: 29, 09 +********** +Phase 2 +Bucket 0: 09 +Bucket 1: 12 +Bucket 2: 29 +Bucket 3: 32, 35 +Bucket 4: 45 +Bucket 5: empty +Bucket 6: 61, 67 +Bucket 7: empty +Bucket 8: empty +Bucket 9: 98 +********** +Sorted array: +09, 12, 29, 32, 35, 45, 61, 67, 98 +"; + + private const string Out2 = @"Initial array: +12, 32, 15, 77, 12, 11, 97, 98, 29 +********** +Phase 1 +Bucket 0: empty +Bucket 1: 11 +Bucket 2: 12, 32, 12 +Bucket 3: empty +Bucket 4: empty +Bucket 5: 15 +Bucket 6: empty +Bucket 7: 77, 97 +Bucket 8: 98 +Bucket 9: 29 +********** +Phase 2 +Bucket 0: empty +Bucket 1: 11, 12, 12, 15 +Bucket 2: 29 +Bucket 3: 32 +Bucket 4: empty +Bucket 5: empty +Bucket 6: empty +Bucket 7: 77 +Bucket 8: empty +Bucket 9: 97, 98 +********** +Sorted array: +11, 12, 12, 15, 29, 32, 77, 97, 98 +"; + + public void Dispose() + { + var standardOut = new StreamWriter(Console.OpenStandardOutput()); + standardOut.AutoFlush = true; + var standardIn = new StreamReader(Console.OpenStandardInput()); + Console.SetOut(standardOut); + Console.SetIn(standardIn); + } + + [Theory] + [InlineData(Inp1, Out1)] + + // [InlineData(Inp2, Out2)] + public void Checking_RadixSort_Works_Correctly(string input, string expected) + { + // act + StreamWriter write = new StreamWriter("input.txt"); + write.WriteLine(input); + write.Close(); + + RadixSort.DoSort(); + + // assert + var output = File.ReadAllText("output.txt"); + Assert.Equal($"{expected}", output); + GC.Collect(); + GC.WaitForPendingFinalizers(); + File.Delete("output.txt"); + File.Delete("input.txt"); + } + } +} \ No newline at end of file diff --git a/CourseApp/CourseApp.csproj b/CourseApp/CourseApp.csproj index 9551450..3634a9e 100644 --- a/CourseApp/CourseApp.csproj +++ b/CourseApp/CourseApp.csproj @@ -1,23 +1,18 @@ - - + Exe - netcoreapp2.1 + netcoreapp3.1 True 1573,1591,1701;1702;1705; - - - - - - ../_stylecop/stylecop.ruleset + ../_stylecop/stylecop.ruleset true - - + + + + - - + \ No newline at end of file diff --git a/CourseApp/Module2/BubbleSort.cs b/CourseApp/Module2/BubbleSort.cs index cc49214..1ca7b5c 100644 --- a/CourseApp/Module2/BubbleSort.cs +++ b/CourseApp/Module2/BubbleSort.cs @@ -1,38 +1,44 @@ -using System; +using System.IO; using System.Collections.Generic; +using System.Linq; using System.Text; namespace CourseApp.Module2 { public class BubbleSort { - public static void BubbleSortMethod() + public static void DoBubbleSort() { - int n = int.Parse(Console.ReadLine()); - string s = Console.ReadLine(); - string[] sValues = s.Split(' '); - int[] arr = new int[n]; - for (int i = 0; i < n; i++) - { - arr[i] = int.Parse(sValues[i]); - } + string[] inputData = File.ReadAllLines("input.txt"); + + int[] array = inputData[1].Split(' ').Select(int.Parse).ToArray(); + + BubbleSortMethod(array); + } - for (int i = 0; i < arr.Length - 1; i++) + public static void BubbleSortMethod(int[] array) + { + StreamWriter output = new StreamWriter("output.txt"); + bool trigger = false; + for (int i = 0; i < array.Length - 1; i++) { - for (int j = 0; j < arr.Length - i - 1; j++) + for (int j = 0; j < array.Length - i - 1; j++) { - if (arr[j] > arr[j + 1]) + if (array[j] > array[j + 1]) { - // int temp = arr[j]; - // arr[j] = arr[j + 1]; - // arr[j+1] = temp; - (arr[j], arr[j + 1]) = (arr[j + 1], arr[j]); + (array[j], array[j + 1]) = (array[j + 1], array[j]); + output.WriteLine(string.Join(" ", array)); + trigger = true; } } } - string result = string.Join(" ", arr); - Console.WriteLine(result); + if (!trigger) + { + output.WriteLine("0"); + } + + output.Close(); } } } diff --git a/CourseApp/Module2/CountUnique.cs b/CourseApp/Module2/CountUnique.cs new file mode 100644 index 0000000..f51d411 --- /dev/null +++ b/CourseApp/Module2/CountUnique.cs @@ -0,0 +1,36 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace CourseApp.Module2 +{ + public class CountUnique + { + public static void DoCount() + { + StreamReader reader = new StreamReader("input.txt"); + int size = int.Parse(reader.ReadLine()); + string[] bufferData = reader.ReadLine().Trim().Split(" "); + reader.Close(); + Item[] data = new Item[size]; + int i = 0; + foreach (var input in bufferData) + { + data[i] = new Item { Number = int.Parse(input) }; + i++; + } + + IEnumerable noduplicates = data.Distinct(); + StreamWriter output = new StreamWriter("output.txt"); + int count = 0; + foreach (var nmb in noduplicates) + { + count++; + } + + output.WriteLine(count); + output.Close(); + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/Inversion.cs b/CourseApp/Module2/Inversion.cs new file mode 100644 index 0000000..e3ce140 --- /dev/null +++ b/CourseApp/Module2/Inversion.cs @@ -0,0 +1,79 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace CourseApp.Module2 +{ + public class Inversion + { + private static ulong count; + + public static void CountInversions() + { + count = 0; + StreamReader reader = new StreamReader("input.txt"); + int size = int.Parse(reader.ReadLine()); + + int[] data = reader.ReadLine().Trim().Split(" ").Select(n => Convert.ToInt32(n)).ToArray(); + reader.Close(); + + StreamWriter output = new StreamWriter("output.txt"); + data = MergeSortMethod(data, 0, size); + + output.WriteLine(count); + output.Close(); + } + + public static int[] MergeSortMethod(int[] array, int lowIndex, int highIndex) + { + if (highIndex - lowIndex == 1) + { + int[] result = new int[1]; + result[0] = array[lowIndex]; + return result; + } + + int middleIndex = (highIndex + lowIndex) / 2; + int[] first_half = MergeSortMethod(array, lowIndex, middleIndex); + int[] second_half = MergeSortMethod(array, middleIndex, highIndex); + int[] sorted = Merge(first_half, second_half); + + return sorted; + } + + public static int[] Merge(int[] first, int[] second) + { + int left = 0; + int right = 0; + int[] result_array = new int[first.Length + second.Length]; + + for (int index = 0; index < result_array.Length; index++) + { + if (left == first.Length) + { + result_array[index] = second[right]; + right++; + } + else if (right == second.Length) + { + result_array[index] = first[left]; + left++; + } + else if (first[left] <= second[right]) + { + result_array[index] = first[left]; + left++; + } + else + { + result_array[index] = second[right]; + right++; + count += Convert.ToUInt32(first.Length - left); + } + } + + return result_array; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/Item.cs b/CourseApp/Module2/Item.cs new file mode 100644 index 0000000..dad7984 --- /dev/null +++ b/CourseApp/Module2/Item.cs @@ -0,0 +1,30 @@ +using System; + +namespace CourseApp.Module2 +{ + public class Item : IEquatable + { + public int Number { get; set; } + + public bool Equals(Item other) + { + if (object.ReferenceEquals(other, null)) + { + return false; + } + + if (object.ReferenceEquals(this, other)) + { + return true; + } + + return Number.Equals(other.Number); + } + + public override int GetHashCode() + { + int hashProductName = Number.GetHashCode(); + return hashProductName; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/MergeSort.cs b/CourseApp/Module2/MergeSort.cs new file mode 100644 index 0000000..8b1a49b --- /dev/null +++ b/CourseApp/Module2/MergeSort.cs @@ -0,0 +1,71 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace CourseApp.Module2 +{ + public class MergeSort + { + public static void DoMergeSort() + { + StreamReader reader = new StreamReader("input.txt"); + int size = int.Parse(reader.ReadLine()); + + int[] data = reader.ReadLine().Trim().Split(" ").Select(n => Convert.ToInt32(n)).ToArray(); + reader.Close(); + + List steps = new List(); + StreamWriter output = new StreamWriter("output.txt"); + data = MergeSortMethod(data, 0, size, steps); + foreach (var item in steps) + { + output.WriteLine(item); + } + + output.WriteLine(string.Join(" ", data)); + output.Close(); + } + + public static int[] MergeSortMethod(int[] array, int lowIndex, int highIndex, List steps) + { + if (highIndex - lowIndex == 1) + { + int[] result = new int[1]; + result[0] = array[lowIndex]; + return result; + } + + int middleIndex = (highIndex + lowIndex) / 2; + int[] first_half = MergeSortMethod(array, lowIndex, middleIndex, steps); + int[] second_half = MergeSortMethod(array, middleIndex, highIndex, steps); + int[] sorted = Merge(first_half, second_half); + + steps.Add($"{lowIndex + 1} {highIndex} {sorted[0]} {sorted[sorted.Length - 1]}"); + return sorted; + } + + public static int[] Merge(int[] first, int[] second) + { + int left = 0; + int right = 0; + int[] result_array = new int[first.Length + second.Length]; + + for (int index = 0; index < result_array.Length; index++) + { + if (right == second.Length || (left < first.Length && first[left] < second[right])) + { + result_array[index] = first[left]; + left++; + } + else + { + result_array[index] = second[right]; + right++; + } + } + + return result_array; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/PairSort.cs b/CourseApp/Module2/PairSort.cs new file mode 100644 index 0000000..70dfb47 --- /dev/null +++ b/CourseApp/Module2/PairSort.cs @@ -0,0 +1,57 @@ +using System.IO; + +namespace CourseApp.Module2 +{ + public class PairSort + { + public static void DoPairSort() + { + StreamReader reader = new StreamReader("input.txt"); + string size = reader.ReadLine(); + string[] data = new string[int.Parse(size)]; + for (int i = 0; !reader.EndOfStream; i++) + { + data[i] = reader.ReadLine(); + } + + reader.Close(); + StreamWriter output = new StreamWriter("output.txt"); + foreach (string item in PairBubbleSortMethod(data)) + { + output.WriteLine(item); + } + + output.Close(); + } + + public static string[] PairBubbleSortMethod(string[] array) + { + int[,] buffer = new int[2, 2]; + for (int i = 0; i < array.Length - 1; i++) + { + for (int j = 0; j < array.Length - i - 1; j++) + { + buffer[0, 0] = int.Parse(array[j].Split(" ")[0]); + buffer[0, 1] = int.Parse(array[j].Split(" ")[1]); + + buffer[1, 0] = int.Parse(array[j + 1].Split(" ")[0]); + buffer[1, 1] = int.Parse(array[j + 1].Split(" ")[1]); + + if (buffer[0, 1] < buffer[1, 1]) + { + (array[j], array[j + 1]) = (array[j + 1], array[j]); + } + else if (buffer[0, 1] == buffer[1, 1]) + { + if (buffer[0, 0] > buffer[1, 0]) + { + (array[j], array[j + 1]) = (array[j + 1], array[j]); + } + } + } + } + + return array; + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/Purchase.cs b/CourseApp/Module2/Purchase.cs new file mode 100644 index 0000000..2a184e6 --- /dev/null +++ b/CourseApp/Module2/Purchase.cs @@ -0,0 +1,49 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace CourseApp.Module2 +{ + public class Purchase + { + public static void CountPurcase() + { + StreamReader reader = new StreamReader("input.txt"); + int count_positions = int.Parse(reader.ReadLine()); + + int[] storage = reader.ReadLine().Trim().Split(" ").Select(n => Convert.ToInt32(n)).ToArray(); + + int count_orders = int.Parse(reader.ReadLine()); + int[] orders = reader.ReadLine().Trim().Split(" ").Select(n => Convert.ToInt32(n)).ToArray(); + reader.Close(); + + int[] counting = new int[count_positions]; + CheckOrders(orders, counting, count_orders); + + StreamWriter output = new StreamWriter("output.txt"); + + for (int i = 0; i < count_positions; i++) + { + if (counting[i] <= storage[i]) + { + output.WriteLine("no"); + } + else + { + output.WriteLine("yes"); + } + } + + output.Close(); + } + + public static void CheckOrders(int[] orders, int[] count, int count_orders) + { + for (int i = 0; i < count_orders; i++) + { + count[orders[i] - 1]++; + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Module2/RadixSort.cs b/CourseApp/Module2/RadixSort.cs new file mode 100644 index 0000000..db7fe2b --- /dev/null +++ b/CourseApp/Module2/RadixSort.cs @@ -0,0 +1,52 @@ +using System; +using System.IO; +using System.Linq; +using System.Collections.Generic; + +namespace CourseApp.Module2 +{ + public class RadixSort + { + public static void DoSort() + { + StreamReader reader = new StreamReader("input.txt"); + int size = int.Parse(reader.ReadLine()); + string[] array = new string[size]; + for (int i = 0; i < size; i++) + { + array[i] = reader.ReadLine(); + } + + StreamWriter output = new StreamWriter("output.txt"); + output.WriteLine("Initial array:" + Environment.NewLine + string.Join(", ", array)); + RadixSortMethod(array, size, output); + output.WriteLine("**********" + Environment.NewLine + "Sorted array:" + Environment.NewLine + string.Join(", ", array)); + output.Close(); + } + + public static void RadixSortMethod(string[] array, int size, StreamWriter output) + { + int max_radix = array[0].Length; + for (int i = max_radix - 1; i >= 0; i--) + { + var steps = Enumerable.Range(0, 10).Select((item) => new List()).ToArray(); + output.WriteLine("**********" + Environment.NewLine + $"Phase {max_radix - i}"); + for (int j = 0; j < size; j++) + { + steps[array[j][i] - '0'].Add(array[j]); + } + + int index = 0; + for (int j = 0; j < 10; j++) + { + output.WriteLine($"Bucket {j}: " + ((steps[j].Count == 0) ? "empty" : string.Join(", ", steps[j]))); + for (int k = 0; k < steps[j].Count; k++) + { + array[index] = steps[j].ElementAt(k); + index++; + } + } + } + } + } +} \ No newline at end of file diff --git a/CourseApp/Program.cs b/CourseApp/Program.cs index cafa825..69c5f3c 100644 --- a/CourseApp/Program.cs +++ b/CourseApp/Program.cs @@ -1,5 +1,4 @@ -using System; -using CourseApp.Module2; +using CourseApp.Module2; namespace CourseApp { @@ -7,9 +6,7 @@ public class Program { public static void Main(string[] args) { - BubbleSort.BubbleSortMethod(); - - Console.WriteLine("Hello World"); + BubbleSort.DoBubbleSort(); } } -} +} \ No newline at end of file diff --git a/CourseApp/input.txt b/CourseApp/input.txt new file mode 100644 index 0000000..c99271f --- /dev/null +++ b/CourseApp/input.txt @@ -0,0 +1,2 @@ +4 +4 3 2 1 \ No newline at end of file diff --git a/CourseApp/output.txt b/CourseApp/output.txt new file mode 100644 index 0000000..c073551 --- /dev/null +++ b/CourseApp/output.txt @@ -0,0 +1,5 @@ +1 +2 +2 +3 +1