Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- #define MAX 10000
- #define in "in.txt"
- #define out "out.txt"
- void insertionsort(int a[], int n)
- {
- int i,j,key;
- for (int j = 1; j < n; ++j)
- {
- key = a[j];
- i = j -1;
- while(i>=0&&a[i]>key)
- {
- a[i+1] = a[i];
- i = i-1;
- }
- a[i+1] = key;
- }
- }
- void GhiFileSoNguyen(int n)
- {
- FILE *f;
- f = fopen(in,"wt");
- if(f==NULL)
- {
- printf("Khong tao duoc file\n");
- //getch();
- exit(0);
- }
- for (int i = 0; i < n; ++i)
- {
- fprintf(f, "%d ", rand()%10000);
- }
- fprintf(f, "\n" );
- fclose(f);
- }
- void GhiFile(int a[MAX], int n)
- {
- FILE *f;
- f = fopen(out,"wt");
- if(f==NULL)
- {
- printf("Khong tao duoc file\n");
- //getch();
- exit(0);
- }
- for (int i = 0; i < n; ++i)
- {
- fprintf(f, "%d\n", a[i]);
- }
- fprintf(f, "\n" );
- fclose(f);
- }
- void DocFile(int a[MAX], int n)
- {
- FILE *f;
- f = fopen(in,"rt");
- if(f==NULL)
- {
- printf("Khong doc duoc file\n");
- //getch();
- exit(0);
- }
- for (int i = 0; i < n; ++i)
- {
- fscanf(f, "%d\l", &a[i]);
- }
- fclose(f);
- }
- int main(int argc, char const *argv[])
- {
- int a[MAX];
- GhiFileSoNguyen(10000);
- DocFile(a,10000);
- insertionsort(a,10000);
- GhiFile(a,10000);
- for(int i=0; i<10000; i++)
- printf("%d\n",a[i]);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment