1.冒泡排序(Bubble Sort)
//冒泡排序
void BubbleSort(int *a, int length)
{
for (int i = 0;i < length - 1;i++)
{
for (int j = 0;j < length - i - 1;j++)
{
if (a[j] > a[j + 1])
{//冒泡,交换
int temp = a[j];
a[j] = a[j + 1];
a[j + 1] = temp;
}
}
}
}
2.选择排序(Selection Sort)
//选择排序
void SelectSort(int *a, int length)
{
for (int i = 0;i < length-1;i++)
{
int k = i;
for (int j = i + 1;j < length;j++)
{
if (a[j] < a[k])
{
k = j;
}
}
//找到了最小值
if (k != i)
{//交换数据
int temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
}
3.插入排序(Insertion Sort)
//插入排序
void InsertSort(int *a, int length)
{
for (int i = 1;i < length;i++)
{
int j = i;
while (a[j - 1] > a[j] && j >= 0)
{
//交换数据
int temp = a[j];
a[j] = a[j - 1];
a[j - 1] = temp;
//更新j的值
j--;
}
}
}
4.希尔排序(Shell Sort)
5.归并排序(Merge Sort)
6.快速排序(Quick Sort)
//快速排序
void QuickSort(int *a, int length)
{
}