#include<stdio.h>
#include<stdlib.h>
#include<time.h>
/*排序的时间复杂度由两个因素决定
* 1.比较次数(compare)
* 2.移动元素次数(more)
*/
typedef int KeyType;
//设计关键字结构体
typedef struct
{
KeyType key;
//....可以携带其他信息
}RecType;
//交换两个结构体
void swap(RecType& i, RecType& j)
{
RecType tmp;
tmp = i;
i = j;
j = tmp;
}
//------------------------------------------------------插入排序----------------------------
//直接插入
/*时间复杂度n*n;
就地排序
稳定*/
void Insert(RecType R[], int n)
{
int i, j;
RecType tmp;
for (i = 1; i < n; i++)
{
if (R[i - 1].key > R[i].key)/*[0,i-1]为有序序列,i后面的序列无序*/
{
tmp = R[i];
j = i - 1;
do/*移动元素*/
{
R[j + 1] = R[j];
j--;
} while (j >= 0 && R[j].key > tmp.key); /*一种是有序数中的所有数都比这个要插入的数大,把这个数放到第一个.
另一种放到比它第一个小的有序数后面*/
R[j + 1] = tmp;
}
}
}
//折半插入排序
/*
* 时间复杂度n*n,比较次数比直接插入少
原地排序
稳定*/
void BinInsertSort(RecType R[], int n)
{
int i, j, low, high, mid;
RecType tmp;
for (i = 1; i < n; i++)
{
if (R[i].key < R[i - 1].key)
{
low = 0;
high = i - 1;
tmp = R[i];
while (low <= high) /* 二分查找到适合插入的位置*/
{
mid = low + high;
if (tmp.key < R[mid].key)
high = mid - 1;
else
low = mid + 1;
}
for (j = i-1;j >= high + 1;j--)/*移动元素*/
R[j + 1] = R[j];
R[high + 1] = tmp;
}
}
}
//希尔排序
/*在最后一次排序之前都是无限的,每次排序后就越接近有序*/
/*时间复杂度n的1.3次方
* 原地排序
* 不稳定
*/
void ShellSort(RecType R[], int n)
{
int i, j, d;
RecType tmp;
d = n / 2; /*希尔增量*/
while (d > 0) /*分成d个组,每组用直接插入的方式*/
{
for (i = d;i < n;i++)
{
j = i - d;
tmp = R[i];
while (j >= 0 && R[j].key > tmp.key)/*类似直接插入*/
{
R[j + d] = R[j];
j = j - d;
}
R[j + d] = tmp;
}
d = d / 2; /*增量不断减少到1*/
}
}
//------------------------------------------------------------------------交换排序--------------------------------------------------
//冒泡排序
/*时间复杂度n*n
原地排序
稳定*/
void BubbleSort(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;
}
}
//快速排序
/*时间复杂度nlog2n
* 就地排序
* 不稳定
*/
int partition(RecType R[], int s, int t) /*在i左边的元素都比i小
在i右边的元素都比i大*/
{
int i = s, j = t;
RecType tmp = R[i];
while (i < j)
{
while (j > i && R[j].key >= tmp.key)
j--;
R[i] = R[j];
while (i < j && R[i].key <= tmp.key)
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); //递归右边
}
}
//选择排序
/*
时间复杂度n*n
就地排序
稳定*/
void SelectSort(RecType R[], int n)
{
int i, j, k;
for (i = 0;i < 10;i++)
{
k = i;
for (j = i + 1;j < 10;j++)
{
if (R[j].key < R[k].key) /*选择最小的一个元素*/
k = j;
}
if (k != i)
swap(R[k], R[i]);
}
}
//展示序列
void show(RecType R[])
{
int i;
for (i = 0;i < 10;i++)
printf("%d ", R[i].key);
printf("\n");
}
int main()
{
RecType R[10];
int i;
srand((int)time(NULL));/*生存一些随机数*/
for (i = 0;i < 10;i++)
R[i].key =rand()%9;
show(R);
//Insert(R, 10);
//BinInsertSort(R, 10);
//ShellSort(R, 10); /*排序函数的用法*/
//BubbleSort(R, 10);
//QuickSort(R, 0,9);
//SelectSort(R, 10);
show(R);
}
排序一(直接插入排序,折半插入排序,希尔排序,冒泡排序,快速排序,选择排序)
猜你喜欢
转载自blog.csdn.net/weixin_50188452/article/details/110724641
今日推荐
周排行