Browse Source

second day

eksemum 3 years ago
parent
commit
08d4589619

+ 32 - 2
SORTER/MainWindow.xaml

@@ -5,6 +5,36 @@
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
         xmlns:local="clr-namespace:SORTER"
         xmlns:local="clr-namespace:SORTER"
         mc:Ignorable="d"
         mc:Ignorable="d"
-        Title="Sorter" Height="450" Width="800">
-    <Grid/>
+        Title="Sorter" Height="400" Width="400" ResizeMode="NoResize" Icon="/icon.png">
+    <Grid>
+        <Grid.ColumnDefinitions>
+            <ColumnDefinition/>
+            <ColumnDefinition/>
+        </Grid.ColumnDefinitions>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="1.5*"/>
+            <RowDefinition Height="1.5*"/>
+            <RowDefinition Height="2*"/>
+        </Grid.RowDefinitions>
+
+        <Button x:Name="SelectFile" Content="Выбрать файл..." Margin="30 50 50 5" Click="SelectFile_Click"/>
+        <Button x:Name="SortButton" Content="Отсортировать" Grid.ColumnSpan="2" Grid.Row="1" Margin="30 20 30 20" Click="SortButton_Click"/>
+
+        <Border Grid.Row="2" BorderBrush="Black" BorderThickness="1"  HorizontalAlignment="Center" Margin="30 12 18 26">
+            <StackPanel Margin="4 6 0 4">
+                <RadioButton x:Name="LinearButton" Content="Линейная" GroupName="sorters" Height="20" Width="160" IsChecked="True"/>
+                <RadioButton x:Name="minMaxButton" Content="Метод МинМакс" GroupName="sorters" Height="20" Width="160"/>
+                <RadioButton x:Name="BubbleButton" Content="Пузырьковая" GroupName="sorters" Height="20" Width="160"/>
+                <RadioButton x:Name="ShuttleButton" Content="Челночная" GroupName="sorters" Height="20" Width="160"/>
+                <RadioButton x:Name="InsertionButton" Content="Сортировка вставки" GroupName="sorters" Height="20" Width="160"/>
+            </StackPanel>
+        </Border>
+
+        <Border Grid.Row="2" Grid.Column="1" BorderBrush="Black" BorderThickness="1" HorizontalAlignment="Center" Margin="12 12 28 90">
+            <StackPanel Margin="4 4 0 0">
+                <RadioButton x:Name="Descending" Content="По убыванию" GroupName="Ending" Height="18" Width="160" IsChecked="True"/>
+                <RadioButton x:Name="Ascending" Content="По возрастанию" GroupName="Ending" Height="18" Width="160"/>
+            </StackPanel>
+        </Border>
+    </Grid>
 </Window>
 </Window>

+ 268 - 58
SORTER/MainWindow.xaml.cs

@@ -1,5 +1,7 @@
-using System;
+using Microsoft.Win32;
+using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
+using System.IO;
 using System.Linq;
 using System.Linq;
 using System.Text;
 using System.Text;
 using System.Threading.Tasks;
 using System.Threading.Tasks;
