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; namespace quickSort { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int[] data = new int[25]; private void button1_Click(object sender, EventArgs e) { listBox1.Items.Clear(); Random R = new Random(); for (int i = 0; i < data.Length; i++) listBox1.Items.Add(R.Next(1, 100).ToString()); } private void dspData() { listBox2.Items.Clear(); for (int i = 0; i < data.Length; i++) listBox2.Items.Add(data[i].ToString()); } private void setData() { for (int i = 0; i < data.Length; i++) { listBox1.SelectedIndex = i; data[i] = int.Parse(listBox1.SelectedItem.ToString()); } listBox1.SelectedIndex = -1; } private int pivot(int[] a, int L, int R) { int x = a[(L + R) / 2], x1 = a[L], x2 = a[R]; if ((x == x1) || (x == x2)) x = (x1 + x2) / 2; else if (x == x2) x = (x1 + x2) / 2; else if ((x <= x1 && x1 <= x2) || (x2 <= x1 && x1 <= x)) x = x1; else if ((x <= x2 && x2 <= x1) || (x1 <= x2 && x2 <= x)) x = x2; return x; } private void quickSort(ref int[]a, int left, int right) { int L = left, R = right, x = pivot(a, L, R); do { while (a[L] < x) L++; while (a[R] > x) R--; if (L <= R) { int t = a[L]; a[L] = a[R]; a[R] = t; L++; R--; } } while (L <= R); if (left < R) quickSort(ref a, left, R); if (L < right) quickSort(ref a, L, right); } private void quickSort() { quickSort(ref data, 0, data.Length - 1); } private void button2_Click(object sender, EventArgs e) { setData(); quickSort(); dspData(); } private void Form1_Load(object sender, EventArgs e) { button1_Click(sender, e); } } }