|
@@ -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]);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|