当前位置:网站首页>【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;
}
运行后截图:
边栏推荐
- Cohere, a large model company, completed the round B financing of US $125million
- QML控件类型:TabBar
- AcWing 608. Poor (implemented in C language)
- Interview skills for interview steps
- Characteristics of solar wireless LED display
- Simple understanding of ThreadLocal
- AGCO AI frontier promotion (2.16)
- 无法重新声明块范围变量
- Ali three sides: what is the difference between using on or where in the left join associated table and the condition
- Web3安全连载(3) | 深入揭秘NFT钓鱼流程及防范技巧
猜你喜欢

ArrayList源码解析

.NET混合开发解决方案24 WebView2对比CefSharp的超强优势

Characteristics of solar wireless LED display

Day39 prototype chain and page Fireworks Effect 2021.10.13

纯纯大怨种!那些年被劝退的考研专业

For example, the visual appeal of the live broadcast of NBA Finals can be seen like this?

Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques

js中的数组方法 2021.09.18

Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
随机推荐
Many benefits of SEO optimization are directly related to traffic
day30 js笔记 BOM和DOM 2021.09.24
AGCO AI frontier promotion (2.16)
day37 js笔记 运动函数 2021.10.11
[sciter]: how sciter uses i18 to realize multi language switching of desktop applications and its advantages and disadvantages
Share the easy-to-use fastadmin open source system - practical part
Fruit FL studio/cubase/studio one music host software comparison
day39 原型链及页面烟花效果 2021.10.13
Multi dimensional monitoring: the data base of intelligent monitoring
Ali three sides: what is the difference between using on or where in the left join associated table and the condition
Fancy features and cheap prices! What is the true strength of Changan's new SUV?
Simulation of the Saier lottery to seek expectation
2. single digit statistics
Which programming language will attract excellent talents?
【北京航空航天大学】考研初试复试资料分享
【JS】斐波那契数列实现(递归与循环)
SoapUI rookie tutorial
Characteristics of solar wireless LED display
SEO优化的许多好处是与流量有直接关系
day32 js笔记 事件(上)2021.09.27