何度も申し訳ありません。どうかよろしくお願いします。
<ソートアルゴリズムの実装>
テンプレートを使用する
課題:
シェルソートの実装
クイックソート
マージソート
ヒープソート
添付ファイル:
シェルソート
クイックソート
マージソート
ヒープソート
実行例
> sort
データタイプ(1-3)
ランダム:1 昇順:2 降順:3 1
データ数: 10
比較: 45 回 交換: 20 回
(ソートテンプレート)
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
void sort(int *data, int n);
int compare(int a, int b);
void swap(int *d, int a, int b);
void initRandData(int *d, int n);
void initAscendingData(int *d, int n);
void initDescendingData(int *d, int n);
void printdata(int *d, int n);
unsigned long counterSwap;
unsigned long counterComp;
void sort(int data[], int n)
{
/* ここにソートプログラムを作成 */
}
int main(void)
{
int *data;
int n;
int i;
int com;
void (* initData[3])(int *d, int x) = {initRandData,
initAscendingData,
initDescendingData };
do {
printf("データタイプ(1-3)\n");
printf("ランダム:1 昇順:2 降順:3 ");
scanf("%d", &com);
} while (! (com==0||com==1||com==2||com==3));
printf("データ数: ");
scanf("%d", &n);
data = (int*) malloc(sizeof(int) *n);
if (com==0) {
for (i=0; i<n; i++) {
scanf("%d", &data[i]);
}
} else {
initData[com-1](data, n);
}
#ifdef DEBUG
printdata(data, n);
#endif
counterSwap = 0;
counterComp = 0;
sort(data, n);
#ifdef DEBUG
printdata(data, n);
#endif
printf(" 比較: %d 回\n", counterComp);
printf(" 交換: %d 回\n", counterSwap);
return 0;
}
int compare(int a, int b)
{
counterComp++;
return (a<b);
}
void swap(int *d, int a, int b)
{
int tmp;
counterSwap++;
tmp = d[a];
d[a] = d[b];
d[b] = tmp;
}
void initRandData(int *d, int n)
{
int i, s;
srand(time(NULL));
s=n*10;
for(i=0; i<n; i++) d[i]=rand()%s;
}
void initAscendingData(int *d, int n)
{
int i;
for(i=0; i<n; i++) d[i]=i;
}
void initDescendingData(int *d, int n)
{
int i;
for(i=0; i<n; i++) d[i]=n-i-1;
}
void printdata(int *d, int n)
{
int i;
for (i=0; i<n; i++) printf("%d ", d[i]);
printf("\n");
}
5L2V5bqm44KC55Sz44GX6Kiz44GC44KK44G+44Gb44KT44CC44Gp44GG44GL44KI44KN44GX44GP44GK6aGY44GE44GX44G+44GZ44CCCgrvvJzjgr3jg7zjg4jjgqLjg6vjgrTjg6rjgrrjg6Djga7lrp/oo4XvvJ4K44OG44Oz44OX44Os44O844OI44KS5L2/55So44GZ44KLCiAK6Kqy6aGM77yaCuOCt+OCp+ODq+OCveODvOODiOOBruWun+ijhQrjgq/jgqTjg4Pjgq/jgr3jg7zjg4gK44Oe44O844K444K944O844OICuODkuODvOODl+OCveODvOODiAoK5re75LuY44OV44Kh44Kk44Or77yaCuOCt+OCp+ODq+OCveODvOODiArjgq/jgqTjg4Pjgq/jgr3jg7zjg4gK44Oe44O844K444K944O844OICuODkuODvOODl+OCveODvOODiAogCuWun+ihjOS+iwo+IHNvcnQK44OH44O844K/44K/44Kk44OXKDEtMykK44Op44Oz44OA44OgOjEg5piH6aCGOjIg6ZmN6aCGOjMgMQrjg4fjg7zjgr/mlbA6IDEwCuavlOi8gzogNDUg5ZueIOS6pOaPmzogMjAg5ZueCiAKKOOCveODvOODiOODhuODs+ODl+ODrOODvOODiCkKI2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hbGxvYy5oPgogCnZvaWQgc29ydChpbnQgKmRhdGEsIGludCBuKTsKIAppbnQgY29tcGFyZShpbnQgYSwgaW50IGIpOwogCnZvaWQgc3dhcChpbnQgKmQsIGludCBhLCBpbnQgYik7CiAKdm9pZCBpbml0UmFuZERhdGEoaW50ICpkLCBpbnQgbik7CiAKdm9pZCBpbml0QXNjZW5kaW5nRGF0YShpbnQgKmQsIGludCBuKTsKIAp2b2lkIGluaXREZXNjZW5kaW5nRGF0YShpbnQgKmQsIGludCBuKTsKIAp2b2lkIHByaW50ZGF0YShpbnQgKmQsIGludCBuKTsKIAogCnVuc2lnbmVkIGxvbmcgY291bnRlclN3YXA7CnVuc2lnbmVkIGxvbmcgY291bnRlckNvbXA7CiAKIAogCiAKdm9pZCBzb3J0KGludCBkYXRhW10sIGludCBuKQp7CgkvKiDjgZPjgZPjgavjgr3jg7zjg4jjg5fjg63jgrDjg6njg6DjgpLkvZzmiJAgKi8KfQogCiAKIAppbnQgbWFpbih2b2lkKQp7CglpbnQgKmRhdGE7CglpbnQgbjsKCWludCBpOwoJaW50IGNvbTsKCXZvaWQgKCogaW5pdERhdGFbM10pKGludCAqZCwgaW50IHgpID0ge2luaXRSYW5kRGF0YSwKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGluaXRBc2NlbmRpbmdEYXRhLAoJICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgaW5pdERlc2NlbmRpbmdEYXRhIH07CiAKCWRvIHsKCQlwcmludGYoIuODh+ODvOOCv+OCv+OCpOODlygxLTMpXG4iKTsKCQlwcmludGYoIuODqeODs+ODgOODoDoxIOaYh+mghjoyIOmZjemghjozICAiKTsKCQlzY2FuZigiJWQiLCAmY29tKTsKCX0gd2hpbGUgKCEgKGNvbT09MHx8Y29tPT0xfHxjb209PTJ8fGNvbT09MykpOwogCglwcmludGYoIuODh+ODvOOCv+aVsDogIik7CglzY2FuZigiJWQiLCAmbik7CiAKCWRhdGEgPSAoaW50KikgbWFsbG9jKHNpemVvZihpbnQpICpuKTsKCWlmIChjb209PTApIHsKCQlmb3IgKGk9MDsgaTxuOyBpKyspIHsKCQkJc2NhbmYoIiVkIiwgJmRhdGFbaV0pOwoJCX0KCX0gZWxzZSB7CgkJaW5pdERhdGFbY29tLTFdKGRhdGEsIG4pOwoJfQogCiNpZmRlZiBERUJVRwoJcHJpbnRkYXRhKGRhdGEsIG4pOwojZW5kaWYKIAoJY291bnRlclN3YXAgPSAwOwoJY291bnRlckNvbXAgPSAwOwogCglzb3J0KGRhdGEsIG4pOwogCiNpZmRlZiBERUJVRwoJcHJpbnRkYXRhKGRhdGEsIG4pOwojZW5kaWYKIAoJcHJpbnRmKCIg5q+U6LyDOiAlZCDlm55cbiIsIGNvdW50ZXJDb21wKTsKCXByaW50ZigiIOS6pOaPmzogJWQg5ZueXG4iLCBjb3VudGVyU3dhcCk7CiAKCXJldHVybiAwOwp9CiAKaW50IGNvbXBhcmUoaW50IGEsIGludCBiKQp7Cgljb3VudGVyQ29tcCsrOwoJcmV0dXJuIChhPGIpOwp9CiAKdm9pZCBzd2FwKGludCAqZCwgaW50IGEsIGludCBiKQp7CglpbnQgdG1wOwogCgljb3VudGVyU3dhcCsrOwoJdG1wID0gZFthXTsKCWRbYV0gPSBkW2JdOwoJZFtiXSA9IHRtcDsKfQogCnZvaWQgaW5pdFJhbmREYXRhKGludCAqZCwgaW50IG4pCnsKCWludCBpLCBzOwoJc3JhbmQodGltZShOVUxMKSk7CglzPW4qMTA7Cglmb3IoaT0wOyBpPG47IGkrKykgZFtpXT1yYW5kKCklczsKfQogCnZvaWQgaW5pdEFzY2VuZGluZ0RhdGEoaW50ICpkLCBpbnQgbikKewoJaW50IGk7Cglmb3IoaT0wOyBpPG47IGkrKykgIGRbaV09aTsKfQogCnZvaWQgaW5pdERlc2NlbmRpbmdEYXRhKGludCAqZCwgaW50IG4pCnsKCWludCBpOwoJZm9yKGk9MDsgaTxuOyBpKyspICBkW2ldPW4taS0xOwp9CiAKdm9pZCBwcmludGRhdGEoaW50ICpkLCBpbnQgbikKewoJaW50IGk7CiAKCWZvciAoaT0wOyBpPG47IGkrKykgcHJpbnRmKCIlZCAiLCBkW2ldKTsKCXByaW50ZigiXG4iKTsKfQ==
Main.java:1: error: illegal character: \12290
??????????????????????????
^
Main.java:1: error: illegal character: \12290
??????????????????????????
^
Main.java:3: error: illegal character: \65308
??????????????
^
Main.java:3: error: illegal character: \65310
??????????????
^
Main.java:6: error: illegal character: \65306
???
^
Main.java:12: error: illegal character: \65306
???????
^
Main.java:26: error: illegal character: \35
#include <stdio.h>
^
Main.java:27: error: illegal character: \35
#include <stdlib.h>
^
Main.java:28: error: illegal character: \35
#include <malloc.h>
^
Main.java:32: error: class, interface, or enum expected
int compare(int a, int b);
^
Main.java:34: error: class, interface, or enum expected
void swap(int *d, int a, int b);
^
Main.java:36: error: class, interface, or enum expected
void initRandData(int *d, int n);
^
Main.java:38: error: class, interface, or enum expected
void initAscendingData(int *d, int n);
^
Main.java:40: error: class, interface, or enum expected
void initDescendingData(int *d, int n);
^
Main.java:42: error: class, interface, or enum expected
void printdata(int *d, int n);
^
Main.java:45: error: class, interface, or enum expected
unsigned long counterSwap;
^
Main.java:46: error: class, interface, or enum expected
unsigned long counterComp;
^
Main.java:51: error: class, interface, or enum expected
void sort(int data[], int n)
^
Main.java:61: error: class, interface, or enum expected
int n;
^
Main.java:62: error: class, interface, or enum expected
int i;
^
Main.java:63: error: class, interface, or enum expected
int com;
^
Main.java:64: error: class, interface, or enum expected
void (* initData[3])(int *d, int x) = {initRandData,
^
Main.java:68: error: class, interface, or enum expected
do {
^
Main.java:70: error: class, interface, or enum expected
printf("????:1 ??:2 ??:3 ");
^
Main.java:71: error: class, interface, or enum expected
scanf("%d", &com);
^
Main.java:72: error: class, interface, or enum expected
} while (! (com==0||com==1||com==2||com==3));
^
Main.java:74: error: class, interface, or enum expected
printf("????: ");
^
Main.java:75: error: class, interface, or enum expected
scanf("%d", &n);
^
Main.java:77: error: class, interface, or enum expected
data = (int*) malloc(sizeof(int) *n);
^
Main.java:78: error: class, interface, or enum expected
if (com==0) {
^
Main.java:79: error: class, interface, or enum expected
for (i=0; i<n; i++) {
^
Main.java:79: error: class, interface, or enum expected
for (i=0; i<n; i++) {
^
Main.java:81: error: class, interface, or enum expected
}
^
Main.java:84: error: class, interface, or enum expected
}
^
Main.java:86: error: illegal character: \35
#ifdef DEBUG
^
Main.java:88: error: illegal character: \35
#endif
^
Main.java:90: error: class, interface, or enum expected
counterSwap = 0;
^
Main.java:91: error: class, interface, or enum expected
counterComp = 0;
^
Main.java:93: error: class, interface, or enum expected
sort(data, n);
^
Main.java:95: error: illegal character: \35
#ifdef DEBUG
^
Main.java:95: error: class, interface, or enum expected
#ifdef DEBUG
^
Main.java:97: error: illegal character: \35
#endif
^
Main.java:99: error: class, interface, or enum expected
printf(" ??: %d ?\n", counterComp);
^
Main.java:100: error: class, interface, or enum expected
printf(" ??: %d ?\n", counterSwap);
^
Main.java:102: error: class, interface, or enum expected
return 0;
^
Main.java:103: error: class, interface, or enum expected
}
^
Main.java:108: error: class, interface, or enum expected
return (a<b);
^
Main.java:109: error: class, interface, or enum expected
}
^
Main.java:115: error: class, interface, or enum expected
counterSwap++;
^
Main.java:116: error: class, interface, or enum expected
tmp = d[a];
^
Main.java:117: error: class, interface, or enum expected
d[a] = d[b];
^
Main.java:118: error: class, interface, or enum expected
d[b] = tmp;
^
Main.java:119: error: class, interface, or enum expected
}
^
Main.java:124: error: class, interface, or enum expected
srand(time(NULL));
^
Main.java:125: error: class, interface, or enum expected
s=n*10;
^
Main.java:126: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=rand()%s;
^
Main.java:126: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=rand()%s;
^
Main.java:126: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=rand()%s;
^
Main.java:127: error: class, interface, or enum expected
}
^
Main.java:132: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=i;
^
Main.java:132: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=i;
^
Main.java:132: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=i;
^
Main.java:133: error: class, interface, or enum expected
}
^
Main.java:138: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=n-i-1;
^
Main.java:138: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=n-i-1;
^
Main.java:138: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=n-i-1;
^
Main.java:139: error: class, interface, or enum expected
}
^
Main.java:145: error: class, interface, or enum expected
for (i=0; i<n; i++) printf("%d ", d[i]);
^
Main.java:145: error: class, interface, or enum expected
for (i=0; i<n; i++) printf("%d ", d[i]);
^
Main.java:145: error: class, interface, or enum expected
for (i=0; i<n; i++) printf("%d ", d[i]);
^
Main.java:146: error: class, interface, or enum expected
printf("\n");
^
Main.java:147: error: class, interface, or enum expected
}
^
72 errors