任务描述
1)读取文件“sort.txt”,文件第一行是十个数字“65,43,79,21,298,12,3,54,44,1”;
2)对读取的“sort.txt”中的十个数字使用冒泡排序进行排序
3)将排序结果写到“result.txt”目录下。
用ubantu完成
任务描述
1)读取文件“sort.txt”,文件第一行是十个数字“65,43,79,21,298,12,3,54,44,1”;
2)对读取的“sort.txt”中的十个数字使用冒泡排序进行排序
3)将排序结果写到“result.txt”目录下。
用ubantu完成
#include <stdio.h>
#include<stdlib.h>
#define swap(a, b) \
{ \
a ^= b; \
b ^= a; \
a ^= b; \
}
void bubbleSort(int *array, int n)
{
int i, j, flag;
for (i = 0; i < n - 1; i++)
{
flag = 0;
for (j = n - 1; j > i; j--)
{
if (array[j - 1] > array[j])
{
swap(array[j - 1], array[j]);
flag = 1;
}
}
if (flag == 0)
break;
}
}
int main()
{
FILE *from_fd,*to_fd;
char *filename = "./sort.txt", *filename1="./result.txt";
from_fd = fopen(filename, "r+");
to_fd = fopen(filename1, "w+");
if (from_fd == NULL)
{
printf("open source file failed!\n");
return -1;
}
if (to_fd == NULL)
{
printf("open source file failed!\n");
return -1;
}
int Array[10];
int i;
for(i=0;i<10;i++){
fscanf(from_fd,"%d",&Array[i]);
}
printf("\n");
bubbleSort(Array,sizeof(Array)/sizeof(int));
for(i=0;i<10;i++){
fprintf(to_fd,"%d ",Array[i]);
}
close(to_fd);
close(from_fd);
}