C语言强化——排序
1、完成堆排,对比堆排和qsort在排序1亿数的时间差异
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define N 100000000
#define SWAP(a,b) {int tmp=a; a=b; b=tmp;}
//单次调整
void adjustMaxHeap(int *arr, int adjustPos, int arrlen) {
int parent = adjustPos;
int child = 2 * parent + 1;
while (child < arrlen) {
if (child + 1 < arrlen && arr[child + 1] > arr[child]) {
child++;
}
if (arr[child] > arr[parent]) {
SWAP(arr[parent], arr[child]);
parent = child;
child = parent * 2 + 1;
}
else {
break;
}
}
}
void HeapSort(int *arr) {
int i;
for (int i = N / 2 - 1;i >= 0;i--) {
adjustMaxHeap(arr, i, N);
}
SWAP(arr[0], arr[N - 1]);
for (int i = N - 1;i > 1;i--) {
adjustMaxHeap(arr, 0, i);
SWAP(arr[0], arr[i - 1]);
}
}
void printArray(int *a) {
for (int i = 0;i < N;i++) {
printf("%3d ", a[i]);
}
printf("\n");
}
void compare(const void * a, const void * b) {
int *p1 = (int*)a;
int *p2 = (int*)b;
return *p1 - *p2;
}
int main() {
int i;
time_t start, end;
int *a = (int*)calloc(N, sizeof(int));
srand(time(NULL));
for (i = 0;i < N;++i) {
a[i] = rand() % 100;
}
//printArray(a, N);
start = time(NULL);
//HeapSort(a);
qsort(a, N, sizeof(int), compare);
end = time(NULL);
//printArray(a);
printf("use time = %d\n", end - start);
return 0;
}
堆排时间:

qsort()时间:

2、完成计数排序,对比计数排序和堆排在排序1亿数的时间差异
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define N 100000000
#define SWAP(a,b) {int tmp=a; a=b; b=tmp;}
#define R 100
//单次调整
void adjustMaxHeap(int *arr, int adjustPos, int arrlen) {
int parent = adjustPos;
int child = 2 * parent + 1;
while (child < arrlen) {
if (child + 1 < arrlen && arr[child + 1] > arr[child]) {
child++;
}
if (arr[child] > arr[parent]) {
SWAP(arr[parent], arr[child]);
parent = child;
child = parent * 2 + 1;
}
else {
break;
}
}
}
void HeapSort(int *arr) {
int i;
for (int i = N / 2 - 1;i >= 0;i--) {
adjustMaxHeap(arr, i, N);
}
SWAP(arr[0], arr[N - 1]);
for (int i = N - 1;i > 1;i--) {
adjustMaxHeap(arr, 0, i);
SWAP(arr[0], arr[i - 1]);
}
}
void printArray(int *a) {
for (int i = 0;i < N;i++) {
printf("%3d ", a[i]);
}
printf("\n");
}
void compare(const void * a, const void * b) {
int *p1 = (int*)a;
int *p2 = (int*)b;
return *p1 - *p2;
}
void CountSort(int *arr) {
int *count = (int*)calloc(R, sizeof(int));
int i, j, k;
for (i = 0;i < N;i++) {
count[arr[i]]++;
}
k = 0;
for (i = 0;i < R;i++) {
for (j = 0;j < count[i];j++) {
arr[k] = i;
k++;
}
}
}
int main() {
int i;
time_t start, end;
int *a = (int*)calloc(N, sizeof(int));
srand(time(NULL));
for (i = 0;i < N;++i) {
a[i] = rand() % 100;
}
//printArray(a, N);
start = time(NULL);
//HeapSort(a);
//qsort(a, N, sizeof(int), compare);
CountSort(a);
end = time(NULL);
//printArray(a);
printf("use time = %d\n", end - start);
return 0;
}
计数排序:

堆排:

