排序
稳定排序:值相同的数排序后相对位置不变。
内部排序:将所有数据全部加载到内存。
稳定排序(插入、冒泡、归并)
插入 O ( N 2 ) O(N^2) O(N2) 比较与交换
已排序区,待排序区
将待排序的第一个元素向前插入到已排序区。
冒泡排序 O ( N 2 ) O(N^2) O(N2) 部分排序较好
在位排序区,每一次排序都会找出最大值并将其放到最后一个位置即已排的第一个元素。
冒泡排序的小优化:当一次遍历没有发生交换时,即排序完成。
归并排序 O ( N l o g ( N ) ) O(Nlog(N)) O(Nlog(N)) 分治 排序 合并 外部排序
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define swap(a, b) {\
a ^= b;\
b ^= a;\
a ^= b;\
}
#define TEST(arr, n, func, args...) { \
int *num = (int *)malloc(sizeof(int) * n);\
memcpy(num, arr, sizeof(int) * n);\
output(num, n);\
printf("%s = ", #func);\
func(args);\
output(num, n);\
free(num);\
printf("\n");\
}
void insert_sort(int *num, int n) {
for (int i = 1; i < n; i++) {
for (int j = i; j > 0 && num[j] < num[j - 1]; j--) {
swap(num[j], num[j - 1]);
}
}
}
void bubble_sort(int *num, int n) {
int times = 1;
for (int i = 1; i < n && times; i++) {
times = 0;
for (int j = 0; j < n - i; j++) {
if (num[j] <= num[j + 1]) continue;
swap(num[j], num[j +1]);
times = 1;
}
}
}
void merge_sort(int *num, int l, int r) {
if (r - l <= 1) {
if (r -l == 1 && num[r] < num[l]) {
swap(num[r], num[l]);
}
return ;
}
int mid = (l + r) >>1;
merge_sort(num, l, mid);
merge_sort(num, mid + 1, r);
int *temp = (int *)malloc(sizeof(int) * (r - l + 1));
int p1 = l, p2 = mid + 1, k = 0;
while (p1 <= mid || p2 <= r) {
if (p2 > r || (p1 <= mid && num[p1] <= num[p2])) {
temp[k++] = num[p1++];
} else {
temp[k++] = num[p2++];
}
}
memcpy(num + l, temp, sizeof(int) * (r - l + 1));
free(temp);
}
void randint(int *num, int n) {
while (n--) num[n] = rand() % 100;
}
void output(int *num, int n) {
printf("[");
for (int i = 0; i < n; i++) {
i && printf(" ");
printf("%d", num[i]);
}
printf("]\n");
}
int main() {
srand(time(0));
#define MAX_N 20
int arr[MAX_N];
randint(arr, MAX_N);
TEST(arr, MAX_N, insert_sort, num, MAX_N);
TEST(arr, MAX_N, bubble_sort, num, MAX_N);
TEST(arr, MAX_N, merge_sort, num, 0, MAX_N - 1);
#undef MAX_N
return 0;
}
非稳定排序 (选择、 快排)
选择排序 O ( N 2 ) O(N^2) O(N2)
每一轮从待排区选择一个最小元素放到已排区的尾部
快速排序额 O ( N l o g ( N ) ) O(Nlog(N)) O(Nlog(N))时间复杂度不稳定 可以退化为 O ( N 2 ) O(N^2) O(N2)
选择基数
以基数为分割
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#define swap(a, b) {\
__typeof(a) __temp = a;\
a = b; b = __temp;\
}
#define TEST(arr, n, func, args...) { \
int *num = (int *)malloc(sizeof(int) * n);\
memcpy(num, arr, sizeof(int) * n);\
output(num, n);\
printf("%s = ", #func);\
func(args);\
output(num, n);\
free(num);\
printf("\n");\
}
void selsct_sort(int *num, int n){
for (int i = 0; i < n - 1; i++) {
int ind = i;
for (int j = i + 1; j < n; j++) {
if (num[j] < num[ind]) ind = j;
}
swap(num[ind], num[i]);
}
return ;
}
void quick_sort(int *num, int l, int r) {
if (l > r) return ;
int x = l, y = r, z= num[x];
while (x < y) {
while (x < y && num[y] > z) y--;
if (x < y) num[x++] = num[y];
while (x < y && num[x] < z) x++;
if (x < y) num[y--] = num[x];
}
num[x] = z;
quick_sort(num, l, x - 1);
quick_sort(num, x + 1, r);
return ;
}
void randint(int *num, int n) {
while (n--) num[n] = rand() % 100;
}
void output(int *num, int n) {
printf("[");
for (int i = 0; i < n; i++) {
i && printf(" ");
printf("%d", num[i]);
}
printf("]\n");
}
int main() {
srand(time(0));
#define MAX_N 20
int arr[MAX_N];
randint(arr, MAX_N);
TEST(arr, MAX_N, selsct_sort, num, MAX_N)
TEST(arr, MAX_N, quick_sort, num, 0, MAX_N - 1);
#undef MAX_N
return 0;
}
快排的优化
优化前
#include<stdio.h>
#define MAX_N 1000000
void quick_sort(int *num, int l, int r) {
if (l > r) return ;
int x = l, y = r, z= num[x];
while (x < y) {
while (x < y && num[y] > z) y--;
if (x < y) num[x++] = num[y];
while (x < y && num[x] < z) x++;
if (x < y) num[y--] = num[x];
}
num[x] = z;
quick_sort(num, l, x - 1);
quick_sort(num, x + 1, r);
return ;
}
void init_arr(int *arr, int n) {
for (int i = 0; i < n; i++){
arr[i] = n - i;
}
return ;
}
void output(int *arr, int n) {
for (int i = 0; i < n; i++) {
i && printf(" ");
printf("%d", arr[i]);
}
return ;
}
int main() {
int arr[MAX_N] = {0};
init_arr(arr, MAX_N - 1);
output(arr, MAX_N);
return 0;
}
优化后
#include<stdio.h>
#define MAX_N 1000000
#define swap(a, b) {\
__typeof(a) __temp = a;\
a = b; b = __temp;\
}
void quick_sort(int *num, int l, int r) {
while (l < r) {
int x = l, y = r, z= num[(l + r) >> 1];
do {
while(num[x] < z) x++;
while(num[y] > z) y--;
if (x <= y) {
swap(num[x], num[y]);
x++; y--;
}
}while (x <= y);
quick_sort(num, l, y);
l = x;
}
return ;
}
void init_arr(int *arr, int n) {
for (int i = 0; i < n; i++){
arr[i] = n - i;
}
return ;
}
void output(int *arr, int n) {
for (int i = 0; i < n; i++) {
i && printf(" ");
printf("%d", arr[i]);
}
return ;
}
int main() {
int arr[MAX_N] = {0};
init_arr(arr, MAX_N - 1);
output(arr, MAX_N);
return 0;
}