猿问

我找了一个快排代码,自己改了一下,但是无论怎么改都不能排全?该怎么办?

小弟在网上找了一个快排代码,自己改了一下,无论怎么改都不能排全。
请各位牛人帮忙,谢谢
[\code]#include<iostream>
using namespace std;
int n;
void kp(int a[],int b,int c)
{
int i=b,j=c,x=a[(b+c)/2];
do
{
while (a[i]<x&&i<j)
i++;
while (a[j]>x&&j>i)
j--;
if (i<=j)
{
swap(a[i],a[j]);
i++;
j--;
}
}while(i<=j);
if (b<j)
kp(a,b,j);
if (c>i)
kp(a,i,c);
}
int main()
{
int i,s=1;
cin>>n;
int a[n+1];
a[0]=0xfffffff;
for (i=1;i<=n;i++)
cin>>a[i];
cout<<endl;
kp(a,1,n);
for (i=1;i<=n;i++)
{
if (a[i]==a[i-1])
{
s++;
continue;
}
if (i!=1)
{
cout<<a[i-1]<<' '<<s<<endl;
s=1;
}
else
cout<<a[i]<<' '<<s<<endl;
}
return 0;
}
[code]

慕慕森
浏览 114回答 2
2回答

小唯快跑啊

这段代码应该没问题。#include<iostream>using namespace std;int a[200000],n;void sort(int low,int high){if(high-low<=9)//小优化,当序列小于9时,使用插入排序或选择排序可以减少少量的时间。我使用的是选择排序,被注释掉的那一大段代码是插入排序。{/*int i,j=low,k,tmp;for(i=low+1;i<=high;i++){j=low;while(a[i]>a[j]&&j<i) j++;if(j!=i){tmp=a[i];for(k=i;k>j;k--)a[k]=a[k-1];a[k]=tmp;}}*/int i,j;for(i=low;i<high;i++)for(j=i+1;j<=high;j++)if(a[j]>a[i])swap(a[i],a[j]);return ;}int v=a[low],i=low,j=high+1;do{do i++;while(a[i]<v);do j--;while(a[j]>v);if (i<j)swap(a[i],a[j]);}while(i<j);a[low]=a[j];a[j]=v;sort(low,j-1);sort(j+1,high);return ;}int main(){int i;scanf("%d",&n);for(i=0;i<n;i++)scanf("%d",&a[i]);sort(0,n-1);int tmp=a[0],s=1;for(i=1;i<n;i++)if(a[i]!=tmp){printf("%d %d\n",tmp,s);s=1;tmp=a[i];}elses++;printf("%d %d\n",tmp,s);return 0;}

弑天下

你的程序,没工夫看,不过前几天做了道题,也用到了快排,下面是我的程序,给你学习学习,我的快排是用递归写的。用到了三个函数。。事实上c++库函数中自带有qsort函数,可直接调用的,你也可以学习qsort函数的用法/* BabelfishTime Limit: 3000MS Memory Limit: 65536KTotal Submissions: 15011 Accepted: 6605DescriptionYou have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.InputInput consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words.Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign wordappears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line.Each word in the input is a sequence of at most 10 lowercase letters.OutputOutput is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".Sample Inputdog ogdaycat atcaypig igpayfroot ootfrayloops oopslayatcayittenkayoopslaySample OutputcatehloopsHintHuge input and output,scanf and printf are recommended.SourceWaterloo local 2001.09.22*/#include <iostream>using namespace std;struct dic_entry{char en[11];//表示english;char fo[11];//表示foreignlanguage} d[100001];//d表示dictionary; //真奇怪,如果将d声明为main()的成员函数,数组就不允许开这么大,就会运行错误void swap(int pos1,int pos2){char temp1[11]="";strcpy(temp1,d[pos1].en);strcpy(d[pos1].en,d[pos2].en);strcpy(d[pos2].en,temp1);char temp2[11]="";strcpy(temp2,d[pos1].fo);strcpy(d[pos1].fo,d[pos2].fo);strcpy(d[pos2].fo,temp2);}int partition(int low,int high)//这个函数返回的是pivot的位置{char pivot[11];int i,last_small;strcpy(pivot,d[low].fo);//last_small=low;for(i=low+1;i<=high;i++){if(strcmp(d[i].fo,pivot)<0){last_small=last_small+1;swap(i,last_small);//交换两个位置的值}}swap(last_small,low);//交换,将pivot放到它应该在的位置//for(i=low;i<=high;i++)return last_small;}void recursive_quick_sort(int low,int high){int pivot_position; //支点的位置if(low<high){pivot_position=partition(low,high);//这个函数是用于寻找支点的位置recursive_quick_sort(low,pivot_position-1);recursive_quick_sort(pivot_position+1,high);}}void QuickSort(int n)//按照fo字段进行排序{recursive_quick_sort(0,n-1);}void BinarySearch(char*word,int n,char* result) //n为字典d中message的个数{strcpy(result,"eh");//如果没有查到就放回eh,这是题目的要求int low=0;int high=n-1;int mid=0;while(low<=high){mid=(low+high)/2;int flag=strcmp(d[mid].fo,word);if(flag==0){strcpy(result,d[mid].en);return ;}if(flag>0){high=mid-1;}else low =mid+1;}}int main(){int n=0;char s[40];while(gets(s)){int len=strlen(s);if(len==0)break;int i=0;for(i=0; s[i]!=' ';i++ )d[n].en[i]=s[i];d[n].en[i]='\0';int k=0;for(int j=i+1; j<len ; j++){d[n].fo[k]=s[j];k++;}d[n].fo[k]='\0';n++;}//对字典按照外文的字典顺序排序QuickSort(n);// cout<<"排序后的结果"<<endl;//for(int i=0;i<n;i++)//cout<<d[i].en<<" "<<d[i].fo<<endl;char word[11];while(cin>>word)// 输入要查询的外文单词放到数组words中{char result[11];BinarySearch(word,n,result);cout<<result<<endl;}return 0;}
随时随地看视频慕课网APP
我要回答