etc
小小的注意点们
- 交换两个变量的值时, 如果使用异或运算符, 需要先判断两个数是否相等
if (a == b) return;
a ^= b;
b = a ^ b;
a ^= b;
取一个数组的中间位置, 应该使用(low + high) / 2, 不适用length / 2, 目的是方便之后可能出现的函数的递归等操作
堆排序
- 对一个整数类型的数组进行升序排序
- 原始数组即为一个二叉堆(左孩子的下标=父亲的下标 * 2 + 1, 右孩子的下标=父亲的下标 * 2 + 2, 孩子父亲的下标=(孩子的下标 - 1) / 2)
- 调整堆, 将堆转换为最大堆(判断if parent < two_children: swap(parent, larger_child))
- 删除原始堆中最后的一个元素(并不是真的删除, 而是该堆的length - 1), 对仍然保留在堆中的元素进行$2的操作
- 对一个整数类型的数组进行升序排序
快速排序
- 递归成立的条件式low < high
Code here
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define ARRAY_PRINT(arr, length) do { \
for (int i = 0; i < length; i++) { \
printf("%d\t", arr[i]); \
} \
} while (0)
#define SWAP(a, b) do { \
if (a == b) break; \
(a) ^= (b); \
(b) = (a) ^ (b); \
(a) ^= (b); \
} while (0)
#define MID(low, high) ((low + high) / 2)
static void quick_sort_recurision(int *a, int low, int high) {
int pivot, i, j;
pivot = low; // select a pivot element
i = low;
j = high;
if (high - low) return;
while (i <= j) {
if (a[i] <= a[pivot] && i <= high) {
i++;
}
else if (a[j] > a[pivot] && j >= low) {
j--;
}
else {
SWAP(a[i], a[j]);
}
}
SWAP(a[j], a[pivot]);
quick_sort_recurision(a, low, j - 1);
quick_sort_recurision(a, j + 1, high);
}
void quick_sort(int *arr, int len) {
if (!arr || len < 0) return;
int low = 0, high = len - 1;
quick_sort_recurision(arr, low, high);
}
#define LENGTH 10
int main() {
srand((int) time(0));
int arr[LENGTH];
for (int i = 0; i < LENGTH; i++) {
arr[i] = rand() % 60;
}
ARRAY_PRINT(arr, LENGTH);
quick_sort(arr, LENGTH);
putchar(10);
ARRAY_PRINT(arr, LENGTH);
return 0;
}
随机推荐
- U-Net: Convolutional Networks for Biomedical Image Segmentation(理解+github代码)
github代码:https://github.com/Chet1996/pytorch-UNet 0 - Abstract 这篇文章是生物学会议ICMICCAI2015的文章,主要针对的是生物影像进 ...
- Binder学习笔记(三)—— binder客户端是如何组织checkService数据的
起点从TestClient.cpp的main函数发起: int main() { sp < IServiceManager > sm = defaultServiceManager(); ...
- 毛玻璃CHBlurEffect
1.将需要加入毛玻璃的UI控件传入接口即可 1.1 .h文件 // // 文 件 名:CHBlurEffect.h // // 版权所有:Copyright © 2018年 leLight. All ...
- 洛谷P2709 BZOJ 3781 小B的询问 (莫队)
题目描述 小B有一个序列,包含N个1~K之间的整数.他一共有M个询问,每个询问给定一个区间[L..R],求Sigma(c(i)^2)的值,其中i的值从1到K,其中c(i)表示数字i在[L..R]中的重 ...
- 配置IIS使用Python 与常见问题解决
打开IIS管理器 选择功能视图,然后选择ISAPI和CGI限制 打开后,在右侧操作,点击添加,会出现下图所示 按图中提示填写相应部分,在选择路径时,默认可能是dll文件,改成全部文件即可,然后再选择p ...
- P2264 情书 Trie匹配
\(\color{#0066ff}{题目描述}\) 为了帮助CYY,我们定义一个量化情书好坏的标准感动值.判断感动值的方法如下: 1.在情书的一句话中若含有给定词汇列表中的特定单词,则感动值加1,但每 ...
- json几种读取方式,ArrayList循环读取【转】
在之前写过提取json数据格式的文章,这次对jmeter读取json数据格式进行整理. 举例一个接口的response 格式如下: { "data" : { "devic ...
- 利用DSB2017冠军开源代码为LUNA16生成mask
代码地址:https://github.com/lfz/DSB2017 先展示下生成的mask与真实mask subset9 subset8 subset7 subset6 subset5 subse ...
- 2-28 if...else
- 读经典——《CLR via C#》(Jeffrey Richter著) 笔记_8.1实例构造器和类A
public class SomeType { } //等价于 public class SomeType { public SomeType():base(){} } [解释]如果定义的类没有显示定 ...