当前位置:网站首页>[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 :
边栏推荐
- AcWing 608. Poor (implemented in C language)
- recent developments
- 建立自己的网站(18)
- fatal: unsafe repository (‘/home/anji/gopath/src/gateway‘ is owned by someone else)
- URL append parameter method, considering #$ Situation of
- Self use demo of basic component integration of fluent
- [Beijing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
- On the output representation of bidirectional LSTM in pytoch
- Zero basic C language (I)
- Many benefits of SEO optimization are directly related to traffic
猜你喜欢

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

【C语言】随机数文件对其进行三种排序方法

Leetcode 705. 设计哈希集合
![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

Android应用安全之JNI混淆

Day30 JS notes BOM and DOM 2021.09.24
![[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

Day37 JS note motion function 2021.10.11

In less than an hour, apple destroyed 15 startups

Practice and Thinking on the architecture of a set of 100000 TPS im integrated message system
随机推荐
Deployment and optimization of vsftpd service
Vivo手机的权限管理
Sha256 encryption tool class
If you want to change to software testing, how can you package your resume as a test engineer with 1 year of work experience
Day28 strict mode, string JS 2021.09.22
Day31 JS notes DOM 2021.09.26
. Net hybrid development solution 24 webview2's superior advantages over cefsharp
Is there a threshold for opening futures accounts? How to open futures accounts safely on the Internet
Chendanqi, Fang Fei, guquanquan and Li Bo won the prize, and the list of Sloan research award in 2022 was released
Unity加载设置:Application.backgroundLoadingPriority
Web3 security serials (3) | in depth disclosure of NFT fishing process and prevention techniques
【vi/vim】基本使用及命令汇总
What is data compliance? How to achieve data compliance?
Daily practice of C language - day 3: calculate the number of occurrences of sub strings of strings
How to deploy the software testing environment?
AcWing 606. Average 1 (implemented in C language)
來吧元宇宙,果然這熱度一時半會兒過不去了
Random forest and poetry maker trained by AMR
MapReduce project case 1
PrecomputedTextCompat用法及原理