当前位置:网站首页>【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;
}
运行后截图:
边栏推荐
- Web3安全连载(3) | 深入揭秘NFT钓鱼流程及防范技巧
- 6. calculation index
- Excel import / export convenience tool class
- Pre parsing, recursive functions and events in day25 JS 2021.09.16
- Difference (one dimension)
- Research on personalized product search
- day31 js笔记 DOM下 2021.09.26
- 2018 joint examination of nine provinces & Merging of line segment trees
- Mutual conversion between mytipartfile and file
- AGCO AI frontier promotion (2.16)
猜你喜欢

js中的class类模式及语法 2021.11.10

Redis 原理 - List

Unity screenshot function

【C语言】如何产生正态分布或高斯分布随机数

建立自己的网站(18)

Using soapUI to obtain freemaker's FTL file template

day32 js笔记 事件(上)2021.09.27

Share the easy-to-use fastadmin open source system - practical part

Recommended practice sharing of Zhilian recruitment based on Nebula graph

day34 js笔记 正则表达式 2021.09.29
随机推荐
Class pattern and syntax in JS 2021.11.10
Deployment and optimization of vsftpd service
Join hands with cigent: group alliance introduces advanced network security protection features for SSD master firmware
Simulation of the Saier lottery to seek expectation
Database Series: is there any way to seamlessly upgrade the business tables of the database
【C语言】NextDay问题
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
Convert black mask picture to color annotation file
Unity屏幕截图功能
AcWing 609. Salary (implemented in C language)
Characteristics of solar wireless LED display
js中的class类模式及语法 2021.11.10
SEO优化的许多好处是与流量有直接关系
來吧元宇宙,果然這熱度一時半會兒過不去了
fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
Pre parsing, recursive functions and events in day25 JS 2021.09.16
Interview skills for interview steps
Day23 JS notes 2021.09.14
Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
day36 js笔记 ECMA6语法 2021.10.09