@@ -20,135 +22,343 @@ namespace SORTER
     /// </summary>
     /// </summary>
     public partial class MainWindow : Window
     public partial class MainWindow : Window
     {
     {
-        int[] array = new int[100] { 1, 5, 4, 3, 2, 6, 8, 7, 10, 9, 11, 15, 13, 12, 14, 18, 16, 17, 19, 23, 21, 20, 22, 30, 43, 53, 23, 26, 78, 28, 15, 90, 99, 22, 76, 55, 68, 23, 54, 43, 46, 41, 51, 59, 91, 5, 3, 16, 17, 29, 34, 36, 76, 88, 84, 43, 95, 93, 23, 67, 64, 34, 38, 32, 23, 26, 84, 89, 94, 97, 12, 3, 5, 8, 9, 54, 67, 76, 72, 12, 16, 17, 18, 20, 33, 1, 32, 86, 98, 32, 24, 27, 19, 87, 98, 43, 29, 98, 78, 82};
+        int[] array;
+        string path;
 
 
         public MainWindow()
         public MainWindow()
         {
         {
             InitializeComponent();
             InitializeComponent();
-            //linearSelectionSort();
+            //linearSelectionSort(false);
             //methodMinMax();
             //methodMinMax();
             //BubbleSort();
             //BubbleSort();
-            ShuttleSort();
+            //ShuttleSort();
+            //InsertionSort();
         }
         }
 
 
 
 
-        void linearSelectionSort()
+        void linearSelectionSort(bool? ordering)
         {
         {
             List<int> arraySorted = new List<int>();
             List<int> arraySorted = new List<int>();
+            int[] arrayCopy = new int[array.Length];
+
             int max = int.MinValue;
             int max = int.MinValue;
+            int min = int.MaxValue;
             int index = 0;
             int index = 0;
 
 
-            while (arraySorted.Count < 100)
+            for (int i = 0; i < arrayCopy.Length; ++i)
             {
             {
-                for (int i = 0; i < array.Length; ++i)
+                arrayCopy[i] = array[i];
+            }
+
+            if (ordering == false)
+            {
+                using (StreamWriter sw = new StreamWriter(path, false))
                 {
                 {
-                    if (max <= array[i] && array[i] != 100)
+                    while (arraySorted.Count < 100)
                     {
                     {
-                        max = array[i];
-                        index = i;
+                        for (int i = 0; i < arrayCopy.Length; ++i)
+                        {
+                            if (max <= arrayCopy[i] && arrayCopy[i] != 100)
+                            {
+                                max = arrayCopy[i];
+                                index = i;
+                            }
+                        }
+                        arrayCopy[index] = 100;
+                        arraySorted.Add(max);
+                        sw.Write(arraySorted[index] + " ");
+                        max = int.MinValue;
                     }
                     }
                 }
                 }
-                array[index] = 100;
-                arraySorted.Add(max);
-                max = int.MinValue;
             }
             }
-
-            string foo = "";
-
-            for (int i = 0; i < arraySorted.Count; ++i)
+            else
             {
             {
-                foo += arraySorted[i].ToString() + " ";
+                using (StreamWriter sw = new StreamWriter(path, false))
+                {
+                    while (arraySorted.Count < 100)
+                    {
+                        for (int i = 0; i < arrayCopy.Length; ++i)
+                        {
+                            if (min >= arrayCopy[i] && arrayCopy[i] != 100)
+                            {
+                                min = arrayCopy[i];
+                                index = i;
+                            }
+                        }
+                        arrayCopy[index] = 100;
+                        arraySorted.Add(min);
+                        sw.Write(arraySorted[min] + " ");
+                        min = int.MaxValue;
+                    }
+                }
             }
             }
 
 
-            MessageBox.Show(foo);
+            arraySorted.Clear();
         }
         }
 
 
 
 
-        void methodMinMax()
+        void methodMinMax(bool? ordering)
         {
         {
-
-            for (int i = 0; i < array.Length - 1; ++i)
+            if(ordering == false)
             {
             {
-                int max = i;
+                for (int i = 0; i < array.Length - 1; ++i)
+                {
+                    int max = i;
+
+                    for (int j = i + 1; j < array.Length; ++j)
+                    {
+                        if (array[j].CompareTo(array[max]) > 0)
+                        {
+                            max = j;
+                        }
+                    }
+
+                    int temp = array[i];
+                    array[i] = array[max];
+                    array[max] = temp;
+                }
 
 
-                for (int j = i + 1; j < array.Length; ++j)
+            }
+            else
+            {
+                for (int i = 0; i < array.Length - 1; ++i)
                 {
                 {
-                    if (array[j].CompareTo(array[max]) > 0)
+                    int max = i;
+
+                    for (int j = i + 1; j < array.Length; ++j)
                     {
                     {
-                        max = j;
+                        if (array[j].CompareTo(array[max]) < 0)
+                        {
+                            max = j;
+                        }
                     }
                     }
+
+                    int temp = array[i];
+                    array[i] = array[max];
+                    array[max] = temp;
                 }
                 }
 
 
-                int temp = array[i];
-                array[i] = array[max];
-                array[max] = temp;
             }
             }
 
 
 
 
-            string foo = "";
+            using (StreamWriter sw = new StreamWriter(path, false))
+            {
+                for (int i = 0; i < array.Length; ++i)
+                {
+                    sw.Write(array[i] + " ");
+                }
+            }
+        }
+
+
+        void BubbleSort(bool? ordering)
+        {
+            int temp;
 
 
-            for (int i = 0; i < array.Length; ++i)
+            if (ordering == false)
             {
             {
-                foo += array[i] + " ";
+                for (int i = 0; i < array.Length - 1; i++)
+                {
+                    for (int j = 0; j < array.Length - i - 1; j++)
+                    {
+                        if (array[j + 1] > array[j])
+                        {
+                            temp = array[j + 1];
+                            array[j + 1] = array[j];
+                            array[j] = temp;
+                        }
+                    }
+                }
+            }
+            else
+            {
+                for (int i = 0; i < array.Length - 1; i++)
+                {
+                    for (int j = 0; j < array.Length - i - 1; j++)
+                    {
+                        if (array[j + 1] < array[j])
+                        {
+                            temp = array[j + 1];
+                            array[j + 1] = array[j];
+                            array[j] = temp;
+                        }
+                    }
+                }
             }
             }
 
 
-            MessageBox.Show(foo);
+            using (StreamWriter sw = new StreamWriter(path, false))
+            {
+                for (int i = 0; i < array.Length; ++i)
+                {
+                    sw.Write(array[i] + " ");
+                }
+            }
         }
         }
 
 
 
 
-        void BubbleSort()
+        void ShuttleSort(bool? ordering)
         {
         {
 
 
-            int temp = 0;
-            for (int i = 0; i < array.Length - 1; i++)
+            int temp;
+
+            if(ordering == false)
+            {
+                for (int i = 1; i < array.Length; i++)
+                {
+                    if (array[i] < array[i - 1])
+                    {
+                        temp = array[i];
+                        array[i] = array[i - 1];
+                        array[i - 1] = temp;
+                        for (int j = i - 1; (j - 1) >= 0; j--)
+                        {
+                            if (array[j] < array[j - 1])
+                            {
+                                temp = array[j];
+                                array[j] = array[j - 1];
+                                array[j - 1] = temp;
+                            }
+                            else
+                            {
+                                break;
+                            }
+                        }
+                    }
+                }
+            }
+            else
             {
             {
-                for (int j = 0; j < array.Length - i - 1; j++)
+                for (int i = 1; i < array.Length; i++)
                 {
                 {
-                    if (array[j + 1] < array[j])
+                    if (array[i] > array[i - 1])
                     {
                     {
-                        temp = array[j + 1];
-                        array[j + 1] = array[j];
-                        array[j] = temp;
+                        temp = array[i];
+                        array[i] = array[i - 1];
+                        array[i - 1] = temp;
+                        for (int j = i - 1; (j - 1) >= 0; j--)
+                        {
+                            if (array[j] > array[j - 1])
+                            {
+                                temp = array[j];
+                                array[j] = array[j - 1];
+                                array[j - 1] = temp;
+                            }
+                            else
+                            {
+                                break;
+                            }
+                        }
                     }
                     }
                 }
                 }
             }
             }
 
 
-            string foo = "";
 
 
-            for (int i = 0; i < array.Length; ++i)
+            using (StreamWriter sw = new StreamWriter(path, false))
             {
             {
-                foo += array[i] + " ";
+                for (int i = 0; i < array.Length; ++i)
+                {
+                    sw.Write(array[i] + " ");
+                }
             }
             }
-
-            MessageBox.Show(foo);
         }
         }
 
 
 
 
-        void ShuttleSort()
+        void InsertionSort(bool? ordering)
         {
         {
 
 
-            int temp = 0;
-            for (int i = 0; i < array.Length - 1; i++)
+            int index, temp;
+
+            if (ordering == false)
             {
             {
-                for (int j = 0; j < array.Length - i - 1; j++)
+                for (int i = 0; i < array.Length; ++i)
                 {
                 {
-                    if (array[j + 1] < array[j])
+                    index = i;
+                    temp = array[i];
+                    for (int j = i + 1; j < array.Length; ++j)
                     {
                     {
-                        temp = array[j + 1];
-                        array[j + 1] = array[j];
-                        array[j] = temp;
+                        if (array[j] > temp)
+                        {
+                            index = j;
+                            temp = array[j];
+                        }
                     }
                     }
+                    array[index] = array[i];
+                    array[i] = temp;
+                }
+            }
+            else
+            {
+                for (int i = 0; i < array.Length; ++i)
+                {
+                    index = i;
+                    temp = array[i];
+                    for (int j = i + 1; j < array.Length; ++j)
+                    {
+                        if (array[j] < temp)
+                        {
+                            index = j;
+                            temp = array[j];
+                        }
+                    }
+                    array[index] = array[i];
+                    array[i] = temp;
                 }
                 }
             }
             }
 
 
-            string foo = "";
+            using (StreamWriter sw = new StreamWriter(path, false))
+            {
+                for (int i = 0; i < array.Length; ++i)
+                {
+                    sw.Write(array[i] + " ");
+                }
+            }
+        }
 
 
-            for (int i = 0; i < array.Length; ++i)
+        private void SortButton_Click(object sender, RoutedEventArgs e)
+        {
+            if (LinearButton.IsChecked == true)
+            {
+                linearSelectionSort(Ascending.IsChecked);
+            }
+            else if (minMaxButton.IsChecked == true)
+            {
+                methodMinMax(Ascending.IsChecked);
+            }
+            else if (BubbleButton.IsChecked == true)
+            {
+                BubbleSort(Ascending.IsChecked);
+            }
+            else if (ShuttleButton.IsChecked == true)
+            {
+                ShuttleSort(Ascending.IsChecked);
+            }
+            else if (InsertionButton.IsChecked == true)
             {
             {
-                foo += array[i] + " ";
+                InsertionSort(Ascending.IsChecked);
             }
             }
+        }
+
+        private void SelectFile_Click(object sender, RoutedEventArgs e)
+        {
+            OpenFileDialog OPF = new OpenFileDialog();
+            OPF.Filter = "Файлы txt|*.txt";
+            if (OPF.ShowDialog() == true)
+            {
+                path = OPF.FileName;
+
+                using (StreamReader sr = new StreamReader(OPF.FileName))
+                {
+                    string str = sr.ReadToEnd();
+                    string[] arrayStr = str.Split(" ");
 
 
-            MessageBox.Show(foo);
+                    array = new int[arrayStr.Length];
+
+                    for (int i = 0; i < arrayStr.Length; ++i)
+                    {
+                        array[i] = Convert.ToInt32(arrayStr[i]);
+                    }
+                }
+            }
         }
         }
     }
     }
 }
 }

+ 8 - 0
SORTER/SORTER.csproj

@@ -6,4 +6,12 @@
     <UseWPF>true</UseWPF>
     <UseWPF>true</UseWPF>
   </PropertyGroup>
   </PropertyGroup>
 
 
+  <ItemGroup>
+    <None Remove="icon.png" />
+  </ItemGroup>
+
+  <ItemGroup>
+    <Resource Include="icon.png" />
+  </ItemGroup>
+
 </Project>
 </Project>

BIN
SORTER/icon.png


+ 0 - 0
SORTER/массив — копия.txt


+ 1 - 0
SORTER/массив.txt

@@ -0,0 +1 @@
+1 5 4 3 2 6 8 7 10 9 11 15 13 12 14 18 16 17 19 23 21 20 22 30 43 53 23 26 78 28 15 90 99 22 76 55 68 23 54 43 46 41 51 59 91 5 3 16 17 29 34 36 76 88 84 43 95 93 23 67 64 34 38 32 23 26 84 89 94 97 12 3 5 8 9 54 67 76 72 12 16 17 18 20 33 1 32 86 98 32 24 27 19 87 98 43 29 98 78 82