当前位置:网站首页>Unstable sorting (selection, fast)
Unstable sorting (selection, fast)
2022-04-22 21:20:00 【yitahutu79】
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define TEST(arr, n, func, ...) {
\ int *num = (int *) malloc (sizeof(int) * n);\ memcpy(num, arr, sizeof(int) * n);\ output(num,n);\ printf("%s = ", #func);\ func(__VA_ARGS__);\ output(num,n);\ free(num);\ printf("\n");\ }
#define swap(a,b){
\ int temp = a;\ a = b;\ b = temp;\ }
void output(int* arr, int n) {
printf("[");
for (int i = 0; i < n; i++) {
i&& printf(", ");
printf("%d",arr[i]);
}
printf("]\n");
return;
}
void select_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[i], num[ind]);
}
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* arr, int n) {
while (n--)
arr[n] = rand() % 100;
return;
}
int main() {
srand(time(0));
#define MAX_N 20
int arr[MAX_N];
randint(arr, MAX_N);
TEST(arr, MAX_N, select_sort, num, MAX_N);
TEST(arr, MAX_N, quick_sort, num, 0, MAX_N - 1);
return 0;
}
版权声明
本文为[yitahutu79]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204222117067451.html
边栏推荐
- 2022 question bank and online simulation examination of hoisting machinery command certificate
- 345-Leetcode 赎金信
- Thread communication mechanism: shared memory VS message passing
- Time report filling rules
- Hdlbits (10) learning notes - finite state machine (fsm1 - lemmings4)
- QT使用windeployqt.exe打包程序
- Question bank and simulation examination of work permit for safety management personnel of hazardous chemical business units in 2022
- M5增强版双系统套餐包含哪些重要配件,该怎么选?
- Count the number of characters C
- 重返天梯-L2-025 分而治之 (25 分)
猜你喜欢

jmeter资料及软件

Based on paogd_ HW1's pop-up ball - simple modeling, interpolation animation

shell脚本中解决SCP命令免密登录

jmeter视频教学课程

344-Leetcode 二叉树的所有路径

MySQL is still suitable for Silicon Valley courses (I)

最新版去水印+外賣cps小程序源碼

Openvx's immediate mode and graph mode and examples

网传华为面试题:“800kg的牛如何过承重700kg的桥?”你怎样思考问题,就怎样过一生

Design and implementation of Snake game based on OpenGL
随机推荐
Navicat for MySQL 连接 Docker中运行的Mysql出现1251- Client does not support authentication protocol 错误
Openvx's immediate mode and graph mode and examples
FreeModbus快速入门指南
OpenVX 的 立即模式(immediate mode)和图模式(graph mode)和示例讲解
SDF accelerate trace
cuda10. 2. Install torch 1 nine
Openvx - read and write image file [pgm format] as VX_ Image objects, and write operations
select distinct语句 去重
BLE---Advertisement data format & service
k8s部署mysql
Study record of the 8th week of graduation project
ImportError: cannot import name ‘get_ all_ providers‘ from ‘onnxruntime. capi._ pybind_ state‘
2022 G3 boiler water treatment national question bank and online simulation examination
Selenium web自动化测试
leetcode-470. Rand10() is implemented with rand7()
Shandong University project training raspberry pie promotion plan phase II (IX) inheritance and innovation
[data cleaning and drawing] simple application of dataframe
Selenium_Webdriver视频自动化脚本分享
Thread communication mechanism: shared memory VS message passing
All paths of 344 leetcode binary tree