插入排序
Code
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace DataStructer
6{
7 class InsertionSorter
8 {
9 public void sort(int[] list)
10 {
11 for (int i=0; i < list.Length; i++)
12 {
13 int t = list[i];
14 int j = i;
15 while((j>0)&&(list[j-1]>t))
16 {
17 list[j] = list[j - 1];
18 --j;
19 }
20
21 list[j] = t;
22 }
23 }
24
25 }
26 }
27}
28
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5namespace DataStructer
6{
7 class InsertionSorter
8 {
9 public void sort(int[] list)
10 {
11 for (int i=0; i < list.Length; i++)
12 {
13 int t = list[i];
14 int j = i;
15 while((j>0)&&(list[j-1]>t))
16 {
17 list[j] = list[j - 1];
18 --j;
19 }
20
21 list[j] = t;
22 }
23 }
24
25 }
26 }
27}
28
选择排序
Code
1public void sort(int[] list)
2 {
3 for (int i = 0; i < list.Length; i++)
4 {
5 min = i;
6 for (int j = i + 1; j < list.Length; j++)
7 {
8 if (list[j] < list[min])
9 {
10 min = j;
11 }
12 }
13
14 int t = list[min];
15 list[min] = list[i];
16 list[i] = t;
17 }
18 }
1public void sort(int[] list)
2 {
3 for (int i = 0; i < list.Length; i++)
4 {
5 min = i;
6 for (int j = i + 1; j < list.Length; j++)
7 {
8 if (list[j] < list[min])
9 {
10 min = j;
11 }
12 }
13
14 int t = list[min];
15 list[min] = list[i];
16 list[i] = t;
17 }
18 }
冒泡
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace DataStructer
{
class BubbleSorter
{
public void sort(int[] list)
{
int i, j, temp;
bool done = false;
j = 1;
while ((j < list.Length) && (!done))
{
done = true;
for (i = 0; i < list.Length - j; i++)
{
if (list[i] > list[i + 1])
{
done = false;
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
j++;
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace DataStructer
{
class BubbleSorter
{
public void sort(int[] list)
{
int i, j, temp;
bool done = false;
j = 1;
while ((j < list.Length) && (!done))
{
done = true;
for (i = 0; i < list.Length - j; i++)
{
if (list[i] > list[i + 1])
{
done = false;
temp = list[i];
list[i] = list[i + 1];
list[i + 1] = temp;
}
}
j++;
}
}
}
}