C语言强化——排序的更多相关文章
- 帮初学者改代码——有多少青春可以挥霍之“c语言 多重排序”
原文:“c语言 多重排序” 原代码: #include<stdio.h> #include<string.h> struct A { char name[100]; int g ...
- go语言的排序和去重
go语言的排序: https://blog.csdn.net/u010983881/article/details/52460998 go语言去重: https://blog.csdn.net/qq_ ...
- C语言实现排序
//C语言版排序#include<stdio.h> #include<stdlib.h> //冒泡排序 void bubleSort(int data[], int n); / ...
- go语言的排序和搜索(转载)
http://studygolang.com/articles/1598 go语言的排序和搜索 晚上准备动手写点 go 的程序的时候,想起 go 如何排序的问题.排序 sort 是个基本的操作,当然搜 ...
- GO语言练习:第一个Go语言工程--排序
1.代码 2.编译 3.运行 1.代码框架 /home/fengbo/sorter $ tree . ├── bin ├── pkg ├── readme.txt └── src ├── algori ...
- C语言常用排序全解(转)
目的:重温经典排序思想,并用C语言指针实现排序算法================================================*/ /*====================== ...
- linux / centos 安装SQL Server 2017 设置默认语言与排序规则Chinese_PRC_CI_AS
安装 安装很简单参照官方教程 https://docs.microsoft.com/zh-cn/sql/linux/quickstart-install-connect-red-hat?view=sq ...
- go语言的排序、结构体排序
原文:https://studygolang.com/articles/1598 晚上准备动手写点 go 的程序的时候,想起 go 如何排序的问题.排序 sort 是个基本的操作,当然搜索 searc ...
- [python学习] 语言基础—排序函数(sort()、sorted()、argsort()函数)
python的内建排序函数有 sort.sorted两个. 1.基础的序列升序排序直接调用sorted()方法即可 ls = list([5, 2, 3, 1, 4]) new_ls = sorted ...
随机推荐
- 《DSP using MATLAB》Problem 5.8
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...
- Spring事务失效的原因
http://blog.csdn.net/paincupid/article/details/51822599 Spring事务失效的原因 5种大的原因 如使用mysql且引擎是MyISAM,则事务会 ...
- pread和pwrite函数
先来介绍pread函数 [root@bogon mycode]# cat test.c #include<stdio.h> #include<stdlib.h> #includ ...
- The key of real time embedded system
对于实时嵌入式系统来说,最重要的是每一个进程所需时间的可检测性,可预测性.要不你的实时性是没有办法保证的.有些时候你对一些没有从事过嵌入式开发的人谈这个进程(TASK)设计是按8ms被调度一次,他们会 ...
- 替换元素(replace,replace_if,replace_copy,replace_copy_if)
replace 审阅range中的每个元素,把old_value替换为new_value template <class ForwardIterator,class T> void rep ...
- Vision GUI programming products
Matrox Design Assistant Tutorial https://www.youtube.com/watch?v=QnE5heA_yWQ merlic https://www.yout ...
- Docker容器挂载主机目录访问出现Permission denied的解决办法
Docker挂载主机目录,访问相应的文件出现Premission denied的权限访问问题 挂载后,查看相应的文件出现如下的提示: [root@ba471da26d07 soft]# lsls: c ...
- ElasticeSearch(五)分布式索引架构
关于分布式架构 首先将ES默认每个索引是5个分片,这样做得目的是两个一个索引的时候速度更快(将数据写到小分片的尾部比写入大分片尾部更加快):另外一个是当数据量达到一定程度之后,分片查询,在汇总(sca ...
- hadoop append 追加文件错误
java.io.IOException:Failed to replace a bad datanode on the existing pipeline due to no more good da ...
- Tensorflow之基于MNIST手写识别的入门介绍
Tensorflow是当下AI热潮下,最为受欢迎的开源框架.无论是从Github上的fork数量还是star数量,还是从支持的语音,开发资料,社区活跃度等多方面,他当之为superstar. 在前面介 ...