#include<stdio.h>
void sort_b(int data1[], int n)
{
int i, j, tmp;
int count_b = 0;
for(i = 0; i < n; i++){
for(j = n - 1; j > i; j--){
if(data1[j] < data1[j-1]){
tmp = data1[j];
data1[j] = data1[j-1];
data1[j-1] = tmp;
count_b++;
}
}
}
for(i = 0; i < n; i++)
printf("バブルソートの交換回数 -> %d回\n\n",count_b
); }
void sort_s(int data2[], int n)
{
int i, j, tmp;
int count_s = 0;
for(i = 0; i < n; i++){
int max = i;
for(j = i+1; j < n; j++)
if(data2[j] > data2[max])max = j;
tmp = data2[i];
data2[i] = data2[max];
data2[max] = tmp;
count_s++;
}
for(i = 0; i < n; i++)
printf("選択ソートの交換回数 -> %d回\n",count_s
);
}
int main()
{
FILE *fp;
int data[20], i = 0;
fp
= fopen("data.txt","r"); if(fp == NULL){
return -1;
}
while(fscanf(fp
,"%d",&data
[i
]) != EOF
)i
++;
sort_b(data,i);
sort_s(data,i);
return 0;
}
I2luY2x1ZGU8c3RkaW8uaD4KCnZvaWQgc29ydF9iKGludCBkYXRhMVtdLCBpbnQgbikgCnsKICAgIGludCBpLCBqLCB0bXA7CiAgICBpbnQgY291bnRfYiA9IDA7Cglmb3IoaSA9IDA7IGkgPCBuOyBpKyspewoJCWZvcihqID0gbiAtIDE7IGogPiBpOyBqLS0pewoJCQlpZihkYXRhMVtqXSA8IGRhdGExW2otMV0pewoJCQkJdG1wID0gZGF0YTFbal07CgkJCQlkYXRhMVtqXSA9IGRhdGExW2otMV07CgkJCQlkYXRhMVtqLTFdID0gdG1wOwoJCQkJY291bnRfYisrOwoJCQl9CgkJfQoJfQoKCWZvcihpID0gMDsgaSA8IG47IGkrKykKCQlwcmludGYoIiVkXG4iLGRhdGExW2ldKTsKCXByaW50Zigi44OQ44OW44Or44K944O844OI44Gu5Lqk5o+b5Zue5pWwIC0+ICVk5ZueXG5cbiIsY291bnRfYik7Cn0KCnZvaWQgc29ydF9zKGludCBkYXRhMltdLCBpbnQgbikKewogICAgaW50IGksIGosIHRtcDsKCWludCBjb3VudF9zID0gMDsKCglmb3IoaSA9IDA7IGkgPCBuOyBpKyspewoJCWludCBtYXggPSBpOwoJCWZvcihqID0gaSsxOyBqIDwgbjsgaisrKQoJCQlpZihkYXRhMltqXSA+IGRhdGEyW21heF0pbWF4ID0gajsKCgkJCXRtcCA9IGRhdGEyW2ldOwoJCQlkYXRhMltpXSA9IGRhdGEyW21heF07CgkJCWRhdGEyW21heF0gPSB0bXA7CgkJCWNvdW50X3MrKzsKCX0KCglmb3IoaSA9IDA7IGkgPCBuOyBpKyspCgkJcHJpbnRmKCIlZFxuIixkYXRhMltpXSk7CglwcmludGYoIumBuOaKnuOCveODvOODiOOBruS6pOaPm+WbnuaVsCAtPiAlZOWbnlxuIixjb3VudF9zKTsKCn0KCmludCBtYWluKCkKewogICBGSUxFICpmcDsKICAgaW50IGRhdGFbMjBdLCBpID0gMDsKCiAgIGZwID0gZm9wZW4oImRhdGEudHh0IiwiciIpOwogICBpZihmcCA9PSBOVUxMKXsKCSAgIHB1dHMoIm9wZW4gZXJyb3IhISIpOwoJICAgcmV0dXJuIC0xOwogICB9CgogICB3aGlsZShmc2NhbmYoZnAsIiVkIiwmZGF0YVtpXSkgIT0gRU9GKWkrKzsKCiAgIHNvcnRfYihkYXRhLGkpOwogICBzb3J0X3MoZGF0YSxpKTsKCiAgIGZjbG9zZShmcCk7CiAgIAogICByZXR1cm4gMDsKfQo=