Загрузка данных
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection.Emit;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
namespace WinFormsApp16
{
public partial class Form1 : Form
{
private List<string> lines = new List<string>();
public Form1()
{
InitializeComponent();
comboBox1.Items.Clear();
comboBox1.Items.AddRange(new string[] { "Пузырёк", "Вставками", "Быстрая сортировка" });
if (comboBox1.Items.Count > 0) comboBox1.SelectedIndex = 0;
label1.Text = "Строк: 0";
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
if (lines.Count == 0) return;
string method = comboBox1.SelectedItem?.ToString();
System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew();
if (method == "Пузырёк") BubbleSort(lines);
else if (method == "Вставками") InsertionSort(lines);
else if (method == "Быстрая сортировка") QuickSort(lines, 0, lines.Count - 1);
sw.Stop();
UpdateUI();
label1.Text = $"Строк: {lines.Count} | Время: {sw.ElapsedMilliseconds} мс";
}
private void button3_Click(object sender, EventArgs e)
{
if (lines.Count == 0) return;
SaveFileDialog sfd = new SaveFileDialog { Filter = "Текстовые файлы|*.txt" };
if (sfd.ShowDialog() == DialogResult.OK)
{
File.WriteAllLines(sfd.FileName, lines);
MessageBox.Show("Файл сохранен!");
}
}
private void BubbleSort(List<string> list)
{
for (int i = 0; i < list.Count - 1; i++)
for (int j = 0; j < list.Count - i - 1; j++)
if (string.Compare(list[j], list[j + 1]) > 0)
{
string temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
private void UpdateUI()
{
listBox1.Items.Clear();
listBox1.Items.AddRange(lines.ToArray());
label1.Text = $"Строк: {lines.Count}";
}
private void InsertionSort(List<string> list)
{
for (int i = 1; i < list.Count; i++)
{
string key = list[i];
int j = i - 1;
while (j >= 0 && string.Compare(list[j], key) > 0)
{
list[j + 1] = list[j];
j--;
}
list[j + 1] = key;
}
}
private void QuickSort(List<string> list, int low, int high)
{
if (low < high)
{
int p = Partition(list, low, high);
QuickSort(list, low, p);
QuickSort(list, p + 1, high);
}
}
private int Partition(List<string> list, int low, int high)
{
string pivot = list[(low + high) / 2];
int i = low - 1;
int j = high + 1;
while (true)
{
do { i++; } while (string.Compare(list[i], pivot) < 0);
do { j--; } while (string.Compare(list[j], pivot) > 0);
if (i >= j) return j;
string temp = list[i];
list[i] = list[j];
list[j] = temp;
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { }
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog { Filter = "Текстовые файлы|*.txt" };
if (ofd.ShowDialog() == DialogResult.OK)
{
lines = File.ReadAllLines(ofd.FileName).ToList();
UpdateUI();
}
}
}
}