四个常见的排序算法[原创]
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Text;
7
using System.Windows.Forms;
8![]()
9
namespace WindowsApplication1
10
{
11
public partial class maopaosort : Form
12
{
13
public maopaosort()
14
{
15
InitializeComponent();
16
}
17![]()
18
//冒泡排序
19
private void button1_Click(object sender, EventArgs e)
20
{
21
int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
22
Sort(iArrary);
23
for (int m = 0; m < iArrary.Length; m++)
24
{
25
this.listBox1.Items.Add(iArrary[m]); //"{0} ",
26
}
27
}
28![]()
29
public void Sort(int[] list)
30
{
31
int i, j, temp;
32
bool done = false;
33
j = 1;
34
while ((j < list.Length) && (!done))
35
{
36
done = true;
37
for (i = 0; i < list.Length - j; i++)
38
{
39
if (list[i] > list[i + 1])
40
{
41
done = false;
42
temp = list[i];
43
list[i] = list[i + 1];
44
list[i + 1] = temp;
45
}
46
}
47
j++;
48
}
49
}
50![]()
51
private void maopaosort_Load(object sender, EventArgs e)
52
{
53
this.label1.Text = " 1,\n 5,\n 13,\n 6,\n 10,\n 55,\n 99,\n 2,\n 87,\n 12,\n 34,\n 75,\n 33,\n 47\n ";
54
this.label2.Text = "1,\n5,\n3,\n6,\n10,\n55,\n9,\n2,\n87,\n12,\n34,\n75,\n33,\n47\n";
55
this.label3.Text = "1,\n13,\n3,\n6,\n10,\n55,\n98,\n2,\n87,\n12,\n34,\n75,\n33,\n47\n";
56
this.label4.Text = "1,\n5,\n13,\n6,\n10,\n55,\n99,\n2,\n87,\n12,\n34,\n75,\n33,\n47\n";
57
}
58![]()
59
60
//选择排序
61
private void button2_Click(object sender, EventArgs e)
62
{
63
int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };
64
Sort1(iArrary);
65
for (int m = 0; m < iArrary.Length; m++)
66
{
67
this.listBox2.Items.Add(iArrary[m]);
68
}
69![]()
70
}
71![]()
72
private int min;
73
public void Sort1(int[] list)
74
{
75
for (int i = 0; i < list.Length - 1; i++)
76
{
77
min = i;
78
for (int j = i + 1; j < list.Length; j++)
79
{
80
if (list[j] < list[min])
81
min = j;
82
}
83
int t = list[min];
84
list[min] = list[i];
85
list[i] = t;
86
}
87
}
88![]()
89
//插入排序
90
private void button3_Click(object sender, EventArgs e)
91
{
92
int[] iArrary = new int[] { 1, 13, 3, 6, 10, 55, 98, 2, 87, 12, 34, 75, 33, 47 };
93
Sort2(iArrary);
94
for (int m = 0; m < iArrary.Length; m++)
95
{
96
this.listBox3.Items.Add(iArrary[m]);
97
}
98
}
99
public void Sort2(int[] list)
100
{
101
for (int i = 1; i < list.Length; i++)
102
{
103
int t = list[i];
104
int j = i;
105
while ((j > 0) && (list[j - 1] > t))
106
{
107
list[j] = list[j - 1];
108
--j;
109
}
110
list[j] = t;
111
}
112
}
113![]()
114
//希尔排序
115
private void button4_Click(object sender, EventArgs e)
116
{
117
int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };
118
Sort3(iArrary);
119
for (int m = 0; m < iArrary.Length; m++)
120
{
121
this.listBox4.Items.Add(iArrary[m]);
122
}
123
}
124
public void Sort3(int[] list)
125
{
126
int inc;
127
for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;
128
for (; inc > 0; inc /= 3)
129
{
130
for (int i = inc + 1; i <= list.Length; i += inc)
131
{
132
int t = list[i - 1];
133
int j = i;
134
while ((j > inc) && (list[j - inc - 1] > t))
135
{
136
list[j - 1] = list[j - inc - 1];
137
j -= inc;
138
}
139
list[j - 1] = t;
140
}
141
}
142
}
143
}
144
}
145![]()
146![]()
using System;2
using System.Collections.Generic;3
using System.ComponentModel;4
using System.Data;5
using System.Drawing;6
using System.Text;7
using System.Windows.Forms;8

