以前のソートの者です。
<ソートの実装>
今回は、シェル・クイック・マージ・ヒープのソートの実装をせよ。というものです。
よろしくお願いします。
(ソートテンプレート)
#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");
}
5Lul5YmN44Gu44K944O844OI44Gu6ICF44Gn44GZ44CCCgrvvJzjgr3jg7zjg4jjga7lrp/oo4XvvJ4K5LuK5Zue44Gv44CB44K344Kn44Or44O744Kv44Kk44OD44Kv44O744Oe44O844K444O744OS44O844OX44Gu44K944O844OI44Gu5a6f6KOF44KS44Gb44KI44CC44Go44GE44GG44KC44Gu44Gn44GZ44CCCuOCiOOCjeOBl+OBj+OBiumhmOOBhOOBl+OBvuOBmeOAggoK77yI44K944O844OI44OG44Oz44OX44Os44O844OIKQojaW5jbHVkZSA8c3RkaW8uaD4KI2luY2x1ZGUgPHN0ZGxpYi5oPgojaW5jbHVkZSA8bWFsbG9jLmg+Cgp2b2lkIHNvcnQoaW50ICpkYXRhLCBpbnQgbik7CgppbnQgY29tcGFyZShpbnQgYSwgaW50IGIpOwoKdm9pZCBzd2FwKGludCAqZCwgaW50IGEsIGludCBiKTsKCnZvaWQgaW5pdFJhbmREYXRhKGludCAqZCwgaW50IG4pOwoKdm9pZCBpbml0QXNjZW5kaW5nRGF0YShpbnQgKmQsIGludCBuKTsKCnZvaWQgaW5pdERlc2NlbmRpbmdEYXRhKGludCAqZCwgaW50IG4pOwoKdm9pZCBwcmludGRhdGEoaW50ICpkLCBpbnQgbik7CgoKdW5zaWduZWQgbG9uZyBjb3VudGVyU3dhcDsKdW5zaWduZWQgbG9uZyBjb3VudGVyQ29tcDsKCgoKCnZvaWQgc29ydChpbnQgZGF0YVtdLCBpbnQgbikKewoJLyog44GT44GT44Gr44K944O844OI44OX44Ot44Kw44Op44Og44KS5L2c5oiQICovCn0KCgoKaW50IG1haW4odm9pZCkKewoJaW50ICpkYXRhOwoJaW50IG47CglpbnQgaTsKCWludCBjb207Cgl2b2lkICgqIGluaXREYXRhWzNdKShpbnQgKmQsIGludCB4KSA9IHtpbml0UmFuZERhdGEsCgkgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpbml0QXNjZW5kaW5nRGF0YSwKCSAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgIGluaXREZXNjZW5kaW5nRGF0YSB9OwoKCWRvIHsKCQlwcmludGYoIuODh+ODvOOCv+OCv+OC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T1yYW5kKCklczsKfQoKdm9pZCBpbml0QXNjZW5kaW5nRGF0YShpbnQgKmQsIGludCBuKQp7CglpbnQgaTsKCWZvcihpPTA7IGk8bjsgaSsrKSAgZFtpXT1pOwp9Cgp2b2lkIGluaXREZXNjZW5kaW5nRGF0YShpbnQgKmQsIGludCBuKQp7CglpbnQgaTsKCWZvcihpPTA7IGk8bjsgaSsrKSAgZFtpXT1uLWktMTsKfQoKdm9pZCBwcmludGRhdGEoaW50ICpkLCBpbnQgbikKewoJaW50IGk7CgoJZm9yIChpPTA7IGk8bjsgaSsrKSBwcmludGYoIiVkICIsIGRbaV0pOwoJcHJpbnRmKCJcbiIpOwp9CgoKCg==
Main.java:1: error: illegal character: \12290
???????????
^
Main.java:3: error: illegal character: \65308
????????
^
Main.java:3: error: illegal character: \65310
????????
^
Main.java:4: error: illegal character: \12289
???????????????????????????????????????
^
Main.java:4: error: illegal character: \12539
???????????????????????????????????????
^
Main.java:4: error: illegal character: \12539
???????????????????????????????????????
^
Main.java:4: error: illegal character: \12539
???????????????????????????????????????
^
Main.java:4: error: illegal character: \12290
???????????????????????????????????????
^
Main.java:4: error: illegal character: \12290
???????????????????????????????????????
^
Main.java:5: error: illegal character: \12290
???????????
^
Main.java:7: error: illegal character: \65288
??????????)
^
Main.java:8: error: illegal character: \35
#include <stdio.h>
^
Main.java:9: error: illegal character: \35
#include <stdlib.h>
^
Main.java:10: error: illegal character: \35
#include <malloc.h>
^
Main.java:14: error: class, interface, or enum expected
int compare(int a, int b);
^
Main.java:16: error: class, interface, or enum expected
void swap(int *d, int a, int b);
^
Main.java:18: error: class, interface, or enum expected
void initRandData(int *d, int n);
^
Main.java:20: error: class, interface, or enum expected
void initAscendingData(int *d, int n);
^
Main.java:22: error: class, interface, or enum expected
void initDescendingData(int *d, int n);
^
Main.java:24: error: class, interface, or enum expected
void printdata(int *d, int n);
^
Main.java:27: error: class, interface, or enum expected
unsigned long counterSwap;
^
Main.java:28: error: class, interface, or enum expected
unsigned long counterComp;
^
Main.java:33: error: class, interface, or enum expected
void sort(int data[], int n)
^
Main.java:43: error: class, interface, or enum expected
int n;
^
Main.java:44: error: class, interface, or enum expected
int i;
^
Main.java:45: error: class, interface, or enum expected
int com;
^
Main.java:46: error: class, interface, or enum expected
void (* initData[3])(int *d, int x) = {initRandData,
^
Main.java:50: error: class, interface, or enum expected
do {
^
Main.java:52: error: class, interface, or enum expected
printf("????:1 ??:2 ??:3 ");
^
Main.java:53: error: class, interface, or enum expected
scanf("%d", &com);
^
Main.java:54: error: class, interface, or enum expected
} while (! (com==0||com==1||com==2||com==3));
^
Main.java:56: error: class, interface, or enum expected
printf("????: ");
^
Main.java:57: error: class, interface, or enum expected
scanf("%d", &n);
^
Main.java:59: error: class, interface, or enum expected
data = (int*) malloc(sizeof(int) *n);
^
Main.java:60: error: class, interface, or enum expected
if (com==0) {
^
Main.java:61: error: class, interface, or enum expected
for (i=0; i<n; i++) {
^
Main.java:61: error: class, interface, or enum expected
for (i=0; i<n; i++) {
^
Main.java:63: error: class, interface, or enum expected
}
^
Main.java:66: error: class, interface, or enum expected
}
^
Main.java:68: error: illegal character: \35
#ifdef DEBUG
^
Main.java:70: error: illegal character: \35
#endif
^
Main.java:72: error: class, interface, or enum expected
counterSwap = 0;
^
Main.java:73: error: class, interface, or enum expected
counterComp = 0;
^
Main.java:75: error: class, interface, or enum expected
sort(data, n);
^
Main.java:77: error: illegal character: \35
#ifdef DEBUG
^
Main.java:77: error: class, interface, or enum expected
#ifdef DEBUG
^
Main.java:79: error: illegal character: \35
#endif
^
Main.java:81: error: class, interface, or enum expected
printf(" ??: %d ?\n", counterComp);
^
Main.java:82: error: class, interface, or enum expected
printf(" ??: %d ?\n", counterSwap);
^
Main.java:84: error: class, interface, or enum expected
return 0;
^
Main.java:85: error: class, interface, or enum expected
}
^
Main.java:90: error: class, interface, or enum expected
return (a<b);
^
Main.java:91: error: class, interface, or enum expected
}
^
Main.java:97: error: class, interface, or enum expected
counterSwap++;
^
Main.java:98: error: class, interface, or enum expected
tmp = d[a];
^
Main.java:99: error: class, interface, or enum expected
d[a] = d[b];
^
Main.java:100: error: class, interface, or enum expected
d[b] = tmp;
^
Main.java:101: error: class, interface, or enum expected
}
^
Main.java:106: error: class, interface, or enum expected
srand(time(NULL));
^
Main.java:107: error: class, interface, or enum expected
s=n*10;
^
Main.java:108: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=rand()%s;
^
Main.java:108: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=rand()%s;
^
Main.java:108: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=rand()%s;
^
Main.java:109: error: class, interface, or enum expected
}
^
Main.java:114: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=i;
^
Main.java:114: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=i;
^
Main.java:114: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=i;
^
Main.java:115: error: class, interface, or enum expected
}
^
Main.java:120: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=n-i-1;
^
Main.java:120: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=n-i-1;
^
Main.java:120: error: class, interface, or enum expected
for(i=0; i<n; i++) d[i]=n-i-1;
^
Main.java:121: error: class, interface, or enum expected
}
^
Main.java:127: error: class, interface, or enum expected
for (i=0; i<n; i++) printf("%d ", d[i]);
^
Main.java:127: error: class, interface, or enum expected
for (i=0; i<n; i++) printf("%d ", d[i]);
^
Main.java:127: error: class, interface, or enum expected
for (i=0; i<n; i++) printf("%d ", d[i]);
^
Main.java:128: error: class, interface, or enum expected
printf("\n");
^
Main.java:129: error: class, interface, or enum expected
}
^
77 errors