.NET 4.x Нужна анимация выделения столбика, который находится по бинарному поиску - C#
Формулировка задачи:
Я написал программу, которая визуализирует сортировку массива. В ней же есть бинарный поиск. Но я не могу придумать несложную анимацию того, как выделяется правильный столбик, который находится через бинарный поиск. Помогите, пожалуйста
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Threading;
- namespace WindowsFormsApplication1
- {
- public partial class Визуализация : Form
- {
- public Визуализация()
- {
- InitializeComponent();
- }
- public int[] array;
- int razmer = 30;
- private void swap<T>(ref T a, ref T b)
- {
- T c = a;
- a = b;
- b = c;
- }
- //Прорисовка сортированного массива
- private void drawSort(int[] array)
- {
- bool flag = true;
- Pen pen = new Pen(Color.Yellow);
- Graphics graphics = pictureBox1.CreateGraphics();
- graphics.Clear(Color.Black);
- for (int i = (int)0; i <= razmer; i++)
- {
- for (int j = 0; j < array.Length; j++)
- {
- if (flag)
- pen = new Pen(Color.Yellow);
- else
- pen = new Pen(Color.Violet);
- flag = !flag;
- if (array[j] >= i)
- graphics.FillRectangle(new SolidBrush(pen.Color), 15 * j, pictureBox1.Height - 15 * i, 15, 15);
- }
- }
- }
- //Прорисовка клеточного поля
- private void drawMarking()
- {
- Graphics graphics = pictureBox1.CreateGraphics();
- Pen pen = new Pen(Color.DarkGreen);
- for (int i = 0; i < pictureBox1.Height; i += 15)
- graphics.DrawLine(pen, 0, pictureBox1.Height - i, pictureBox1.Width, pictureBox1.Height - i);
- for (int i = 0; i < pictureBox1.Width; i += 15)
- graphics.DrawLine(pen, i, 0, i, pictureBox1.Width);
- }
- private void pictureBox1_Paint(object sender, PaintEventArgs e)
- {
- ((PictureBox)sender).CreateGraphics().Clear(Color.Black);
- drawMarking();
- }
- //Кнопка "Запуск"
- private void button3_Click(object sender, EventArgs e)
- {
- textBox1.Text = " ";
- listBox1.Items.Clear();
- Random random = new Random();
- array = new int[(int)30];
- array = array.Select(x => random.Next((int)0, (int)(razmer))).ToArray();
- for (int i = 0; i < array.Length; i++)
- {
- int min = i;
- for (int j = i + 1; j < array.Length; j++)
- if (array[j] < array[min])
- min = j;
- if (min != i)
- swap(ref array[i], ref array[min]);
- listBox1.Items.Add(array[i]);
- drawSort(array);
- drawMarking();
- Thread.Sleep(40);
- }
- }
- //Кнопка "Поиск"
- private void button2_Click(object sender, EventArgs e)
- {
- //Бинарный поиск
- int chislo = (int)numericUpDown1.Value;
- int lev = 0;
- int prav = razmer;
- int mid;
- if (chislo < array[0] || chislo > array[array.Length - 1])
- {
- textBox1.Text = "Число за границами массива";
- }
- else
- {
- while (lev < prav)
- {
- mid = lev + (prav - lev) / 2;
- if (chislo <= array[mid])
- {
- prav = mid;
- }
- else
- {
- lev = mid + 1;
- }
- }
- if (array[prav] == chislo)
- {
- int N = prav + 1;
- int i = prav;
- while (array[i] == chislo & i < array.Length - 1)
- {
- i++;
- }
- textBox1.Text = "Место числа в массиве: " + N;
- }
- else
- {
- textBox1.Text = "Числа нет в массиве";
- }
- }
- }
- //Закрыть
- private void button5_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- private void button4_Click(object sender, EventArgs e)
- {
- Титульник form1 = new Титульник();
- form1.Show();
- Hide();
- }
- private void pictureBox1_Click(object sender, EventArgs e)
- {
- }
- }
- }
Решение задачи: «.NET 4.x Нужна анимация выделения столбика, который находится по бинарному поиску»
textual
Листинг программы
- System.Threading.Thread.Sleep(1000);
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д