汇总带动图的:https://www.cnblogs.com/zjp-blog/p/12186232.html
八大排序复杂度及应用场景:https://blog.csdn.net/FDk_LCL/article/details/84299390
1.插入排序
void insert()
{
int i, j, k;
int tmp;
int buf[12] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int cnt = 11;
for(i = 1; i < cnt; i++)
{
tmp = buf[i];
for(j = i; j > 0 && tmp < buf[j-1]; j--)
{
buf[j] = buf[j-1];
}
if(j != i)
{
buf[j] = tmp;
}
print(buf, cnt);
}
print(buf, cnt);
}
2.希尔排序
详解:https://blog.csdn.net/u010095372/article/details/84259178
void xier()
{
int i, j, k;
int tmp;
int buf[12] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int cnt = 11;
int dp;
print(buf, cnt);
for(dp = cnt / 2; dp > 0; dp /= 2)
{
for(i = dp; i < cnt; i++)
{
tmp = buf[i];
for(j = i; j >= dp && tmp < buf[j-dp]; j-=dp)
{
buf[j] = buf[j-dp];
}
if(j != i)
{
buf[j] = tmp;
}
print(buf, cnt);
}
}
}
3.冒泡排序
详解:https://blog.csdn.net/u010095372/article/details/83998763
int maopao()
{
int i, j, k;
int tmp;
int buf[12] = {10, 9, 8, 7, 16, 5, 4, 3, 2, 1, 0};
int cnt = 11;
int dp;
print(buf, cnt);
for(i = 0; i < cnt - 1; i++) //控制需要遍历几次
{
for(j = 0; j < cnt-i-1; j++) //从下标0开始往后比较
{
if(buf[j] > buf[j+1])
{
tmp = buf[j];
buf[j] = buf[j+1];
buf[j+1] = tmp;
}
}
print(buf, cnt);
}
}
4.快速排序
详解:https://blog.csdn.net/mj475002864/article/details/107222489/
void kuaipai(int *buf, int low, int high)
{
int key;
int left = low;
int right = high;
if(low >= high)
return;
key = buf[left];
while(left < right)
{
while(left < right && buf[right] > key)
{
right--;
}
buf[left] = buf[right];
while(left < right && buf[left] < key)
{
left++;
}
buf[right] = buf[left];
}
buf[left] = key;
kuaipai(buf, low, left - 1);
kuaipai(buf, left + 1, high);
}
5.归并排序
//将两个有序序列合成一个有序序列
void sort(int *buf, int left, int mid, int right)
{
int index = 0;
int tmp[32] = {0};
int k = mid + 1;
int i = left;
while(i <= mid && k <= right)
{
if(buf[i] < buf[k])
{
tmp[index] = buf[i];
i++;
}
else
{
tmp[index] = buf[k];
k++;
}
index++;
}
while(i <= mid)
{
tmp[index] = buf[i];
i++;
index++;
}
while(k <= right)
{
tmp[index] = buf[k];
k++;
index++;
}
memcpy(buf+left, tmp, index*sizeof(int));
}
//归并排序
void guibing(int *buf, int left, int right)
{
if(left >= right)
{
return;
}
int mid = (left + right) / 2;
guibing(buf, left, mid); //左部分递归归并
guibing(buf, mid+1, right); //右部分递归归并
sort(buf, left, mid, right);//左右部分有序合并
}
6.基数排序
https://www.cnblogs.com/daemon94011/p/8870326.html
7.简单选择排序
int main(void)
{
int i, j, k;
int tmp;
int buf[12] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int cnt = 11;
int min;
print(buf, cnt);
for(i = 0; i < cnt; i++)
{
min = i;
for(k = i; k < cnt; k++) //遍历没有被排序的数,找到最小值索引
{
if(buf[k] < buf[min])
{
min = k;
}
}
tmp = buf[min];
buf[min] = buf[i];
buf[i] = tmp;
}
print(buf, cnt);
return 0;
}
8.堆排序
https://blog.csdn.net/weixin_42109012/article/details/91668543
https://blog.csdn.net/qiukui111/article/details/106558600
void swap(int *arr, int i, int j)
{
int tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
void ajustHeap(int *arr, int index, int size)
{
int parent = index;
int child = parent * 2 + 1; //左子节点
while(child < size)
{
//选出较大的子节点
if(child + 1 < size && arr[child] < arr[child + 1])
{
child++;
}
//父节点大于较大的子节点,就不需要交换
if(arr[parent] > arr[child])
{
break;
}
//交换了父子节点,继续后边比较直到父节点大于子节点
swap(arr, parent, child);
parent = child;
child = child * 2 + 1;
}
}
int main(void)
{
int i, j, k;
int tmp;
int buf[12] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
int cnt = 11;
int min;
print(buf, cnt);
//构建大根堆, 从最后一个非叶子节点往前遍历
for(i = cnt/2-1; i >= 0; i--)
{
ajustHeap(buf, i, cnt);
}
for(i = cnt-1; i > 0; i--)
{
swap(buf, 0, i);
ajustHeap(buf, 0, i);
}
print(buf, cnt);
return 0;
}