ポインタへのポインタを使ってクイックソート
Posted: 2010年6月27日(日) 12:46
[1] 初めてこのサイトを利用させてもらいます><
初心者です。ポインタへのポインタを使ってquickSort関数を自分で作る学校の課題なのですが、
完成したとおもったらエラーがでてしまってどこが間違ってるかがよくわかりません。。
[1.1] 教科の合計点(sum)をクイックソートを用いて高→低順に並べていきたい。
[1.2] #include <stdio.h>
#include <stdlib.h>
struct student{
int number;
int math;
int eng;
int phy;
int sum;
struct student *next;
};
typedef struct student Student;
void swap(Student **x, Student **y);
void quickSort(Student **sort, int first, int c);
int main(void)
{
Student *top,**sort,**sortTop,*tmp;
char line[80];
int count=0,a=0;
top=(Student *)malloc(sizeof(Student));
top->number=-1; top->math=-1; top->eng=-1; top->phy=-1; top->sum=-1;
top->next=NULL;
while(fgets(line,sizeof(line),stdin)!=NULL){
tmp=(Student *)malloc(sizeof(Student));
if(tmp==NULL){
fprintf(stderr,"No more memory...\n");
return(1);
}
count++;
tmp->next=top->next;
top->next=tmp;
sscanf(line,"%d,%d,%d,%d",
&(tmp->number),
&(tmp->eng),
&(tmp->math),
&(tmp->phy));&(tmp->number),
tmp->sum=(tmp->eng)+(tmp->math)+(tmp->phy);
}
sort=(Student **)malloc((count+1)*sizeof(Student *));
sortTop=sort;
tmp=top->next;
while(tmp!=NULL){
*sort=tmp;
tmp=tmp->next;
sort++;
}
sort=NULL;
sort=sortTop;
quickSort(sort,a,count);
sort=sortTop;
while(*sort!=NULL){
fprintf(stdout,"%8d\t%3d\t%3d\t%3d\t%3d\n",
(*sort)->number,
(*sort)->eng,
(*sort)->math,
(*sort)->phy,
(*sort)->sum);
sort++;
}
return(0);
}
void quickSort(Student **sort, int first, int c)
{
Student **t1,**t2,**pivot;
int left,right;
left=first;
right=c;
t1=sort+first;
t2=sort+c;
pivot=sort+(c/2);
while(1){
while(((*t1)->sum)<((*pivot)->sum)){
t1++;
first++;
}
while(((*pivot)->sum)<((*t2)->sum)){
t2--;
c--;
}
if(t1 >= t2){
break;
}
swap(t1,t2);
t1++;
t2--;
first++;
c--;
}
if(left<first-1){
quickSort(sort,left,first-1);
}
if(c+1<right){
quickSort(sort,c+1,right);
}
return;
}
void swap(Student **x, Student **y)
{
Student *tmp;
tmp=*x;
*x=*y;
*y=tmp;
return;
}
[1.3] このCソースをコンパイルした場合のコンパイルエラーが
6[main]a 9272_cygtls::handle_exceptions:Error while dumping state
(probably corrupted stack)
とでます。
どうしてこのようなコンパイルエラーがおきるのか、
またこの方法でのクイックソートは間違っていないのかを指導してもらえると助かります。
初心者です。ポインタへのポインタを使ってquickSort関数を自分で作る学校の課題なのですが、
完成したとおもったらエラーがでてしまってどこが間違ってるかがよくわかりません。。
[1.1] 教科の合計点(sum)をクイックソートを用いて高→低順に並べていきたい。
[1.2] #include <stdio.h>
#include <stdlib.h>
struct student{
int number;
int math;
int eng;
int phy;
int sum;
struct student *next;
};
typedef struct student Student;
void swap(Student **x, Student **y);
void quickSort(Student **sort, int first, int c);
int main(void)
{
Student *top,**sort,**sortTop,*tmp;
char line[80];
int count=0,a=0;
top=(Student *)malloc(sizeof(Student));
top->number=-1; top->math=-1; top->eng=-1; top->phy=-1; top->sum=-1;
top->next=NULL;
while(fgets(line,sizeof(line),stdin)!=NULL){
tmp=(Student *)malloc(sizeof(Student));
if(tmp==NULL){
fprintf(stderr,"No more memory...\n");
return(1);
}
count++;
tmp->next=top->next;
top->next=tmp;
sscanf(line,"%d,%d,%d,%d",
&(tmp->number),
&(tmp->eng),
&(tmp->math),
&(tmp->phy));&(tmp->number),
tmp->sum=(tmp->eng)+(tmp->math)+(tmp->phy);
}
sort=(Student **)malloc((count+1)*sizeof(Student *));
sortTop=sort;
tmp=top->next;
while(tmp!=NULL){
*sort=tmp;
tmp=tmp->next;
sort++;
}
sort=NULL;
sort=sortTop;
quickSort(sort,a,count);
sort=sortTop;
while(*sort!=NULL){
fprintf(stdout,"%8d\t%3d\t%3d\t%3d\t%3d\n",
(*sort)->number,
(*sort)->eng,
(*sort)->math,
(*sort)->phy,
(*sort)->sum);
sort++;
}
return(0);
}
void quickSort(Student **sort, int first, int c)
{
Student **t1,**t2,**pivot;
int left,right;
left=first;
right=c;
t1=sort+first;
t2=sort+c;
pivot=sort+(c/2);
while(1){
while(((*t1)->sum)<((*pivot)->sum)){
t1++;
first++;
}
while(((*pivot)->sum)<((*t2)->sum)){
t2--;
c--;
}
if(t1 >= t2){
break;
}
swap(t1,t2);
t1++;
t2--;
first++;
c--;
}
if(left<first-1){
quickSort(sort,left,first-1);
}
if(c+1<right){
quickSort(sort,c+1,right);
}
return;
}
void swap(Student **x, Student **y)
{
Student *tmp;
tmp=*x;
*x=*y;
*y=tmp;
return;
}
[1.3] このCソースをコンパイルした場合のコンパイルエラーが
6[main]a 9272_cygtls::handle_exceptions:Error while dumping state
(probably corrupted stack)
とでます。
どうしてこのようなコンパイルエラーがおきるのか、
またこの方法でのクイックソートは間違っていないのかを指導してもらえると助かります。