当前位置:网站首页>【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;
}
运行后截图:
边栏推荐
- Day37 JS note motion function 2021.10.11
- Bisection (integer bisection and floating point bisection)
- day29 js笔记 2021.09.23
- 【C语言】关于scanf()与scanf_s()的一些问题
- Analyze whether there is duplicate data in the list and repeat it several times
- 【C语言】如何很好的实现复数类型
- [Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
- 【JS】斐波那契数列实现(递归与循环)
- Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
- Research on personalized product search
猜你喜欢

Array method in JS 2021.09.18

What method is required for word, PDF and txt files to realize full-text content retrieval?

开源项目维权成功案例: spug 开源运维平台成功维权

day36 js笔记 ECMA6语法 2021.10.09
![Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed](/img/9a/97813f5ac4d7c15711891cff25b9dd.jpg)
Connectionreseterror: [winerror 10054] the remote host forced an existing connection to be closed

水果FL Studio/Cubase/Studio one音乐宿主软件对比

Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly

Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released

SEO优化的许多好处是与流量有直接关系

js中的class类模式及语法 2021.11.10
随机推荐
Leetcode 48. 旋转图像(可以,已解决)
Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques
How to deploy the software testing environment?
Multi dimensional monitoring: the data base of intelligent monitoring
案例驱动 :从入门到掌握Shell编程详细指南
【sciter】: sciter-fs模块扫描文件API的使用及其注意细节
recent developments
【C语言】NextDay问题
3. seat number
Join hands with cigent: group alliance introduces advanced network security protection features for SSD master firmware
day37 js笔记 运动函数 2021.10.11
day28 严格模式、字符串 js 2021.09.22
Prepare for Jin San Yin Si I. testers without experience in automated testing projects should look at it quickly
Research on personalized product search
Web3安全连载(3) | 深入揭秘NFT钓鱼流程及防范技巧
Zero basic C language (I)
Characteristics of solar wireless LED display
Which programming language will attract excellent talents?
Deployment and optimization of vsftpd service
2022 open source software security status report: over 41% of enterprises do not have enough confidence in open source security