数据结构各种排序
插入排序
1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace DataStructer6


{7
class InsertionSorter8

{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

选择排序
1
public 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
}冒泡
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++;
}
}
}
}
浙公网安备 33010602011771号