Загрузка данных
using System;
using System.Windows.Forms;
namespace ArraySortSearchApp
{
public class MainForm : Form
{
private TextBox txtInput;
private TextBox txtTarget;
private TextBox txtOutput;
private Button btnSort;
private Button btnSearch;
private int[] numbers;
public MainForm()
{
Text = "Массив, сортировка, поиск";
Width = 600;
Height = 500;
Label lblInput = new Label
{
Text = "Введите 10 чисел через запятую:",
Left = 10,
Top = 10,
Width = 300
};
txtInput = new TextBox
{
Left = 10,
Top = 35,
Width = 560
};
btnSort = new Button
{
Text = "Заполнить и отсортировать",
Left = 10,
Top = 65,
Width = 220
};
btnSort.Click += BtnSort_Click;
Label lblTarget = new Label
{
Text = "Число для поиска:",
Left = 10,
Top = 100,
Width = 150
};
txtTarget = new TextBox
{
Left = 160,
Top = 97,
Width = 100
};
btnSearch = new Button
{
Text = "Найти (бинарный поиск)",
Left = 270,
Top = 95,
Width = 200
};
btnSearch.Click += BtnSearch_Click;
txtOutput = new TextBox
{
Left = 10,
Top = 135,
Width = 560,
Height = 300,
Multiline = true,
ScrollBars = ScrollBars.Vertical,
ReadOnly = true
};
Controls.Add(lblInput);
Controls.Add(txtInput);
Controls.Add(btnSort);
Controls.Add(lblTarget);
Controls.Add(txtTarget);
Controls.Add(btnSearch);
Controls.Add(txtOutput);
}
private void BtnSort_Click(object sender, EventArgs e)
{
txtOutput.Clear();
string[] parts = txtInput.Text.Split(',');
if (parts.Length != 10)
{
MessageBox.Show("Введите ровно 10 чисел через запятую.");
return;
}
numbers = new int[10];
for (int i = 0; i < 10; i++)
{
if (!int.TryParse(parts[i].Trim(), out numbers[i]))
{
MessageBox.Show("Одно из значений — не целое число.");
return;
}
}
txtOutput.AppendText("Массив до сортировки: " + string.Join(", ", numbers) + Environment.NewLine);
for (int i = 0; i < numbers.Length - 1; i++)
{
for (int j = 0; j < numbers.Length - 1 - i; j++)
{
if (numbers[j] > numbers[j + 1])
{
int temp = numbers[j];
numbers[j] = numbers[j + 1];
numbers[j + 1] = temp;
}
}
txtOutput.AppendText($"После прохода {i + 1}: {string.Join(", ", numbers)}" + Environment.NewLine);
}
txtOutput.AppendText("Массив после сортировки: " + string.Join(", ", numbers) + Environment.NewLine);
}
private void BtnSearch_Click(object sender, EventArgs e)
{
if (numbers == null)
{
MessageBox.Show("Сначала заполните и отсортируйте массив.");
return;
}
if (!int.TryParse(txtTarget.Text.Trim(), out int target))
{
MessageBox.Show("Введите корректное число для поиска.");
return;
}
txtOutput.AppendText(Environment.NewLine + $"Поиск числа {target}:" + Environment.NewLine);
int left = 0;
int right = numbers.Length - 1;
int result = -1;
while (left <= right)
{
int middle = (left + right) / 2;
txtOutput.AppendText($"left={left}, right={right}, middle={middle} (numbers[middle]={numbers[middle]})" + Environment.NewLine);
if (numbers[middle] == target)
{
result = middle;
break;
}
if (target < numbers[middle])
right = middle - 1;
else
left = middle + 1;
}
if (result != -1)
txtOutput.AppendText($"Число {target} найдено на индексе {result}." + Environment.NewLine);
else
txtOutput.AppendText($"Число {target} в массиве не найдено." + Environment.NewLine);
}
}
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new MainForm());
}
}
}