9
namespace WindowsApplication110
{11
public partial class maopaosort : Form12
{13
public maopaosort()14
{15
InitializeComponent();16
}17

18
//冒泡排序19
private void button1_Click(object sender, EventArgs e)20
{21
int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };22
Sort(iArrary);23
for (int m = 0; m < iArrary.Length; m++)24
{25
this.listBox1.Items.Add(iArrary[m]); //"{0} ",26
} 27
}28

29
public void Sort(int[] list)30
{31
int i, j, temp;32
bool done = false;33
j = 1;34
while ((j < list.Length) && (!done))35
{36
done = true;37
for (i = 0; i < list.Length - j; i++)38
{39
if (list[i] > list[i + 1])40
{41
done = false;42
temp = list[i];43
list[i] = list[i + 1];44
list[i + 1] = temp;45
}46
}47
j++;48
}49
}50

51
private void maopaosort_Load(object sender, EventArgs e)52
{53
this.label1.Text = " 1,\n 5,\n 13,\n 6,\n 10,\n 55,\n 99,\n 2,\n 87,\n 12,\n 34,\n 75,\n 33,\n 47\n ";54
this.label2.Text = "1,\n5,\n3,\n6,\n10,\n55,\n9,\n2,\n87,\n12,\n34,\n75,\n33,\n47\n";55
this.label3.Text = "1,\n13,\n3,\n6,\n10,\n55,\n98,\n2,\n87,\n12,\n34,\n75,\n33,\n47\n";56
this.label4.Text = "1,\n5,\n13,\n6,\n10,\n55,\n99,\n2,\n87,\n12,\n34,\n75,\n33,\n47\n";57
}58

59
60
//选择排序61
private void button2_Click(object sender, EventArgs e)62
{63
int[] iArrary = new int[] { 1, 5, 3, 6, 10, 55, 9, 2, 87, 12, 34, 75, 33, 47 };64
Sort1(iArrary);65
for (int m = 0; m < iArrary.Length; m++)66
{67
this.listBox2.Items.Add(iArrary[m]);68
} 69

70
}71

72
private int min;73
public void Sort1(int[] list)74
{75
for (int i = 0; i < list.Length - 1; i++)76
{77
min = i;78
for (int j = i + 1; j < list.Length; j++)79
{80
if (list[j] < list[min])81
min = j;82
}83
int t = list[min];84
list[min] = list[i];85
list[i] = t;86
}87
}88

89
//插入排序90
private void button3_Click(object sender, EventArgs e)91
{92
int[] iArrary = new int[] { 1, 13, 3, 6, 10, 55, 98, 2, 87, 12, 34, 75, 33, 47 };93
Sort2(iArrary);94
for (int m = 0; m < iArrary.Length; m++)95
{96
this.listBox3.Items.Add(iArrary[m]);97
}98
}99
public void Sort2(int[] list)100
{101
for (int i = 1; i < list.Length; i++)102
{103
int t = list[i];104
int j = i;105
while ((j > 0) && (list[j - 1] > t))106
{107
list[j] = list[j - 1];108
--j;109
}110
list[j] = t;111
}112
}113

114
//希尔排序115
private void button4_Click(object sender, EventArgs e)116
{117
int[] iArrary = new int[] { 1, 5, 13, 6, 10, 55, 99, 2, 87, 12, 34, 75, 33, 47 };118
Sort3(iArrary);119
for (int m = 0; m < iArrary.Length; m++)120
{121
this.listBox4.Items.Add(iArrary[m]);122
}123
}124
public void Sort3(int[] list)125
{126
int inc;127
for (inc = 1; inc <= list.Length / 9; inc = 3 * inc + 1) ;128
for (; inc > 0; inc /= 3)129
{130
for (int i = inc + 1; i <= list.Length; i += inc)131
{132
int t = list[i - 1];133
int j = i;134
while ((j > inc) && (list[j - inc - 1] > t))135
{136
list[j - 1] = list[j - inc - 1];137
j -= inc;138
}139
list[j - 1] = t;140
}141
}142
}143
}144
}145

146



浙公网安备 33010602011771号