当前位置:网站首页>[C language] three sorting methods for random number files
[C language] three sorting methods for random number files
2022-06-28 12:16:00 【Jia Pu】
General content : Generate a random number file , And load it into the heap , Bubble it , Insert , Selection sort , Quick sort please go to my blog to find .
The three sorting ideas will not be repeated , It's also very simple to implement , Here directly on the code !
Compile environment :Ubuntu18.04 GCC && CLion
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define max 200
// Random number file creation , contain max It's worth
void creatValue() {
FILE *file = fopen("./value.txt", "w");
if (!file)
return;
// Random number seed import
srand((unsigned) time(NULL));
int arr[max] = {
0};
// write file
for (int i = 0; i < max; ++i) {
arr[i] = rand() % 300;
fprintf(file, "%d\n", arr[i]);
}
printf(" Create success \n");
fclose(file);
}
// Exchange function
void swapValue(int *value1, int *value2) {
int temp = *value1;
*value1 = *value2;
*value2 = temp;
}
// Import the random number file into the heap
int *importValue() {
FILE *file = fopen("./value.txt", "r");
if (!file)
return NULL;
// Open up heap space of corresponding size
int *arr = (int *) malloc(sizeof(int) * max);
// Import into heap
for (int i = 0; i < max; ++i) {
fscanf(file, "%d\n", &arr[i]);
}
return arr;
}
// Print function , Ten in a row
void printValue(int *arr) {
for (int i = 0; i < max; ++i) {
printf("%d\t", *(arr + i));
if (!((i + 1) % 10))
printf("\n");
}
printf("\n");
}
// Bubble sort
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]);
}
}
}
}
// Selection sort
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]);
}
}
// Insertion sort
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();
// Before ordering
printValue(p);
printf("--------------------------------------\n");
// After ordering
//bubbleSort(p);
//selectionSort(p);
//insertionSort(p);
printValue(p);
//printf("%p\n", p);
}
int main() {
test01();
return EXIT_SUCCESS;
}
Post run screenshot :
边栏推荐
- AsyncTask经验小结
- Day34 JS notes regular expression 2021.09.29
- Sha256 encryption tool class
- 多维度监控:智能监控的数据基础
- 1. print hourglass
- ByteV搭建动态数字孪生网络安全平台----助力网络安全发展
- 已知两个点和中间一个比例的点,求该点坐标
- FTP protocol for Wireshark packet capture analysis
- In less than an hour, apple destroyed 15 startups
- 2018 joint examination of nine provinces & Merging of line segment trees
猜你喜欢

已知两个点和中间一个比例的点,求该点坐标

Still using simpledateformat for time formatting? Be careful that the project collapses!

Ali three sides: what is the difference between using on or where in the left join associated table and the condition

Convert black mask picture to color annotation file

Leetcode 705. 设计哈希集合
![[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination](/img/06/df5a64441814c9ecfa2f039318496e.jpg)
[Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination

RemoteViews布局和类型限制源码分析

KDD 2022 | 图“预训练、提示、微调”范式下的图神经网络泛化框架

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

Leetcode 48. 旋转图像(可以,已解决)
随机推荐
If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience
Function and principle of remoteviews
Redis 原理 - List
AGCO AI frontier promotion (2.16)
AsyncTask经验小结
JS foundation 10
Prefix and (2D)
【C语言】二叉树的实现及三种遍历
ArrayList源码解析
Necessary for beginners PR 2021 quick start tutorial, PR green screen matting operation method
双缓冲绘图
Day31 JS notes DOM 2021.09.26
Is there a threshold for opening futures accounts? How to open futures accounts safely on the Internet
Three ways to implement LRU cache (recommended Collection)
AcWing 608. Poor (implemented in C language)
Still using simpledateformat for time formatting? Be careful that the project collapses!
Deployment and optimization of vsftpd service
fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
Mutual conversion between mytipartfile and file
NFT card chain game system development DAPP construction technical details