插入排序
1、直接插入排序
基本思想:把待排序的记录按其关键码值的大小逐个插入到一个已经排好序的有序序列中,直到所有的记录插入完为止,得到一个新的有序序列 。
Sort.c –>下面的排序都用这个就不重复写了
#pragma once
#include<stdio.h>
void PrintArray(int* a, int n);
// 插入排序
void InsertSort(int* a, int n);
void TestInsertSort();
// 希尔排序
void ShellSort(int* a, int n);
void TestShellSort();
// 直接选择排序
void SelectSort(int* a, int n);
void TestSelectSort();
// 堆排序
void HeapSort(int* a, int n);
void TestHeapSort();
// 1、驼峰法 HeapSort 类型/函数所有单词有字母大写,变量首单词小写,后面单词大写
// 2、_分割命名 heap_sort
// 都可以,但是不要混着
// 不要用拼音,不会写单词,去查一查
void BubbleSort(int* a, int n);
void TestBubbleSort();
void QuickSort(int* a, int left, int right);
void TestQuickSort();
void MergeSort(int* a, int n);
void TestMergeSort();
// 计数
void CountSort(int*, int n);
void TestCountSort();
Sort.c
#include "Sort.h"
void PrintArray(int* a, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d ", a[i]);
}
printf("\n");
}
//直接插入排序
void InsertSort(int* a, int n)
{
for (int i = 0; i < n - 1; ++i)
{
// 单趟插入
int end = i;
int tmp = a[end + 1];
while (end >= 0)
{
if (tmp < a[end])
{
a[end + 1] = a[end];
--end;
}
else
{
break;
}
}
a[end + 1] = tmp;
}
}
void TestInsertSort()
{
int a[] = { 54, 38, 96, 23, 15, 72, 60, 45, 83 };
InsertSort(a, sizeof(a) / sizeof(int));
PrintArray(a, sizeof(a) / sizeof(int));
}
test.c ->输出排序结构
int main()
{
TestInsertSort();
//TestShellSort();
//TestSelectSort();
//TestHeapSort();
//TestOP();
return 0;
}
test.c->输出所需要的时间
#include <time.h>
#include <stdlib.h>
#include "Sort.h"
void TestOP()
{
srand(time(0));
const int N = 10000;
int* a1 = (int*)malloc(sizeof(int)*N);
int* a2 = (int*)malloc(sizeof(int)*N);
int* a3 = (int*)malloc(sizeof(int)*N);
int* a4 = (int*)malloc(sizeof(int)*N);
int* a5 = (int*)malloc(sizeof(int)*N);
int* a6 = (int*)malloc(sizeof(int)*N);
int* a7 = (int*)malloc(sizeof(int)*N);
for (int i = 0; i < N; ++i)
{
a1[i] = rand();
a2[i] = a1[i];
a3[i] = a1[i];
a4[i] = a1[i];
a5[i] = a1[i];
a6[i] = a1[i];
a7[i] = a1[i];
}
int begin1 = clock();
InsertSort(a1, N);
int end1 = clock();
printf("InsertSort:%d\n", end1 - begin1);
free(a1);
free(a2);
free(a3);
free(a4);
free(a5);
free(a6);
}
int main()
{
// TestInsertSort();
//TestShellSort();
//TestSelectSort();
//TestHeapSort();
TestOP();
return 0;
}
2、希尔排序
希尔排序法又称缩小增量法。
基本思想:先选定一个整数,把待排序文件中所有记录分成个组,所有距离为的记录分在同一组内,并对每一组内的记录进行排序。然后,取,重复上述分组和排序的工作。当到达=1时,所有记录在统一组内排好序。
希尔排序的特性总结:
- 希尔排序是对直接插入排序的优化。
- 当gap > 1时都是预排序,目的是让数组更接近于有序。当gap == 1时,数组已经接近有序的了,这样就会很快。这样整体而言,可以达到优化的效果。我们实现后可以进行性能测试的对比。
- 希尔排序的时间复杂度不好计算,因为gap的取值方法很多,导致很难去计算,因此在好些树中给出的希尔排序的时间复杂度都不固定Sort.c
#include "Sort.h"
void PrintArray(int* a, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d ", a[i]);
}
printf("\n");
}
//希尔排序
// 时间复杂度:O(N^1.3)
void ShellSort(int* a, int n)
{
// gap > 1 预排序
// gap == 1 直接插入排序
int gap = n;
while (gap > 1)
{
gap = gap / 3 + 1;
//gap = gap / 2;
// 间隔为gap的多组并排
for (int i = 0; i < n - gap; ++i)
{
int end = i;
int tmp = a[end + gap];
while (end >= 0)
{
if (tmp < a[end])
{
a[end + gap] = a[end];
end -= gap;
}
else
{
break;
}
}
a[end + gap] = tmp;
}
}
}
void TestShellSort()
{
int a[] = { 54, 38, 96, 23, 15, 72, 60, 45, 83 };
ShellSort(a, sizeof(a) / sizeof(int));
PrintArray(a, sizeof(a) / sizeof(int));
}
test.c
#include <time.h>
#include <stdlib.h>
#include "Sort.h"
void TestOP()
{
srand(time(0));
const int N = 10000;
int* a1 = (int*)malloc(sizeof(int)*N);
int* a2 = (int*)malloc(sizeof(int)*N);
int* a3 = (int*)malloc(sizeof(int)*N);
int* a4 = (int*)malloc(sizeof(int)*N);
int* a5 = (int*)malloc(sizeof(int)*N);
int* a6 = (int*)malloc(sizeof(int)*N);
int* a7 = (int*)malloc(sizeof(int)*N);
for (int i = 0; i < N; ++i)
{
a1[i] = rand();
a2[i] = a1[i];
a3[i] = a1[i];
a4[i] = a1[i];
a5[i] = a1[i];
a6[i] = a1[i];
a7[i] = a1[i];
}
int begin2 = clock();
ShellSort(a2, N);
int end2 = clock();
printf("ShellSort:%d\n", end2 - begin2);
free(a1);
free(a2);
free(a3);
free(a4);
free(a5);
free(a6);
}
int main()
{
// TestInsertSort();
TestShellSort();
//TestSelectSort();
//TestHeapSort();
TestOP();
return 0;
}
时间复杂度:O(N^1.3)–>外循环–每次以gap=gap/3+1走,即while走了log3 N次,内循环—N次,则时间复杂度为O(N*log3 N)约等于O(N^1.3)
选择排序
1、选择排序
遍历一次拿到最大和最小值,最小值往前放,最大值往后放,再将我们区间缩小慢慢进行排序
Sort.c
#include "Sort.h"
void PrintArray(int* a, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d ", a[i]);
}
printf("\n");
}
void Swap(int* px, int* py)
{
int tmp = *px;
*px = *py;
*py = tmp;
}
//选择排序
// 时间复杂度:O(N^2) 最坏
// 最好情况是多少:O(N^2)
void SelectSort(int* a, int n)
{
//排升序
int begin = 0, end = n - 1;
while (begin < end)
{
// 选出 [begin, end]
int mini = begin, maxi = begin;
for (int i = begin; i <= end; ++i)
{
if (a[i] > a[maxi])
maxi = i;
if (a[i] < a[mini])
mini = i;
}
Swap(&a[begin], &a[mini]);
// 如果begin和maxi重叠,需要修正一下新的maxi的位置
if (begin == maxi)
{
maxi = mini;
}
Swap(&a[end], &a[maxi]);
++begin;
--end;
}
}
void TestSelectSort()
{
int a[] = { 154, 38, 96, 23, 15, 72, 60, 45, 83 };
SelectSort(a, sizeof(a) / sizeof(int));
PrintArray(a, sizeof(a) / sizeof(int));
}
test.c
#include <time.h>
#include <stdlib.h>
#include "Sort.h"
void TestOP()
{
srand(time(0));
const int N = 10000;
int* a1 = (int*)malloc(sizeof(int)*N);
int* a2 = (int*)malloc(sizeof(int)*N);
int* a3 = (int*)malloc(sizeof(int)*N);
int* a4 = (int*)malloc(sizeof(int)*N);
int* a5 = (int*)malloc(sizeof(int)*N);
int* a6 = (int*)malloc(sizeof(int)*N);
int* a7 = (int*)malloc(sizeof(int)*N);
for (int i = 0; i < N; ++i)
{
a1[i] = rand();
a2[i] = a1[i];
a3[i] = a1[i];
a4[i] = a1[i];
a5[i] = a1[i];
a6[i] = a1[i];
a7[i] = a1[i];
}
int begin3 = clock();
SelectSort(a3, N);
int end3 = clock();
free(a1);
free(a2);
free(a3);
free(a4);
free(a5);
free(a6);
}
int main()
{
// TestInsertSort();
//TestShellSort();
TestSelectSort();
//TestHeapSort();
TestOP();
return 0;
}
时间复杂度:O(N^2)–每次都选择数字出来排序,N,N-1,N-2…
2、 堆排序
之前在树那里写过–升序–大堆
Sort.c
#include "Sort.h"
void PrintArray(int* a, int n)
{
for (int i = 0; i < n; ++i)
{
printf("%d ", a[i]);
}
printf("\n");
}
void AdjustDown(int* a, int n, int parent)
{
int child = parent * 2 + 1;
while (child < n)
{
if (child + 1 < n && a[child + 1] > a[child])
{
++child;
}
if (a[child] > a[parent])
{
Swap(&a[child], &a[parent]);
parent = child;
child = parent * 2 + 1;
}
else
{
break;
}
}
}
// 堆排序 O(N*logN)
void HeapSort(int* a, int n)
{
// 建大堆 O(N)
for (int i = (n - 1 - 1) / 2; i >= 0; --i)
{
AdjustDown(a, n, i);
}
// O(N*logN)
int end = n - 1;
while (end > 0)
{
Swap(&a[0], &a[end]);
AdjustDown(a, end, 0);
--end;
}
}
void TestHeapSort()
{
int a[] = { 54, 38, 96, 23, 15, 72, 60, 45, 83 };
HeapSort(a, sizeof(a) / sizeof(int));
PrintArray(a, sizeof(a) / sizeof(int));
}
test.c
#include <time.h>
#include <stdlib.h>
#include "Sort.h"
void TestOP()
{
srand(time(0));
const int N = 10000;
int* a1 = (int*)malloc(sizeof(int)*N);
int* a2 = (int*)malloc(sizeof(int)*N);
int* a3 = (int*)malloc(sizeof(int)*N);
int* a4 = (int*)malloc(sizeof(int)*N);
int* a5 = (int*)malloc(sizeof(int)*N);
int* a6 = (int*)malloc(sizeof(int)*N);
int* a7 = (int*)malloc(sizeof(int)*N);
for (int i = 0; i < N; ++i)
{
a1[i] = rand();
a2[i] = a1[i];
a3[i] = a1[i];
a4[i] = a1[i];
a5[i] = a1[i];
a6[i] = a1[i];
a7[i] = a1[i];
}
int begin4 = clock();
HeapSort(a4, N);
int end4 = clock();
printf("HeapSort:%d\n", end4 - begin4);
free(a1);
free(a2);
free(a3);
free(a4);
free(a5);
free(a6);
}
int main()
{
// TestInsertSort();
//TestShellSort();
//TestSelectSort();
TestHeapSort();
TestOP();
return 0;
}
时间复杂度:O(N*logN),建堆时间复杂度为O(N)
归并排序
Sort.c
void _MergeSort(int* a, int left, int right, int* tmp)
{
//区间只有一个数的时候返回
if (left >= right)
return;
//分解区间
int mid = (right + left) / 2;
// [left, mid][mid+1, right]
_MergeSort(a, left, mid, tmp);
_MergeSort(a, mid + 1, right, tmp);
// 归并
int begin1 = left, end1 = mid;
int begin2 = mid + 1, end2 = right;
int index = left;
//其中一个走完了就结束
while (begin1 <= end1 && begin2 <= end2)
{
if (a[begin1] < a[begin2])
{
tmp[index++] = a[begin1++];
}
else
{
tmp[index++] = a[begin2++];
}
}
//最后-考虑谁里面还有数据就把它放到tmp中的最后一个数据
while (begin1 <= end1)
{
tmp[index++] = a[begin1++];
}
while (begin2 <= end2)
{
tmp[index++] = a[begin2++];
}
// 归并后的结果,拷贝回到原数组
for (int i = left; i <= right; ++i)
{
a[i] = tmp[i];
}
}
void MergeSort(int* a, int n)
{
int* tmp = (int*)malloc(sizeof(int)*n);
_MergeSort(a, 0, n - 1, tmp);
free(tmp);
}
void TestMergeSort()
{
//int a[] = { 6, 1, 2, 7, 9, 3, 4, 5, 10, 8 };
int a[] = { 10, 6, 7, 1, 3, 9, 4, 2 };
PrintArray(a, sizeof(a) / sizeof(int));
MergeSort(a, sizeof(a) / sizeof(int));
PrintArray(a, sizeof(a) / sizeof(int));
}
计数排序
void CountSort(int* a, int n)
{
//找出最大最小的数来开辟空间
int min = a[0], max = a[0];
for (int i = 1; i < n; ++i)
{
if (a[i] < min)
{
min = a[i];
}
if (a[i] > max)
{
max = a[i];
}
}
int range = max - min + 1;
int* count = (int*)calloc(range, sizeof(int));
// 统计次数
//根据最大最小值去开辟的空间所以下标要减去最小值才是真正的下标
for (int i = 0; i < n; ++i)
{
count[a[i] - min]++;
}
// 根据count数组排序
int i = 0;
for (int j = 0; j < range; ++j)
{
while (count[j]--)
{
a[i++] = j + min;
}
}
}
void TestCountSort()
{
//int a[] = { 6, 1, 2, 7, 9, 3, 4, 5, 10, 8 };
int a[] = { 10, 6, 7, 1, 3, 9, 4, 2, 2, 3, 6, 7, 4, 10 };
PrintArray(a, sizeof(a) / sizeof(int));
CountSort(a, sizeof(a) / sizeof(int));
PrintArray(a, sizeof(a) / sizeof(int));
}