//■シェルソート // Buttonを2個、ListBoxを2個配置すること。 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 shellSort { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private int[] data = new int[100]; 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 seth() { int h = 1; while (h < data.Length / 9) h = h * 3 + 1; return h; } private void shellSort() {//ギャップ列を変更するには、sethと h /= 3 の部分を変更すること for (int h = seth(); h > 0; h /= 3) { //MessageBox.Show("h=" + h.ToString()); for (int i = h; i < data.Length; i++) { int tmp = data[i], j; for (j = i - h; j >= 0 && data[j] > tmp; j -= h) data[j + h] = data[j]; data[j + h] = tmp; } } } private void button2_Click(object sender, EventArgs e) { setData(); shellSort(); dspData(); } private void Form1_Load(object sender, EventArgs e) { button1_Click(sender, e); } } }