본문 바로가기
Programming/C

[C] text file 정렬

by graygreat 2017. 5. 13.
728x90
반응형


input.txt


0, 5, 9, 7, 5, 3, 2, 4, 8, 6, 9, 10, 31, -1, 25, -5, -4, -9, 1, 2, 8, 6, 7, 4, 9, 1, 0, 20, 83, 

147, -3, -8, -6, 2, -8, 14, 8, 3, 4, 85, -7, 69, 14, 85, 75, 1000, 32, -50, -44, -9009, -48, 68, 52



#include <stdio.h> void bubble(int arr[], int num); void select(int arr[], int num); void main(){ int buf[100] = {0, }; FILE *fp = fopen("input.txt", "r"); int count = 0; int i = 0; while(!feof(fp)){ fscanf(fp, "%d, ", &buf[count]); count++; } fclose(fp); // bubble(buf, count); select(buf, count); FILE *fr = fopen("output.txt", "w"); while(i < count){ fprintf(fr, "%d, ", buf[i]); i++; } fclose(fr); printf("\n"); } // 버블 정렬 void bubble(int arr[], int num){ int i, j; int temp = 0; for(i = 0; i < num; i++){ for(j = 0; j < num - 1; j++){ if(arr[j] > arr[j + 1]){ temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } // 선택 정렬 void select(int arr[], int num){ int i, j; int min; int temp = 0; for(i = 0; i < num - 1; i++){ min = i; for(j = i + 1; j < num ; j++){ if(arr[j] < arr[min]) min = j; } temp = arr[min]; arr[min] = arr[i]; arr[i] = temp; } }


output.txt


-9009, -50, -48, -44, -9, -8, -8, -7, -6, -5, -4, -3, -1, 0, 0, 0, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 5,

5, 6, 6, 7, 7, 8, 8, 8, 9, 9, 9, 10, 14, 14, 20, 25, 31, 32, 52, 68, 69, 75, 83, 85, 85, 147, 1000,


반응형

댓글