# 考研复习 排序算法 插入排序 折半插入排序 冒泡排序 快速排序 选择排序

## 算法列表

//插入排序
void insertSort(SortTable ST)
{
int i = 0, j = 0;
for (i = 2;i <= ST.length; ++i)
{
if (ST.elem[i] < ST.elem[i-1])
{
ST.elem[0] = ST.elem[i];
for (j = i - 1;ST.elem[0] < ST.elem[j];j--)
ST.elem[j + 1] = ST.elem[j];
ST.elem[j + 1] = ST.elem[0];
}
}
}
//折半插入排序
void B_InsertSort(SortTable ST)
{
int i = 0, j = 0;
int low = 1, mid = 1, high = 1;
for (i = 2;i <= ST.length;++i)
{
low = 1;
high = i - 1;
ST.elem[0]= ST.elem[i];
while (low <= high)
{
mid = (low + high) / 2;
if (ST.elem[mid] < ST.elem[0]) high = mid - 1;   //更换  >   <号
else low = mid + 1;
}
for (j = i - 1;j >= high+1;--j)
ST.elem[j + 1] = ST.elem[j];
ST.elem[high + 1] = ST.elem[0];
}
}
//冒泡排序
void BubbleSort(SortTable ST)
{
int i = 0, j = 0,flag=0;
for (i = ST.length;i > 1;--i)
{
for (j = ST.length;j >ST.length-i+1;--j)
{
if (ST.elem[j - 1] < ST.elem[j])
{
int tmp = ST.elem[j - 1];
ST.elem[j - 1] = ST.elem[j];
ST.elem[j] = tmp;
flag = 1;
}
}
if (flag == 0)
break;
else
flag = 0;
}
}
//快速排序
int Partition(SortTable ST, int low, int high)
{
ElemType prvot = ST.elem[low];
while (low < high)
{
while (low<high && ST.elem[high]>= prvot) --high;
ST.elem[low] = ST.elem[high];
while (low<high && ST.elem[low]<= prvot) ++low;
ST.elem[high] = ST.elem[low];
}
ST.elem[low] = prvot;
return low;
}
void QuickSort(SortTable ST,int low,int high)
{
if (low < high)
{
int PrvotPost = Partition(ST,low,high);
QuickSort(ST, low, PrvotPost-1);
QuickSort(ST, PrvotPost+1, high);
}
}
//选择排序
void SelectSort(SortTable ST)
{
for (int i = 1;i < ST.length - 1;i++)
{
int min = i;
for (int j = i + 1;j <=ST.length;j++)
{
if (ST.elem[min] > ST.elem[j])
min = j;
}
if (min != i)
swap(ST.elem[min], ST.elem[i]);
}
}

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客