数据结构与算法(七)排序
- 直接插入排序
void DInsertSort(SqList* Sq)//直接插入排序
{
int i, j;
for (i = 2; i <= Sq->length; ++i)
{
Sq->r[0].key = Sq->r[i].key;
for (j = i-1; Sq->r[0].key<Sq->r[j].key; j--)
{
Sq->r[j + 1].key = Sq->r[j].key;
}
Sq->r[j + 1].key = Sq->r[0].key;
}
}
- 二分法插入排序
void BInsersort(SqList* Sq)//二分插入排序
{
int i, j;
for (i = 2; i <= Sq->length; ++i)
{
Sq->r[0].key = Sq->r[i].key;
int low = 1;
int high = i-1;
while (low <= high)
{
int mid = (low + high) / 2;
if (Sq->r[0].key < Sq->r[mid].key)
high = mid - 1;
else
low = mid + 1;
}
for (j = i - 1; j >= high + 1; --j)
Sq->r[j + 1].key = Sq->r[j].key;
Sq->r[high + 1].key = Sq->r[0].key;
}
}
- 希尔排序
void ShellInsert(SqList* Sq, int delta)//希尔排序,插入部分
{
int i, j;
for (i = 1 + delta; i <= Sq->length; ++i)
{
if (Sq->r[i].key < Sq->r[i - delta].key)
{
Sq->r[0].key = Sq->r[i].key;
for (j = i - delta; j > 0 && Sq->r[0].key < Sq->r[j].key; j = j - delta)
{
Sq->r[j + delta].key = Sq->r[j].key;
}
Sq->r[j + delta].key = Sq->r[0].key;
}
}
}
void ShellSort(SqList* Sq)//希尔排序,循环部分
{
int i = 0;
int delta[3] = { 5,3,1 };
for (i = 0; i < 3; ++i)
{
ShellInsert(Sq,delta[i]);
}
}
- 冒泡排序
void BubbleSort(SqList* Sq)//冒泡排序
{
int i, j;
for (i = 1; i <= Sq->length - 1; ++i)
{
for (j = 1; j <= Sq->length - i; ++j)
{
if (Sq->r[j].key > Sq->r[j + 1].key)
{
int t = Sq->r[j].key;
Sq->r[j].key = Sq->r[j + 1].key;
Sq->r[j + 1].key = t;
}
}
}
}
- 快速排序
int Partition(SqList* Sq, int low, int high)//快速排序,定位函数
{
Sq->r[0].key = Sq->r[low].key;
int pivotkey = Sq->r[low].key;
while (low < high)
{
while (low < high && Sq->r[high].key >= pivotkey)
high--;
Sq->r[low].key = Sq->r[high].key;
while (low < high && Sq->r[low].key <= pivotkey)
low++;
Sq->r[high].key = Sq->r[low].key;
}
Sq->r[low].key = Sq->r[0].key;
return low;
}
void QSort(SqList* Sq, int low, int high)//快速排序,排序函数
{
int pivotloc;
if (low < high)
{
pivotloc = Partition(Sq,low,high);
QSort(Sq,low,pivotloc-1);
QSort(Sq, pivotloc + 1,high);
}
}
完整代码
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define MAX 20
typedef struct
{
int key;
int other;
}RedType;
typedef struct
{
RedType r[MAX+1];
int length;
}SqList;
SqList* CreateList(int n)
{
SqList* Sq = (SqList*)malloc(sizeof(SqList)*n);
if (Sq == NULL)
{
exit(-1);
}
for (int i = 1; i <= n; ++i)//[0]不放元素,用于后面排序的优化
{
printf("请输入元素:");
scanf_s("%d",&(Sq->r[i].key));
}
Sq->length = n;
return Sq;
}
void show(SqList* Sq)
{
for (int i = 1; i <= Sq->length; ++i)
{
printf("%d\t",Sq->r[i].key);
}
printf("\n");
}
void DInsertSort(SqList* Sq)//直接插入排序
{
int i, j;
for (i = 2; i <= Sq->length; ++i)
{
Sq->r[0].key = Sq->r[i].key;
for (j = i-1; Sq->r[0].key<Sq->r[j].key; j--)
{
Sq->r[j + 1].key = Sq->r[j].key;
}
Sq->r[j + 1].key = Sq->r[0].key;
}
}
void BInsersort(SqList* Sq)//二分插入排序
{
int i, j;
for (i = 2; i <= Sq->length; ++i)
{
Sq->r[0].key = Sq->r[i].key;
int low = 1;
int high = i-1;
while (low <= high)
{
int mid = (low + high) / 2;
if (Sq->r[0].key < Sq->r[mid].key)
high = mid - 1;
else
low = mid + 1;
}
for (j = i - 1; j >= high + 1; --j)
Sq->r[j + 1].key = Sq->r[j].key;
Sq->r[high + 1].key = Sq->r[0].key;
}
}
void ShellInsert(SqList* Sq, int delta)//希尔排序,插入部分
{
int i, j;
for (i = 1 + delta; i <= Sq->length; ++i)
{
if (Sq->r[i].key < Sq->r[i - delta].key)
{
Sq->r[0].key = Sq->r[i].key;
for (j = i - delta; j > 0 && Sq->r[0].key < Sq->r[j].key; j = j - delta)
{
Sq->r[j + delta].key = Sq->r[j].key;
}
Sq->r[j + delta].key = Sq->r[0].key;
}
}
}
void ShellSort(SqList* Sq)//希尔排序,循环部分
{
int i = 0;
int delta[3] = { 5,3,1 };
for (i = 0; i < 3; ++i)
{
ShellInsert(Sq,delta[i]);
}
}
void BubbleSort(SqList* Sq)//冒泡排序
{
int i, j;
for (i = 1; i <= Sq->length - 1; ++i)
{
for (j = 1; j <= Sq->length - i; ++j)
{
if (Sq->r[j].key > Sq->r[j + 1].key)
{
int t = Sq->r[j].key;
Sq->r[j].key = Sq->r[j + 1].key;
Sq->r[j + 1].key = t;
}
}
}
}
int Partition(SqList* Sq, int low, int high)//快速排序,定位函数
{
Sq->r[0].key = Sq->r[low].key;
int pivotkey = Sq->r[low].key;
while (low < high)
{
while (low < high && Sq->r[high].key >= pivotkey)
high--;
Sq->r[low].key = Sq->r[high].key;
while (low < high && Sq->r[low].key <= pivotkey)
low++;
Sq->r[high].key = Sq->r[low].key;
}
Sq->r[low].key = Sq->r[0].key;
return low;
}
void QSort(SqList* Sq, int low, int high)//快速排序,排序函数
{
int pivotloc;
if (low < high)
{
pivotloc = Partition(Sq,low,high);
QSort(Sq,low,pivotloc-1);
QSort(Sq, pivotloc + 1,high);
}
}
int main()
{
int n;
SqList* Sq;
printf("请输入元素个数: ");
scanf_s("%d",&n);
Sq=CreateList(n);
show(Sq);
//DInsertSort(Sq);
//show(Sq);
//BInsersort(Sq);
//show(Sq);
//ShellSort(Sq);
//show(Sq);
//BubbleSort(Sq);
//show(Sq);
QSort(Sq,1,Sq->length);
show(Sq);
return 0;
}