// 冒泡排序
void BubbleSort(RecType R[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = n - 1; j > i; j--)
if (R[j].key < R[j - 1].key)
swap(R[j], R[j - 1]);
}
void BubbleSort1(RecType R[], int n)
{
int i, j;
bool exchange;
for (i = 0; i < n - 1; i++)
{
exchange = false;
for (j = n - 1; j > i; j--)
if (R[j].key < R[j - 1].key)
{
swap(R[j], R[j - 1]);
exchange = true;
}
if (!exchange)
return;
}
}
快速排序
//快速排序
int partition(RecType R[], int s, int t)
{
int i = s, j = t;
RecType tmp = R[i];
while (i < j) //i=j截止
{
while (j > i && R[j].key >= tmp.key) //右到左 找到小于tmp的
j--;
R[i] = R[j];
while (j > i && R[j].key <= tmp.key) //左到右 找到大于tmp的
i++;
R[j] = R[i];
}
R[i] = tmp;
return i;
}
void QuickSort(RecType R[], int s, int t)
{
int i;
if (s < t)
{
i = partition(R, s, t);
QuickSort(R, s, i - 1);
QuickSort(R, i + 1, t);
}
}