Sorting 快速排序(不能传递结果)
我在传递函数方面非常糟糕,在传递数组方面更是如此。我的代码有问题,我使用快速排序从外部文件对数据进行排序。函数无法传递排序结果 更新:函数可以传递排序,但是,从小到大排序,我想从大到小排序,怎么做 但是如果我使用简单的排序,比如Sorting 快速排序(不能传递结果),sorting,Sorting,我在传递函数方面非常糟糕,在传递数组方面更是如此。我的代码有问题,我使用快速排序从外部文件对数据进行排序。函数无法传递排序结果 更新:函数可以传递排序,但是,从小到大排序,我想从大到小排序,怎么做 但是如果我使用简单的排序,比如 int temp_score; string temp_name; for(int j=0;j<100;j++) { for(int k=0;k<100;k++) { if(sc
int temp_score;
string temp_name;
for(int j=0;j<100;j++)
{
for(int k=0;k<100;k++)
{
if(score[j]>score[k])
{
temp_score=score[j];
score[j]=score[k];
score[k]=temp_score;
temp_name=name[j];
name[j]=name[k];
name[k]=temp_name;
}
}
}
int-temp_分数;
字符串临时名称;
对于(int j=0;j要按降序排序,通常会将“”替换为i-,i++的限制是相反的-我的意思是作为一个一般准则,而不是盲目地到处替换。例如:
int temp_score;
string temp_name;
for(int j=0;j<100;j++)
{
for(int k=0;k<100;k++)
{
if(score[j]<score[k])
{
temp_score=score[j];
score[j]=score[k];
score[k]=temp_score;
temp_name=name[j];
name[j]=name[k];
name[k]=temp_name;
}
}
}
int-temp_分数;
字符串临时名称;
对于(intj=0;j如何按降序排序?)我很困惑
void quicksort(int score[],string name[],int kiri,int kanan){
int temp_score=0,i=kiri,j=kanan;
int pivot = score[(kiri+kanan)/2];
string temp_name;
//Pengurutan data berdasar Pivot
while (i <= j){
while (score[i]<pivot){
i++;
}while (score[j]>pivot){
j--;
}if (i <= j){
temp_score=score[i];
score[i]=score[j];
score[j]=temp_score;
temp_name=name[i];
name[i]=name[j];
name[j]=temp_name;
i++;
j--;
}
}
//Rekursif
if (kiri<j){
quicksort(score,name,kiri,j);
}if (i<kanan){
quicksort(score,name,i,kanan);
}
}
void快速排序(int分数[],字符串名称[],int kiri,int kanan){
int temp_得分=0,i=kiri,j=kanan;
int pivot=分数[(基里+卡南)/2];
字符串临时名称;
//彭古鲁坦数据中心
while(但是,我混淆了快速排序功能的实现,即按降序排序。@MuhamadEnrinal请参阅编辑。我已经尝试了您的代码,但存在问题,数据没有排序。@MuhamadEnrinal:请参阅我的编辑。
void quicksort(int score[],string name[],int kiri,int kanan){
int temp_score=0,i=kiri,j=kanan;
int pivot = score[(kiri+kanan)/2];
string temp_name;
//Pengurutan data berdasar Pivot
while (i <= j){
while (score[i]<pivot){
i++;
}while (score[j]>pivot){
j--;
}if (i <= j){
temp_score=score[i];
score[i]=score[j];
score[j]=temp_score;
temp_name=name[i];
name[i]=name[j];
name[j]=temp_name;
i++;
j--;
}
}
//Rekursif
if (kiri<j){
quicksort(score,name,kiri,j);
}if (i<kanan){
quicksort(score,name,i,kanan);
}
}