当前位置:网站首页>【C语言】随机数文件对其进行三种排序方法
【C语言】随机数文件对其进行三种排序方法
2022-06-28 11:36:00 【贾璞】
大致内容:生成一个随机数文件,并对其加载至堆区,对其进行冒泡,插入,选择排序,快速排序请到我的博客中查找。
三种排序思想不再赘述,实现起来也非常简单,在此直接上代码!
编译环境:Ubuntu18.04 GCC && CLion
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define max 200
//随机数文件创建,含max个值
void creatValue() {
FILE *file = fopen("./value.txt", "w");
if (!file)
return;
//随机数种子导入
srand((unsigned) time(NULL));
int arr[max] = {
0};
//写入文件
for (int i = 0; i < max; ++i) {
arr[i] = rand() % 300;
fprintf(file, "%d\n", arr[i]);
}
printf("创建成功\n");
fclose(file);
}
//交换函数
void swapValue(int *value1, int *value2) {
int temp = *value1;
*value1 = *value2;
*value2 = temp;
}
//将随机数文件导入到堆区中
int *importValue() {
FILE *file = fopen("./value.txt", "r");
if (!file)
return NULL;
//开辟对应大小堆空间
int *arr = (int *) malloc(sizeof(int) * max);
//导入堆中
for (int i = 0; i < max; ++i) {
fscanf(file, "%d\n", &arr[i]);
}
return arr;
}
//打印函数,一行十个
void printValue(int *arr) {
for (int i = 0; i < max; ++i) {
printf("%d\t", *(arr + i));
if (!((i + 1) % 10))
printf("\n");
}
printf("\n");
}
//冒泡排序
void bubbleSort(int *arr) {
for (int i = 0; i < max - 1; ++i) {
for (int j = 0; j < max - 1 - i; ++j) {
if (arr[j] > arr[j + 1]) {
swapValue(&arr[j], &arr[j + 1]);
}
}
}
}
//选择排序
void selectionSort(int *arr) {
for (int i = 0; i < max - 1; ++i) {
int temp = i;
for (int j = i + 1; j < max; ++j) {
if (arr[j] < arr[temp])
temp = j;
}
swapValue(&arr[i], &arr[temp]);
}
}
//插入排序
void insertionSort(int *arr) {
for (int i = 1; i < max; i++) {
int temp = arr[i];
int j;
for (j = i; j > 0 && arr[j - 1] > temp; j--) {
arr[j] = arr[j - 1];
}
arr[j] = temp;
}
}
void test01() {
//creatValue();
int *p = importValue();
//排序前
printValue(p);
printf("--------------------------------------\n");
//排序后
//bubbleSort(p);
//selectionSort(p);
//insertionSort(p);
printValue(p);
//printf("%p\n", p);
}
int main() {
test01();
return EXIT_SUCCESS;
}
运行后截图:
边栏推荐
- SoapUI rookie tutorial
- Database Series: is there any way to seamlessly upgrade the business tables of the database
- day23 js笔记 2021.09.14
- Deployment and optimization of vsftpd service
- 零基础C语言(一)
- Prefix and (one dimension)
- Contract quantitative trading system development | contract quantitative app development (ready-made cases)
- What is DAPP system development and analytical understanding
- Research on personalized product search
- What is the main chain system?
猜你喜欢

Redis 原理 - List

Jetpack Compose Desktop 桌面版本的打包和发布应用

Day31 JS notes DOM 2021.09.26

js中的数组方法 2021.09.18

Swin, three degrees! Eth open source VRT: a transformer that refreshes multi domain indicators of video restoration

Join hands with cigent: group alliance introduces advanced network security protection features for SSD master firmware

Using soapUI to obtain freemaker's FTL file template

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system

Fancy features and cheap prices! What is the true strength of Changan's new SUV?

无法重新声明块范围变量
随机推荐
Day32 JS note event (Part 1) September 27, 2021
Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released
ProCAST finite element casting process simulation software
SEO优化的许多好处是与流量有直接关系
ArrayList源码解析
Day24 JS notes 2021.09.15
Using soapUI to obtain freemaker's FTL file template
Contract quantification system development (construction explanation) - contract quantification system development (source code analysis and ready-made cases)
Redis hash hash type string (5)
MapReduce项目案例1
网页提示此站点不安全解决方案
Class pattern and syntax in JS 2021.11.10
Redis 原理 - List
Chapter 2 do you remember the point, line and surface (2)
Difference (one dimension)
Apache2 configuration denies access to the directory, but can access the settings of the files inside
day39 原型链及页面烟花效果 2021.10.13
【C语言】二叉树的实现及三种遍历
Is it safe to buy stocks and open an account on the account QR code of the CICC securities manager? Ask the great God for help
[sciter]:sciter如何使用i18实现桌面应用多语言切换及其利弊