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 ...
随机推荐
- LeetCode - Reorganize String
Given a string S, check if the letters can be rearranged so that two characters that are adjacent to ...
- 更改MySQL数据库的编码为utf8mb4
原文:http://blog.csdn.net/woslx/article/details/49685111 utf-8编码可能2个字节.3个字节.4个字节的字符,但是MySQL的utf8编码只支持3 ...
- CH4907 作诗
题意 4907 作诗 0x49「数据结构进阶」练习 描述 神犇SJY虐完HEOI之后给傻×LYD出了一题:SHY是T国的公主,平时的一大爱好是作诗.由于时间紧迫,SHY作完诗之后还要虐OI,于是SHY ...
- MongoDB 副本集 pymongo使用
搭建没有仲裁节点的副本集,推荐使用 2.清空node2的db文件夹 和 log 文件夹 rm -rf /var/lib/mongod/* rm -rf /var/log/mongod/* 3.修改no ...
- <------------------字节流--------------------->
字节流: 输入和输出:1.参照物都是java程序来惨遭 2.Input输入,持久化上的数据---->内存 3.Output输出,内存--->硬盘 字节输出流: OutputStream: ...
- 存储-实例-ibm v3700
raid5总容量计算(n-1)*最小盘容量 RAID0:N块盘组成,逻辑容量为N块盘容量之和:RAID1:两块盘组成,逻辑容量为一块盘容量:RAID3:N+1块盘组成,逻辑容量为N块盘容量之和:RAI ...
- Java之SimpleDateFormat日期格式转换(Date 和 String 类型之间的转换)
SimpleDateFormat : 可以选择任何用户定义的日期-时间格式的模式 "yyyy-MM-dd HH:mm:ss:SSS"1.格式化:Date -->Stri ...
- Zookeeper基本数据模型
一.Zookeeper基本数据模型 是一个树形结构,类似于前端开发中的tree.js组件 zk的数据模型也可以理解为linux/unix的文件目录 /usr/local/... 每一个节点称之为zn ...
- java集合与包装类
一.集合概述 1 为什么需要使用集合? 引入案例:存储每天产生的新闻. 是要解决数组的局限性(定长),由于数组定长,可能会导致内存浪费或者内存不够. 需要一种技术:能够根据数据量而动态伸缩内存空间一种 ...
- Keras/Tensorflow训练逻辑研究
Keras是什么,以及相关的基础知识,这里就不做详细介绍,请参考Keras学习站点http://keras-cn.readthedocs.io/en/latest/ Tensorflow作为backe ...